|
20 | 20 | def register_class(target_class: Registry): |
21 | 21 | """Add a class to the registry""" |
22 | 22 | if hasattr(target_class, "schema_version"): |
23 | | - assert "openminds" in target_class.__module__ |
24 | | - parts = target_class.__module__.split(".") |
25 | 23 | 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'") |
32 | 36 |
|
33 | 37 | registry["names"][name] = target_class |
34 | 38 |
|
|
0 commit comments