Skip to content

Commit

Permalink
services: added record extension registry
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandromumo committed Nov 13, 2024
1 parent d6ef02c commit 7936522
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
19 changes: 19 additions & 0 deletions invenio_rdm_records/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __init__(self, app=None):
def init_app(self, app):
"""Flask application initialization."""
self.init_config(app)
self.init_record_service_registry(app)
self.init_services(app)
self.init_resource(app)
app.extensions["invenio-rdm-records"] = self
Expand Down Expand Up @@ -310,6 +311,24 @@ def fix_datacite_configs(self, app):
if config_item in app.config:
app.config[config_item] = str(app.config[config_item])

def init_record_service_registry(self, app):
# TODO load entry points
self.record_service_registry = {}
self._register_entry_point(
self.record_service_registry,
"invenio_rdm_records.services.record_service_registry",
)
# Discussed w/Alex
# Interface could be the function itself that modifies the record class and returns it

def _register_entry_point(self, registry, ep_name):
"""Load entry points into the given registry."""
for ep in set(entry_points(group=ep_name)):
ext_name = ep.name
callback = ep.load()
assert callable(callback)
registry.setdefault(ext_name, callback)


def finalize_app(app):
"""Finalize app.
Expand Down
17 changes: 15 additions & 2 deletions invenio_rdm_records/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
StatusParam,
)
from .sort import VerifiedRecordsSortParam
from werkzeug.utils import cached_property


def is_draft_and_has_review(record, ctx):
Expand Down Expand Up @@ -396,8 +397,20 @@ def expand(self, obj, context):
class RDMRecordServiceConfig(RecordServiceConfig, ConfiguratorMixin):
"""RDM record draft service config."""

# Record and draft classes
record_cls = FromConfig("RDM_RECORD_CLS", default=RDMRecord)
@cached_property
def record_cls(self):
"""Record class."""
cfg_cls = self._app.config.get("RDM_RECORD_CLS", RDMRecord)
exts = getattr(
self._app.extensions["invenio-rdm-records"], "record_service_registry", {}
)
new_cls = cfg_cls
for name, _ext in exts.items():
tmp = _ext(self._app, new_cls)
assert type(tmp) == type(cfg_cls), f"Invalid record type. Expected: {type(cfg_cls)}, got: {type(tmp)}"
new_cls = tmp
return new_cls

draft_cls = FromConfig("RDM_DRAFT_CLS", default=RDMDraft)

# Schemas
Expand Down

0 comments on commit 7936522

Please sign in to comment.