[docs]classBaseReference(BaseModel):""" Base class for references. Mostly there to add some interface documentation. """
[docs]def__init__(self,/,**data:Any)->None:# type: ignore"""Create a new model by parsing and validating input data from keyword arguments. Raises :class:`pydantic_core.ValidationError` if the input data cannot be validated. """super().__init__(**data)
[docs]@classmethoddefbuild_from_yaml(cls,loader,node)->Self:""" Secondary constructor implementing the pyyaml constructor interface. """raiseNotImplementedError("YAML constructor not implemented")
[docs]classLazyServiceReference(BaseReference):""" Reference to a service, that will be resolved the latest possible, when the instance will actually be needed. """#: Reference target, a.k.a. the service symbolic name. Can be either a string, or a list of strings. The later will#: resolve as the first available service in the list.target:str|list[str]
[docs]defresolve(self,resolver,context):""" Resolve reference value in using the given resolver (callable) and resolution context. """returnresolver(self.target,context)
operators={"==":operator.eq," is not ":operator.is_not," is ":operator.is_,}
[docs]classLazySettingReference(BaseReference):""" Reference to a setting value, that will be resolved when the settings are bound to the service definitions collection, when calling :meth:`harp.services.Container.load`. Use this in `services.yml` files using the `!cfg` constructor: .. code:: yaml foo: !cfg "foo" Can be provided a two-element list for defaults: .. code:: yaml foo: !cfg ["foo", "default if no «foo» in bound settings"] """target:strdefault:Any=_notset
[docs]defresolve(self,settings):operator=Noneif"=="inself.target:target,operator,other_operand=self.target.partition("==")elif" is not "inself.target:target,operator,other_operand=self.target.partition(" is not ")elif" is "inself.target:target,operator,other_operand=self.target.partition(" is ")else:target=self.targetother_operand=Nonex=settingsforpartintarget.strip().split("."):try:x=x[part]except(TypeError,KeyError):x=self.defaultbreakifoperatorandother_operand:other_operand=eval(other_operand.strip())ifoperatorinoperators:returnoperators[operator](x,other_operand)orNoneelse:raiseValueError(f"Unsupported operator: {operator}")returnxifxisnot_notsetelseNone