-
Notifications
You must be signed in to change notification settings - Fork 2
refactor: introduce AbstractResource base class for resource interface #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |||||||||||||||||||||||||||
| # License for the specific language governing permissions and limitations | ||||||||||||||||||||||||||||
| # under the License. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import abc | ||||||||||||||||||||||||||||
| from functools import partial | ||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||
| import enum | ||||||||||||||||||||||||||||
|
|
@@ -506,7 +507,62 @@ class Requirement( | |||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| class AbstractResource(metaclass=abc.ABCMeta): | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def get_uri(self): | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def to_str(self, field: str) -> str: | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def get_parameter_value(self, parameter): | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def get_actual_state_safe(self): | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def render_target_state(self, engine=None): | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def link(self): | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def get_provider_element(self): | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def kind(self): | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def calculate_full_hash(self): | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def actualize(self): | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def delete(self, session=None): | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||
| @abc.abstractmethod | ||||||||||||||||||||||||||||
| def original(self): | ||||||||||||||||||||||||||||
| raise NotImplementedError("Not implemented") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| class Resource( | ||||||||||||||||||||||||||||
| AbstractResource, | ||||||||||||||||||||||||||||
| models.ModelWithUUID, | ||||||||||||||||||||||||||||
| models.ModelWithTimestamp, | ||||||||||||||||||||||||||||
| models.CustomPropertiesMixin, | ||||||||||||||||||||||||||||
|
|
@@ -891,16 +947,14 @@ def link(self): | |||||||||||||||||||||||||||
| return f"{self.element.link}.imports.${self.name}" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| class ImportedResource: | ||||||||||||||||||||||||||||
| class ImportedResource(AbstractResource): | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def __init__(self, element, resource, name): | ||||||||||||||||||||||||||||
| super().__init__() | ||||||||||||||||||||||||||||
| self._element = element | ||||||||||||||||||||||||||||
| self._resource = resource | ||||||||||||||||||||||||||||
| self._name = name | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def __getattr__(self, name): | ||||||||||||||||||||||||||||
| return getattr(self._resource, name) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def get_parameter_value(self, parameter): | ||||||||||||||||||||||||||||
| return type(self._resource).get_parameter_value(self, parameter) | ||||||||||||||||||||||||||||
|
Comment on lines
958
to
959
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation is broken after removing A simple delegation to I suggest reconstructing the parameter string to use the original resource's name and then delegating the call. This correctly reuses the logic without code duplication and fixes the bug.
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -916,6 +970,38 @@ def name(self): | |||||||||||||||||||||||||||
| def link(self): | ||||||||||||||||||||||||||||
| return f"{self.element.link}.imports.${self.name}" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def get_uri(self): | ||||||||||||||||||||||||||||
| return self._resource.get_uri() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def to_str(self, field: str) -> str: | ||||||||||||||||||||||||||||
| return self._resource.to_str(field) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def get_actual_state_safe(self): | ||||||||||||||||||||||||||||
| return self._resource.get_actual_state_safe() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def render_target_state(self, engine=None): | ||||||||||||||||||||||||||||
| return self._resource.render_target_state(engine) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def get_provider_element(self): | ||||||||||||||||||||||||||||
| return self._resource.get_provider_element() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||
| def kind(self): | ||||||||||||||||||||||||||||
| return self._resource.kind | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def calculate_full_hash(self): | ||||||||||||||||||||||||||||
| return self._resource.calculate_full_hash() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def actualize(self): | ||||||||||||||||||||||||||||
| return self._resource.actualize() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def delete(self, session=None): | ||||||||||||||||||||||||||||
| return self._resource.delete(session) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||
| def original(self): | ||||||||||||||||||||||||||||
| return self._resource | ||||||||||||||||||||||||||||
|
Comment on lines
+973
to
+1003
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency and improved type safety, consider adding type hints to these delegated methods, similar to how def get_uri(self) -> str:
return self._resource.get_uri()
def to_str(self, field: str) -> str:
return self._resource.to_str(field)
def get_actual_state_safe(self) -> dict:
return self._resource.get_actual_state_safe()
def render_target_state(self, engine: 'ElementEngine' | None = None) -> dict:
return self._resource.render_target_state(engine)
def get_provider_element(self) -> 'Element':
return self._resource.get_provider_element()
@property
def kind(self) -> str:
return self._resource.kind
def calculate_full_hash(self) -> str:
return self._resource.calculate_full_hash()
def actualize(self) -> None:
return self._resource.actualize()
def delete(self, session: 'tp.Any' | None = None) -> None:
return self._resource.delete(session)
@property
def original(self) -> 'Resource':
return self._resource |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| class OutdatedResources(models.ModelWithUUID, orm.SQLStorableMixin): | ||||||||||||||||||||||||||||
| __tablename__ = "em_outdated_resources_view" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve type safety and align with the PR's goal, it would be beneficial to add type hints to the abstract methods. This will make the interface clearer for implementers. You may need to import
typing as tpand use forward references (string quotes) for types defined later in the file or for circular dependencies.