|
| 1 | +import re |
| 2 | +from typing import Mapping, Union |
| 3 | + |
| 4 | +from viam.proto.app.robot import ComponentConfig |
| 5 | +from viam.proto.common import ResourceName |
| 6 | + |
| 7 | +from .. import logging |
| 8 | +from .base import ResourceBase |
| 9 | +from .types import Model, ModelFamily, Subtype |
| 10 | +from .registry import Registry, ResourceCreatorRegistration |
| 11 | + |
| 12 | +modelRegex = re.compile(r"^([^:]+):([^:]+):([^:]+)$") |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +def _parse_model(orig: Union[str, Model]) -> Model: |
| 18 | + "take a model or string and turn it into a Model" |
| 19 | + if isinstance(orig, Model): |
| 20 | + return orig |
| 21 | + match = modelRegex.match(orig) |
| 22 | + if not match: |
| 23 | + raise ValueError(f"MODEL {orig} doesn't match expected format 'org:type:name'") |
| 24 | + *family, name = match.groups() |
| 25 | + return Model(ModelFamily(*family), name) |
| 26 | + |
| 27 | + |
| 28 | +class EasyResource: |
| 29 | + """ |
| 30 | + EasyResource is a mixin that simplifies the process of creating Viam modules (extension programs) |
| 31 | + and resources (the resource classes provided by those extension programs). |
| 32 | +
|
| 33 | + Basic usage: |
| 34 | +
|
| 35 | + class MyModel(Sensor, EasyResource): |
| 36 | + MODEL = "my-org:sensor:my-sensor" |
| 37 | +
|
| 38 | + async def get_readings(self, **kwargs): |
| 39 | + return {"ok": True} |
| 40 | +
|
| 41 | + See examples/easy_resource/main.py for extended usage. |
| 42 | + """ |
| 43 | + SUBTYPE: Subtype |
| 44 | + MODEL: Model |
| 45 | + |
| 46 | + def __init_subclass__(cls, register=True, **kwargs): |
| 47 | + """ |
| 48 | + When you subclass this mixin, it parses cls.MODEL and registers cls in global registry. |
| 49 | + """ |
| 50 | + super().__init_subclass__(**kwargs) |
| 51 | + if not hasattr(cls, "MODEL"): |
| 52 | + raise ValueError("please define a MODEL like 'org:type:name' on your class, for example 'viam:camera:IMX219'") |
| 53 | + cls.MODEL = _parse_model(cls.MODEL) |
| 54 | + if register: |
| 55 | + cls.register() |
| 56 | + |
| 57 | + def __init__(self, name: str): |
| 58 | + # note: this mirrors the constructor for ComponentBase and ServiceBase. |
| 59 | + self.name = name |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def new(cls, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]): |
| 63 | + """ |
| 64 | + This is passed to register_resource_creator; the default implementation calls reconfigure() |
| 65 | + when an instance of your model is instantiated. You can override this in your subclass. |
| 66 | + """ |
| 67 | + self = cls(config.name) |
| 68 | + logger.debug("created %s %s %s", self.SUBTYPE, self.MODEL, config.name) |
| 69 | + self.reconfigure(config, dependencies) |
| 70 | + return self |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def register(cls): |
| 74 | + """ |
| 75 | + This adds the model to the global registry. It is called by __init_subclass__ and you typically |
| 76 | + won't call it directly. |
| 77 | + """ |
| 78 | + logger.debug('registering %s %s', cls.SUBTYPE, cls.MODEL) |
| 79 | + # note: We could fix this pyright-ignore if EasyResource inherited ResourceBase, but that crashes in the mro() |
| 80 | + # walk in ResourceManager.register. |
| 81 | + Registry.register_resource_creator( |
| 82 | + cls.SUBTYPE, cls.MODEL, ResourceCreatorRegistration(cls.new)) # pyright: ignore [reportArgumentType] |
| 83 | + |
| 84 | + def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]): |
| 85 | + logger.debug('reconfigure %s %s', self.SUBTYPE, self.MODEL) |
0 commit comments