Skip to content

Commit

Permalink
Extensible lists & dicts in configuration (#264)
Browse files Browse the repository at this point in the history
* Extensible lists & dicts in configuration

* Tests

* black

* github actions

* default oarepo version
  • Loading branch information
mesemus authored Jun 13, 2024
1 parent 0eb82ff commit 707ac44
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
oarepo:
description: OARepo version (11, 12, ...)
required: true
default: 11
default: 12
type: string

env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/manual.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
oarepo:
description: OARepo version (11, 12, ...)
required: true
default: 11
default: 12

jobs:
build:
Expand Down
7 changes: 1 addition & 6 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ permissions:
contents: read

jobs:
build11:
uses: ./.github/workflows/build.yaml
with:
oarepo: 11

build12:
uses: ./.github/workflows/build.yaml
with:
oarepo: 12

publish:
runs-on: ubuntu-latest
needs: build11
needs: build12
steps:
- name: Use built artifacts
uses: actions/download-artifact@v3
Expand Down
4 changes: 2 additions & 2 deletions oarepo_model_builder/datatypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
from .strings import ( # noqa
FulltextDataType,
FulltextKeywordDataType,
HtmlDataType,
KeywordDataType,
URLDataType,
UUIDDataType,
HtmlDataType
)

DEFAULT_DATATYPES = [
Expand All @@ -76,5 +76,5 @@
ArrayDataType,
URLDataType,
ModelDataType,
HtmlDataType
HtmlDataType,
]
1 change: 1 addition & 0 deletions oarepo_model_builder/datatypes/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class URLDataType(StringDataType):
"facet-class": "invenio_records_resources.services.records.facets.TermsFacet",
}


class HtmlDataType(FulltextDataType):
model_type = "html"
marshmallow = {
Expand Down
7 changes: 5 additions & 2 deletions oarepo_model_builder/invenio/invenio_record_search_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
return facet_groups, default_group, sort_options
5 changes: 3 additions & 2 deletions oarepo_model_builder/invenio/invenio_script_sample_data.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,7 +46,7 @@ def random_html(self):
i = 0
while i < 4:
tag = random.choice(["<p>", "<div>", "<span>"])
content = ''.join(Faker().sentence())
content = "".join(Faker().sentence())
html += f"{tag}{content}{tag[0]}/{tag[1:]}"
i = i + 1
return html
Expand Down
7 changes: 6 additions & 1 deletion oarepo_model_builder/invenio/templates/ext.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 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.84
version = 4.0.85
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
3 changes: 1 addition & 2 deletions tests/test_builder_from_entrypoints.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import os
import random
import sys
from pathlib import Path

Expand All @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion tests/test_simple_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 707ac44

Please sign in to comment.