Skip to content

Commit

Permalink
Merge pull request #194 from oarepo/ext-service-hardcode
Browse files Browse the repository at this point in the history
ext refactor
  • Loading branch information
SilvyPuzzlewell authored Jun 27, 2023
2 parents 2f73692 + 4a78b57 commit a529624
Show file tree
Hide file tree
Showing 23 changed files with 205 additions and 139 deletions.
2 changes: 1 addition & 1 deletion oarepo_model_builder/builders/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def generate_model(self, node):
generated = self.generate(node)
generated.pop("enabled", None)
generated.pop("type", None)
node.section_global_mapping.config.pop('properties', None)
node.section_global_mapping.config.pop("properties", None)

return {**node.section_global_mapping.config, "mappings": generated}

Expand Down
2 changes: 2 additions & 0 deletions oarepo_model_builder/datatypes/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
AppModelComponent,
BlueprintsModelComponent,
DefaultsModelComponent,
ExtResourceModelComponent,
FacetsModelComponent,
JSONSchemaModelComponent,
MappingModelComponent,
Expand Down Expand Up @@ -72,4 +73,5 @@
NestedFacetsComponent,
RegularFacetsComponent,
ArrayFacetsComponent,
ExtResourceModelComponent,
]
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def update(self, facet_section):
# searchable not set up, so take it from argument
self.searchable = facet_section.get("searchable", None)

def set_field(self, facet_section, arguments, field_class = None):
def set_field(self, facet_section, arguments, field_class=None):
field = facet_section.get("field")
if field:
self.field = field
Expand Down
2 changes: 2 additions & 0 deletions oarepo_model_builder/datatypes/components/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .app import AppModelComponent
from .blueprints import BlueprintsModelComponent
from .defaults import DefaultsModelComponent
from .ext_resource import ExtResourceModelComponent
from .facets import FacetsModelComponent
from .jsonschema import JSONSchemaModelComponent
from .mapping import MappingModelComponent
Expand Down Expand Up @@ -40,4 +41,5 @@
"UIModelComponent",
"DefaultsModelComponent",
"ProxyModelComponent",
"ExtResourceModelComponent",
]
7 changes: 3 additions & 4 deletions oarepo_model_builder/datatypes/components/model/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ class ModelSchema(ma.Schema):
)

def before_model_prepare(self, datatype, **kwargs):
prefix = datatype.definition["module"]["prefix"]
alias = datatype.definition["module"]["alias"]
module = datatype.definition["module"]["qualified"]
base_title = datatype.definition["module"]["base-title"]

config = set_default(datatype, "config", {})

Expand All @@ -81,9 +80,9 @@ def before_model_prepare(self, datatype, **kwargs):

ext.setdefault("generate", True)
ext_module = ext.setdefault("module", f"{module}.ext")
ext.setdefault("class", f"{ext_module}.{prefix}Ext")
ext.setdefault("class", f"{ext_module}.{base_title}Ext")
ext.setdefault("base-classes", [])
ext.setdefault("extra_code", "")
ext.setdefault("alias", alias)
ext.setdefault("alias", module)
ext.setdefault("imports", [])
convert_config_to_qualified_name(ext)
10 changes: 9 additions & 1 deletion oarepo_model_builder/datatypes/components/model/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ class Meta:
data_key="base-upper",
metadata={"doc": "Uppercase of the base name"},
)

base_title = ma.fields.String(
attribute="base-title",
data_key="base-title",
metadata={"doc": "Capitalized base name"},
)
kebab_module = ma.fields.String(
attribute="kebab-module",
data_key="kebab-module",
Expand Down Expand Up @@ -95,6 +99,10 @@ def get_model_name(_):
"base-upper",
module_base.upper(),
)
module_container.setdefault(
"base-title",
module_base.capitalize(),
)
module_container.setdefault(
"kebab-module",
module.replace(".", "-").replace("_", "-"),
Expand Down
38 changes: 38 additions & 0 deletions oarepo_model_builder/datatypes/components/model/ext_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import marshmallow as ma

from oarepo_model_builder.datatypes import DataTypeComponent, ModelDataType
from oarepo_model_builder.datatypes.components.model.utils import set_default


class ExtResourceSchema(ma.Schema):
generate = ma.fields.Bool()
skip = ma.fields.Bool()

class Meta:
unknown = ma.RAISE


class ExtResourceModelComponent(DataTypeComponent):
eligible_datatypes = [ModelDataType]

class ModelSchema(ma.Schema):
ext_resource = ma.fields.Nested(
ExtResourceSchema,
attribute="ext-resource",
data_key="ext-resource",
)

def process_ext_resource(self, datatype, section, **kwargs):
if self.is_record_profile:
cfg = section.config
cfg["ext-service-name"] = "service_records"
cfg["ext-resource-name"] = "resource_records"

def before_model_prepare(self, datatype, *, context, **kwargs):
self.is_record_profile = context["profile"] == "record"
if not self.is_record_profile:
return
ext = set_default(datatype, "ext-resource", {})

ext.setdefault("generate", True)
ext.setdefault("skip", False)
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def marshmallow_register_class_names(self, *, datatype, classes, **kwargs):
marshmallow_def = dict_get(datatype.definition, self.model_marshmallow_section)
classes[marshmallow_def["class"]].append((True, datatype))

def before_model_prepare(self, datatype, **kwargs):
def before_model_prepare(self, datatype, *, context, **kwargs):
prefix = datatype.definition["module"]["prefix"]
services_module = parent_module(datatype.definition["service"]["module"])

Expand Down
3 changes: 1 addition & 2 deletions oarepo_model_builder/datatypes/components/model/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ def before_model_prepare(self, datatype, *, context, **kwargs):
module = datatype.definition["module"]["qualified"]
module_base_upper = datatype.definition["module"]["base-upper"]
record_prefix = datatype.definition["module"]["prefix"]
flask_extension_name = datatype.definition["ext"]["alias"]

service_package = f"{module}.services.{profile_module}"

Expand All @@ -124,7 +123,7 @@ def before_model_prepare(self, datatype, *, context, **kwargs):
f"{config_module}.{record_prefix}ServiceConfig",
)
config.setdefault("extra-code", "")
config.setdefault("service-id", flask_extension_name)
config.setdefault("service-id", datatype.definition["module"]["suffix-snake"])
config.setdefault(
"base-classes",
["PermissionsPresetsConfigMixin", "InvenioRecordServiceConfig"],
Expand Down
1 change: 1 addition & 0 deletions oarepo_model_builder/invenio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
"pid-provider": "templates/pid_provider.py.jinja2",
# utils and included
"imports": "templates/imports.py.jinja2",
"ext-resource": "templates/ext_resource.py.jinja2",
}
4 changes: 4 additions & 0 deletions oarepo_model_builder/invenio/invenio_api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ class InvenioAPIViewsBuilder(InvenioBaseClassPythonBuilder):
TYPE = "invenio_api_views"
section = "api-blueprint"
template = "api-views"

def finish(self, **extra_kwargs):
ext = self.current_model.section_ext_resource.config
super().finish(ext=ext, **extra_kwargs)
4 changes: 4 additions & 0 deletions oarepo_model_builder/invenio/invenio_app_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ class InvenioAPPViewsBuilder(InvenioBaseClassPythonBuilder):
TYPE = "invenio_app_views"
section = "app-blueprint"
template = "app-views"

def finish(self, **extra_kwargs):
ext = self.current_model.section_ext_resource.config
super().finish(ext=ext, **extra_kwargs)
11 changes: 11 additions & 0 deletions oarepo_model_builder/invenio/invenio_ext_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .invenio_base import InvenioBaseClassPythonBuilder


class InvenioExtResourceBuilder(InvenioBaseClassPythonBuilder):
TYPE = "invenio_ext_resource"
section = "ext"
template = "ext-resource"

def finish(self, **extra_kwargs):
ext = self.current_model.section_ext_resource.config
super().finish(ext=ext, **extra_kwargs)
4 changes: 4 additions & 0 deletions oarepo_model_builder/invenio/invenio_proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ class InvenioProxiesBuilder(InvenioBaseClassPythonBuilder):
TYPE = "invenio_proxies"
section = "proxy"
template = "proxies"

def finish(self, **extra_kwargs):
ext = self.current_model.section_ext_resource.config
super().finish(ext=ext, **extra_kwargs)
8 changes: 4 additions & 4 deletions oarepo_model_builder/invenio/templates/api_views.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from flask import Blueprint

def {{ vars.api_blueprint.function|base_name }}(app):
"""Create {{ vars.record.class|base_name }} blueprint."""
blueprint = app.extensions["{{ vars.ext.alias }}"].resource.as_blueprint()
blueprint = app.extensions["{{ vars.ext.alias }}"].{{ ext.ext_resource_name }}.as_blueprint()
blueprint.record_once(init_{{ vars.api_blueprint.function|base_name }})

#calls record_once for all other functions starting with "init_addons_"
Expand All @@ -22,14 +22,14 @@ def init_{{ vars.api_blueprint.function|base_name }}(state):
{% if not vars.service_config.skip %}
# register service
sregistry = app.extensions["invenio-records-resources"].registry
sregistry.register(ext.service, service_id="{{ vars.service_config.service_id }}")
sregistry.register(ext.{{ ext.ext_service_name }}, service_id=ext.{{ ext.ext_service_name }}.config.service_id)
{% endif %}

{% if not vars.mapping_settings.skip %}
# Register indexer
if hasattr(ext.service, "indexer"):
if hasattr(ext.{{ ext.ext_service_name }}, "indexer"):
iregistry = app.extensions["invenio-indexer"].registry
iregistry.register(ext.service.indexer, indexer_id="{{ vars.ext.alias }}")
iregistry.register(ext.{{ ext.ext_service_name }}.indexer, indexer_id=ext.{{ ext.ext_service_name }}.config.service_id)
{% endif %}

{{ vars.api_blueprint.extra_code }}
6 changes: 3 additions & 3 deletions oarepo_model_builder/invenio/templates/app_views.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ def init_{{ vars.app_blueprint.function|base_name }}(state):
{% if not vars.service_config.skip %}
# register service
sregistry = app.extensions["invenio-records-resources"].registry
sregistry.register(ext.service, service_id="{{ vars.service_config.service_id }}")
sregistry.register(ext.{{ ext.ext_service_name }}, service_id=ext.{{ ext.ext_service_name }}.config.service_id)
{% endif %}

{% if not vars.mapping_settings.skip %}
# Register indexer
if hasattr(ext.service, "indexer"):
if hasattr(ext.{{ ext.ext_service_name }}, "indexer"):
iregistry = app.extensions["invenio-indexer"].registry
iregistry.register(ext.service.indexer, indexer_id="{{ vars.ext.alias }}")
iregistry.register(ext.{{ ext.ext_service_name }}.indexer, indexer_id=ext.{{ ext.ext_service_name }}.config.service_id)
{% endif %}

{{ vars.app_blueprint.extra_code }}
22 changes: 1 addition & 21 deletions oarepo_model_builder/invenio/templates/ext.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import re
class {{ vars.ext|class_header }}:

def __init__(self, app=None):
"""Extension initialization."""
{% if not vars.resource.skip %}self.resource = None{% endif %}
{% if not vars.service.skip %}self.service = None{% endif %}
{% if vars.ext.base_classes %}
super().__init__(app=None)
{% endif %}
Expand All @@ -21,7 +18,6 @@ class {{ vars.ext|class_header }}:
{% endif %}
self.init_config(app)
if not self.is_inherited():
{% if not vars.resource.skip or not vars.service.skip %}self.init_resource(app){% endif %}
self.register_flask_extension(app)

def register_flask_extension(self, app):
Expand All @@ -30,29 +26,13 @@ class {{ vars.ext|class_header }}:
{% endif %}
app.extensions["{{ vars.ext.alias }}"] = self

def init_resource(self, app):
"""Initialize vocabulary resources."""
{% if not vars.service.skip %}
self.service = app.config["{{ vars.service.config_key }}"](
config=app.config["{{ vars.service_config.config_key }}"](),
{% if vars.service.additional_args %}
{{ vars.service.additional_args|generate_list }}
{% endif %}
)
{% endif %}
{% if not vars.resource.skip %}
self.resource = app.config["{{ vars.resource.config_key }}"](
{% if not vars.service.skip %}service=self.service,{% endif %}
config=app.config["{{ vars.resource_config.config_key }}"](),
)
{% endif %}

def init_config(self, app):
"""Initialize configuration."""
for identifier in dir(config):
if re.match('^[A-Z_0-9]*$', identifier) and not identifier.startswith('_'):
app.config.setdefault(identifier, getattr(config, identifier))


def is_inherited(self):
from importlib_metadata import entry_points

Expand Down
23 changes: 23 additions & 0 deletions oarepo_model_builder/invenio/templates/ext_resource.py.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from functools import cached_property
{{ vars.config.module|generate_import(alias='config') }}

class {{ vars.ext|class_header }}:
{% if not vars.service.skip %}
@cached_property
def {{ ext.ext_service_name }}(self):
return config.{{ vars.service.config_key }}(
config=config.{{ vars.service_config.config_key }}(),
{% if vars.service.additional_args %}
{{ vars.service.additional_args|generate_list }}
{% endif %}
)
{% endif %}

{% if not vars.resource.skip %}
@cached_property
def {{ ext.ext_resource_name }}(self):
return config.{{ vars.resource.config_key }}(
service=self.{{ ext.ext_service_name }},
config=config.{{ vars.resource_config.config_key }}(),
)
{% endif %}
4 changes: 2 additions & 2 deletions oarepo_model_builder/invenio/templates/proxies.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def _ext_proxy(attr):
lambda: getattr(current_app.extensions["{{ vars.ext.alias }}"], attr))

{% if not vars.service.skip %}
{{ vars.service.proxy }} = _ext_proxy('service')
{{ vars.service.proxy }} = _ext_proxy('{{ ext.ext_service_name }}')
"""Proxy to the instantiated vocabulary service."""
{% endif %}

{% if not vars.resource.skip %}
{{ vars.resource.proxy }} = _ext_proxy('resource')
{{ vars.resource.proxy }} = _ext_proxy('{{ ext.ext_resource_name }}')
"""Proxy to the instantiated vocabulary resource."""
{% endif %}
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = oarepo-model-builder
version = 4.0.19
version = 4.0.20
description = A utility library that generates OARepo required data model files from a JSON specification file
authors = Miroslav Bauer <[email protected]>, Miroslav Simek <[email protected]>
readme = README.md
Expand Down Expand Up @@ -148,6 +148,7 @@ oarepo_model_builder.builders.record =
0430-ui_serializer = oarepo_model_builder.invenio.invenio_record_ui_serializer:InvenioRecordUISerializerBuilder
0500-invenio_config = oarepo_model_builder.invenio.invenio_config:InvenioConfigBuilder
0600-invenio_ext = oarepo_model_builder.invenio.invenio_ext:InvenioExtBuilder
0605-invenio_ext_resource = oarepo_model_builder.invenio.invenio_ext_resource:InvenioExtResourceBuilder
0610-invenio_ext_setup_cfg = oarepo_model_builder.invenio.invenio_ext_setup_cfg:InvenioExtSetupCfgBuilder
0700-invenio_ext = oarepo_model_builder.invenio.invenio_proxies:InvenioProxiesBuilder
0910-invenio_record_metadata_alembic_setup_cfg = oarepo_model_builder.invenio.invenio_record_metadata_alembic_setup_cfg:InvenioRecordMetadataAlembicSetupCfgBuilder
Expand Down
Loading

0 comments on commit a529624

Please sign in to comment.