Skip to content

Commit dd6704c

Browse files
Merge pull request openMetadataInitiative#70 from apdavison/reusable-registry
Make the class registry reusable by other packages
2 parents 7f2bbc6 + 483394c commit dd6704c

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

pipeline/src/registry.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@
2020
def register_class(target_class: Registry):
2121
"""Add a class to the registry"""
2222
if hasattr(target_class, "schema_version"):
23-
assert "openminds" in target_class.__module__
24-
parts = target_class.__module__.split(".")
2523
version = target_class.schema_version.split(".")[0] # e.g. 'v3' or 'latest'
26-
name = ".".join(parts[0:3] + [target_class.__name__]) # e.g. openminds.latest.core.Dataset
27-
# taking the first 3 parts is artbitrary, should add an attribute to each class
28-
# with its preferred import name
29-
# e.g. for `openminds.latest.core.research.protocol_execution.ProtocolExecution`
30-
# the preferred import name is `openminds.latest.core.ProtocolExecution`
31-
# because the intermediate directory structure is an implementation detail
24+
if target_class.__module__.startswith("openminds"):
25+
parts = target_class.__module__.split(".")
26+
name = ".".join(parts[0:3] + [target_class.__name__]) # e.g. openminds.latest.core.Dataset
27+
# taking the first 3 parts is arbitrary, we could instead set the
28+
# attribute "preferred_import_path" on each class, with its preferred import name.
29+
# e.g. for `openminds.latest.core.research.protocol_execution.ProtocolExecution`
30+
# the preferred import name is `openminds.latest.core.ProtocolExecution`
31+
# because the intermediate directory structure is an implementation detail
32+
elif hasattr(target_class, "preferred_import_path"): # e.g., classes used for testing
33+
name = target_class.preferred_import_path
34+
else:
35+
raise AttributeError("Cannot register this class, it does not define 'preferred_import_path'")
3236

3337
registry["names"][name] = target_class
3438

0 commit comments

Comments
 (0)