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

Extend 3 #259

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion oarepo_model_builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .builders import OutputBuilder
from .fs import AbstractFileSystem, FileSystem
from .outputs import OutputBase
from .schema import ModelSchema
from .schema.schema import ModelSchema
from .utils.import_class import import_class


Expand Down
10 changes: 2 additions & 8 deletions oarepo_model_builder/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,12 @@
multiple=True,
)
@click.argument("model_filename", type=click.Path(exists=True), required=True)
@click.argument(
"included_models", nargs=-1, type=click.Path(exists=True), required=False
)
def run(
output_directory, # NOSONAR
package,
sets,
configs,
model_filename,
included_models,
verbosity,
isort,
black,
Expand All @@ -113,7 +109,6 @@ def run(
run_internal(
output_directory,
model_filename,
included_models,
package,
configs,
sets,
Expand Down Expand Up @@ -141,7 +136,6 @@ def run(
def run_internal(
output_directory, # NOSONAR
model_filename,
included_models,
package,
configs,
sets,
Expand Down Expand Up @@ -183,13 +177,14 @@ def run_internal(
0,
"\n\n%s\n\nProcessing model(s) %s into output directory %s",
datetime.datetime.now(),
[model_filename, *included_models],
model_filename,
output_directory,
)

includes = {
x.split("=", maxsplit=1)[0]: x.split("=", maxsplit=1)[1] for x in includes
}
includes = {k: str(Path(v).resolve()) for k, v in includes.items()}

# load model (and resolve includes) and optionally save it before the processing (for debugging)
model = load_model(
Expand All @@ -199,7 +194,6 @@ def run_internal(
isort,
autoflake,
sets,
merged_models=included_models,
extra_included=includes,
)
if save_model:
Expand Down
2 changes: 1 addition & 1 deletion oarepo_model_builder/datatypes/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def links(self):
imports=[Import("invenio_records_resources.services.RecordLink")],
),
Link(
name="self_html",
name="=self_html",
link_class="RecordLink",
link_args=[
f'"{{+ui}}{html_url_prefix}{{id}}"',
Expand Down
37 changes: 7 additions & 30 deletions oarepo_model_builder/entrypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import importlib_metadata

from oarepo_model_builder.builder import ModelBuilder
from oarepo_model_builder.schema import ModelSchema, remove_star_keys
from oarepo_model_builder.schema.schema import ModelSchema
from oarepo_model_builder.validation import InvalidModelException


Expand Down Expand Up @@ -57,7 +57,7 @@ def load_from_entry_point_internal(name, group_name, loaded_keys, loaded_entries


def load_model_from_entrypoint(ep: importlib_metadata.EntryPoint):
def load(schema):
def load(loader):
try:
loaded_schema = ep.load()
except: # NOSONAR intentionally broad
Expand All @@ -79,12 +79,10 @@ def load(schema):
full_fn = Path(file_name)
if not full_fn.exists():
continue
loaded_schema = schema._load(full_fn, content=full_fn.read_text())
break

return loader._load_raw_data_from_file(full_fn)
else:
raise InvalidModelException(f"Could not load entry point {ep}")
remove_star_keys(loaded_schema)
return loaded_schema

return load

Expand All @@ -105,7 +103,6 @@ def load_model(
sets=(),
model_content=None,
extra_included=None,
merged_models=None,
):
loaders = load_entry_points_dict("oarepo_model_builder.loaders")
included_models = load_included_models_from_entry_points()
Expand All @@ -116,29 +113,9 @@ def load_model(
content=model_content,
loaders=loaders,
included_models=included_models,
merged_models=merged_models,
reference_processors={
ModelSchema.REF_KEYWORD: load_entry_points_list(
"oarepo_model_builder.loaders.ref", None
),
ModelSchema.USE_KEYWORD: load_entry_points_list(
"oarepo_model_builder.loaders.use", None
),
ModelSchema.EXTEND_KEYWORD: load_entry_points_list(
"oarepo_model_builder.loaders.extend", None
),
},
post_reference_processors={
ModelSchema.REF_KEYWORD: load_entry_points_list(
"oarepo_model_builder.loaders.post.ref", None
),
ModelSchema.USE_KEYWORD: load_entry_points_list(
"oarepo_model_builder.loaders.post.use", None
),
ModelSchema.EXTEND_KEYWORD: load_entry_points_list(
"oarepo_model_builder.loaders.post.extend", None
),
},
schema_preprocessors=load_entry_points_list(
"oarepo_model_builder.schema_preprocessors", None
),
)
for config in configs:
load_config(schema, config, loaders)
Expand Down
3 changes: 2 additions & 1 deletion oarepo_model_builder/invenio/templates/marshmallow.py.jinja2
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from oarepo_runtime.services.schema import consistent_resolution
import marshmallow as ma
{{ imports|imports }}
{% for cls in generated_classes %}
Expand All @@ -9,7 +10,7 @@ import marshmallow as ma


{% for cls in generated_classes %}
class {{cls.class_name|base_name}}({% for bc in cls.base_classes %}{% if not loop.first%}, {% endif %}{{bc|base_name}}{% endfor %}):
class {{cls.class_name|base_name}}({% if cls.base_classes|length>1 %}consistent_resolution({% endif %}{% for bc in cls.base_classes %}{% if not loop.first%}, {% endif %}{{bc|base_name}}{% endfor %}{% if cls.base_classes|length>1 %}){% endif %}):
class Meta:
unknown = ma.{{ cls.unknown }}
{% for fld in cls.fields %}
Expand Down
33 changes: 8 additions & 25 deletions oarepo_model_builder/loaders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
try:
import json5
except ImportError:
import json as json5
from pathlib import Path

import json5
import yaml

def json_loader(
file_path, schema, content=None # NOSONAR schema kept for extensibility
):
if content:
return json5.loads(content)
with open(file_path) as f:
return json5.load(f)

def json_loader(path: Path):
return json5.loads(path.read_text(encoding="utf-8"))

def yaml_loader(
file_path, schema, content=None # NOSONAR schema kept for extensibility
):
try:
import yaml
except ImportError:
raise RuntimeError(
"Loader for yaml not found. Please install pyyaml library to use yaml files"
)

if content:
return yaml.full_load(content)

with open(file_path) as f:
return yaml.full_load(f)
def yaml_loader(path: Path):
with path.open(encoding="utf-8") as stream:
return yaml.safe_load(stream)
Loading
Loading