Skip to content

Commit

Permalink
Add commit and after_configure events
Browse files Browse the repository at this point in the history
  • Loading branch information
frthjf committed Mar 23, 2024
1 parent 3b75318 commit 0bbf6f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/machinable/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ def config(self) -> DictConfig:
self.__model__.config = OmegaConf.to_container(self._config)

OmegaConf.clear_resolver("config_method")

self.on_after_configure()
except Exception as _ex:
raise ConfigurationError(str(_ex)) from _ex

Expand Down Expand Up @@ -661,6 +663,12 @@ def on_configure(self) -> None:
that are applied at a later stage.
"""

def on_after_configure(self) -> None:
"""Configuration event operating on the resolved, read-only configuration
This may be used to validate given the interface instance. If validation
can be performed without the instance, use a config schema instead.
"""

def _clear_caches(self) -> None:
self._config = None
self.__model__.config = None
Expand Down
16 changes: 16 additions & 0 deletions src/machinable/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,17 @@ def commit(self) -> Self:
if index.find_by_id(self.uuid) is not None:
return self

self.on_before_commit()

self.__model__.context = context = self.compute_context()
self.__model__.uuid = update_uuid_payload(self.__model__.uuid, context)

# ensure that configuration and predicate has been computed
assert self.config is not None
self.__model__.predicate = self.compute_predicate()

self.on_commit()

# commit to index
for k, v in self.__related__.items():
if v is None:
Expand Down Expand Up @@ -277,8 +281,20 @@ def commit(self) -> Self:
self.save_file(filepath, data)
self._deferred_data = {}

self.on_after_commit()

return self

def on_before_commit(self):
"""Event hook before interface is committed."""

def on_commit(self):
"""Event hook during interface commit when uuid, config, context
and predicate have been computed and commit is about to be performed"""

def on_after_commit(self):
"""Event hook after interface has been committed."""

@belongs_to
def project():
from machinable.project import Project
Expand Down

0 comments on commit 0bbf6f7

Please sign in to comment.