From 707ac44592e88d8ffbdca3e357c34c8916379ec9 Mon Sep 17 00:00:00 2001 From: Mirek Simek Date: Thu, 13 Jun 2024 14:24:13 +0200 Subject: [PATCH] Extensible lists & dicts in configuration (#264) * Extensible lists & dicts in configuration * Tests * black * github actions * default oarepo version --- .github/workflows/build.yaml | 2 +- .github/workflows/manual.yaml | 2 +- .github/workflows/push.yaml | 7 +------ oarepo_model_builder/datatypes/__init__.py | 4 ++-- oarepo_model_builder/datatypes/strings.py | 1 + .../invenio/invenio_record_search_options.py | 7 +++++-- oarepo_model_builder/invenio/invenio_script_sample_data.py | 5 +++-- oarepo_model_builder/invenio/templates/ext.py.jinja2 | 7 ++++++- run-tests.sh | 2 +- setup.cfg | 2 +- tests/test_builder_from_entrypoints.py | 3 +-- tests/test_simple_builders.py | 7 ++++++- 12 files changed, 29 insertions(+), 20 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 5b5190f1..720256aa 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -6,7 +6,7 @@ on: oarepo: description: OARepo version (11, 12, ...) required: true - default: 11 + default: 12 type: string env: diff --git a/.github/workflows/manual.yaml b/.github/workflows/manual.yaml index e05df2cf..841c1586 100644 --- a/.github/workflows/manual.yaml +++ b/.github/workflows/manual.yaml @@ -6,7 +6,7 @@ on: oarepo: description: OARepo version (11, 12, ...) required: true - default: 11 + default: 12 jobs: build: diff --git a/.github/workflows/push.yaml b/.github/workflows/push.yaml index 54317498..cb598600 100644 --- a/.github/workflows/push.yaml +++ b/.github/workflows/push.yaml @@ -7,11 +7,6 @@ permissions: contents: read jobs: - build11: - uses: ./.github/workflows/build.yaml - with: - oarepo: 11 - build12: uses: ./.github/workflows/build.yaml with: @@ -19,7 +14,7 @@ jobs: publish: runs-on: ubuntu-latest - needs: build11 + needs: build12 steps: - name: Use built artifacts uses: actions/download-artifact@v3 diff --git a/oarepo_model_builder/datatypes/__init__.py b/oarepo_model_builder/datatypes/__init__.py index 9b907075..e9a74ebc 100644 --- a/oarepo_model_builder/datatypes/__init__.py +++ b/oarepo_model_builder/datatypes/__init__.py @@ -48,10 +48,10 @@ from .strings import ( # noqa FulltextDataType, FulltextKeywordDataType, + HtmlDataType, KeywordDataType, URLDataType, UUIDDataType, - HtmlDataType ) DEFAULT_DATATYPES = [ @@ -76,5 +76,5 @@ ArrayDataType, URLDataType, ModelDataType, - HtmlDataType + HtmlDataType, ] diff --git a/oarepo_model_builder/datatypes/strings.py b/oarepo_model_builder/datatypes/strings.py index 37510e23..158b8fce 100644 --- a/oarepo_model_builder/datatypes/strings.py +++ b/oarepo_model_builder/datatypes/strings.py @@ -68,6 +68,7 @@ class URLDataType(StringDataType): "facet-class": "invenio_records_resources.services.records.facets.TermsFacet", } + class HtmlDataType(FulltextDataType): model_type = "html" marshmallow = { diff --git a/oarepo_model_builder/invenio/invenio_record_search_options.py b/oarepo_model_builder/invenio/invenio_record_search_options.py index 08265d0a..c6f57164 100644 --- a/oarepo_model_builder/invenio/invenio_record_search_options.py +++ b/oarepo_model_builder/invenio/invenio_record_search_options.py @@ -13,13 +13,16 @@ class InvenioRecordSearchOptionsBuilder(InvenioBaseClassPythonBuilder): def finish(self, **extra_kwargs): facets: List[FacetDefinition] = get_distinct_facets(self.current_model) - facet_groups, default_group, sort_options = facet_data(facets, self.current_model) + facet_groups, default_group, sort_options = facet_data( + facets, self.current_model + ) extra_kwargs["facet_groups"] = facet_groups extra_kwargs["default_group"] = default_group extra_kwargs["sort_definition"] = sort_options super().finish(**extra_kwargs) + def facet_data(facets, current_model): facet_groups = {} default_group = [] @@ -77,4 +80,4 @@ def facet_data(facets, current_model): sort_options = current_model.definition["sortable"] else: sort_options = {} - return facet_groups, default_group, sort_options \ No newline at end of file + return facet_groups, default_group, sort_options diff --git a/oarepo_model_builder/invenio/invenio_script_sample_data.py b/oarepo_model_builder/invenio/invenio_script_sample_data.py index 36c14e9a..cb9fbc43 100644 --- a/oarepo_model_builder/invenio/invenio_script_sample_data.py +++ b/oarepo_model_builder/invenio/invenio_script_sample_data.py @@ -1,6 +1,7 @@ import json -from typing import Callable import random +from typing import Callable + import faker import faker.providers from faker import Faker @@ -45,7 +46,7 @@ def random_html(self): i = 0 while i < 4: tag = random.choice(["

", "

", ""]) - content = ''.join(Faker().sentence()) + content = "".join(Faker().sentence()) html += f"{tag}{content}{tag[0]}/{tag[1:]}" i = i + 1 return html diff --git a/oarepo_model_builder/invenio/templates/ext.py.jinja2 b/oarepo_model_builder/invenio/templates/ext.py.jinja2 index bd2c905b..36db7ec8 100644 --- a/oarepo_model_builder/invenio/templates/ext.py.jinja2 +++ b/oarepo_model_builder/invenio/templates/ext.py.jinja2 @@ -30,7 +30,12 @@ class {{ vars.ext|class_header }}: """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)) + if isinstance(app.config.get(identifier), list): + app.config[identifier] += getattr(config, identifier) + elif isinstance(app.config.get(identifier), dict): + app.config[identifier].update(getattr(config, identifier)) + else: + app.config.setdefault(identifier, getattr(config, identifier)) def is_inherited(self): diff --git a/run-tests.sh b/run-tests.sh index 06a8cd58..e27eb707 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -10,7 +10,7 @@ export BUILDER_VENV=.venv export TEST_VENV=.venv-tests export SERVER_VENV=.venv-server -OAREPO_VERSION=${OAREPO_VERSION:-11} +OAREPO_VERSION=${OAREPO_VERSION:-12} initialize_server_venv() { diff --git a/setup.cfg b/setup.cfg index 71bf7f1f..820e377b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = oarepo-model-builder -version = 4.0.84 +version = 4.0.85 description = A utility library that generates OARepo required data model files from a JSON specification file authors = Miroslav Bauer , Miroslav Simek readme = README.md diff --git a/tests/test_builder_from_entrypoints.py b/tests/test_builder_from_entrypoints.py index b63f8be0..e48a1485 100644 --- a/tests/test_builder_from_entrypoints.py +++ b/tests/test_builder_from_entrypoints.py @@ -1,6 +1,5 @@ import json import os -import random import sys from pathlib import Path @@ -9,10 +8,10 @@ from tests.utils import assert_python_equals from .utils import strip_whitespaces -import yaml OAREPO_USE = "use" + # def test_diff(): # with open("complex-model/data/sample_data.yaml") as f: # sample_data = list(yaml.safe_load_all(f)) diff --git a/tests/test_simple_builders.py b/tests/test_simple_builders.py index a8272019..7be9a903 100644 --- a/tests/test_simple_builders.py +++ b/tests/test_simple_builders.py @@ -206,7 +206,12 @@ 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)) + if isinstance(app.config.get(identifier), list): + app.config[identifier]+= getattr(config, identifier) + elif isinstance(app.config.get(identifier), dict): + app.config[identifier].update(getattr(config, identifier)) + else: + app.config.setdefault(identifier, getattr(config, identifier)) def is_inherited(self):