Skip to content
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

services: added record extension registry #1781

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions invenio_rdm_records/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from flask import Blueprint
from flask_iiif import IIIF
from flask_principal import identity_loaded
from importlib_metadata import entry_points
from invenio_records_resources.resources.files import FileResource

from . import config
Expand Down Expand Up @@ -102,6 +103,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 +312,22 @@ 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):
"""Initialize record service registry."""
self.record_service_registry = {}
self._register_entry_point(
self.record_service_registry,
"invenio_rdm_records.services.record_service_registry",
)

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
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-

Check failure on line 1 in invenio_rdm_records/services/config.py

View workflow job for this annotation

GitHub Actions / Python / Tests (3.9, postgresql14, opensearch2)

isort-check from invenio_requests.services.requests.config import RequestSearchOptions from requests import Request from werkzeug.local import LocalProxy +from werkzeug.utils import cached_property from invenio_rdm_records.records.processors.tiles import TilesProcessor StatusParam, ) from .sort import VerifiedRecordsSortParam -from werkzeug.utils import cached_property def is_draft_and_has_review(record, ctx):

Check failure on line 1 in invenio_rdm_records/services/config.py

View workflow job for this annotation

GitHub Actions / Python / Tests (3.9, postgresql14, opensearch2)

Black format check --- /home/runner/work/invenio-rdm-records/invenio-rdm-records/invenio_rdm_records/services/config.py 2024-11-13 16:20:05.783870+00:00 +++ /home/runner/work/invenio-rdm-records/invenio-rdm-records/invenio_rdm_records/services/config.py 2024-11-13 16:44:12.534607+00:00 @@ -405,11 +405,13 @@ 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)}" + 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)

Check failure on line 1 in invenio_rdm_records/services/config.py

View workflow job for this annotation

GitHub Actions / Python / Tests (3.12, postgresql14, opensearch2)

isort-check from invenio_requests.services.requests.config import RequestSearchOptions from requests import Request from werkzeug.local import LocalProxy +from werkzeug.utils import cached_property from invenio_rdm_records.records.processors.tiles import TilesProcessor StatusParam, ) from .sort import VerifiedRecordsSortParam -from werkzeug.utils import cached_property def is_draft_and_has_review(record, ctx):

Check failure on line 1 in invenio_rdm_records/services/config.py

View workflow job for this annotation

GitHub Actions / Python / Tests (3.12, postgresql14, opensearch2)

Black format check --- /home/runner/work/invenio-rdm-records/invenio-rdm-records/invenio_rdm_records/services/config.py 2024-11-13 16:20:04.953677+00:00 +++ /home/runner/work/invenio-rdm-records/invenio-rdm-records/invenio_rdm_records/services/config.py 2024-11-13 16:46:25.174477+00:00 @@ -405,11 +405,13 @@ 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)}" + 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)
#
# Copyright (C) 2020-2024 CERN.
# Copyright (C) 2020-2021 Northwestern University.
Expand Down 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 @@
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)}"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the place where the interface is being enforced

new_cls = tmp
return new_cls

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

# Schemas
Expand Down
Loading