From 1e8e73145dd07d77b8ca42806e08ad261630da7d Mon Sep 17 00:00:00 2001 From: Mirek Simek Date: Fri, 2 Sep 2022 19:43:27 +0200 Subject: [PATCH] dumping model on request --- oarepo_model_builder/cli.py | 18 +++++++++++++++--- setup.cfg | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/oarepo_model_builder/cli.py b/oarepo_model_builder/cli.py index 361a9f37..71d9b730 100644 --- a/oarepo_model_builder/cli.py +++ b/oarepo_model_builder/cli.py @@ -6,6 +6,7 @@ from pathlib import Path import click +import yaml from oarepo_model_builder.conflict_resolvers import AutomaticResolver, InteractiveResolver from oarepo_model_builder.entrypoints import create_builder_from_entrypoints, load_model @@ -35,6 +36,11 @@ help="Increase the verbosity. This option can be used multiple times.", count=True, ) +@click.option( + "--save-model", + "save_model", + help="Save resolved model into this file.", +) @click.option( "--config", "configs", @@ -49,13 +55,15 @@ @click.option("--black/--skip-black", default=True, help="Call black on generated sources") @click.option("--resolve-conflicts", type=click.Choice(["replace", "keep", "comment", "debug"])) @click.argument("model_filename") -def run(output_directory, package, sets, configs, model_filename, verbosity, isort, black, resolve_conflicts): +def run(output_directory, package, sets, configs, model_filename, + verbosity, isort, black, resolve_conflicts, save_model): """ Compiles an oarepo model file given in MODEL_FILENAME into an Invenio repository model. """ try: run_internal( - output_directory, model_filename, package, configs, resolve_conflicts, sets, black, isort, verbosity + output_directory, model_filename, package, configs, resolve_conflicts, + sets, black, isort, verbosity, save_model ) except Exception as e: if verbosity >= 2: @@ -69,7 +77,8 @@ def run(output_directory, package, sets, configs, model_filename, verbosity, iso sys.exit(1) -def run_internal(output_directory, model_filename, package, configs, resolve_conflicts, sets, black, isort, verbosity): +def run_internal(output_directory, model_filename, package, configs, + resolve_conflicts, sets, black, isort, verbosity, save_model): # extend system's search path to add script's path in front (so that scripts called from the compiler are taken # from the correct virtual environ) os.environ["PATH"] = str(Path(sys.argv[0]).parent.absolute()) + os.pathsep + os.environ.get("PATH", "") @@ -91,6 +100,9 @@ def run_internal(output_directory, model_filename, package, configs, resolve_con output_directory, ) model = load_model(model_filename, package, configs, black, isort, sets) + if save_model: + with open(save_model, 'w') as f: + yaml.dump(model.schema, f) model.schema["output-directory"] = output_directory if not resolve_conflicts or resolve_conflicts == "debug": resolver = InteractiveResolver(resolve_conflicts == "debug") diff --git a/setup.cfg b/setup.cfg index 5bd66ecb..e93dd02d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = oarepo-model-builder -version = 1.0.0.dev15 +version = 1.0.0.dev16 description = An utility library that generates OARepo required data model files from a JSON specification file authors = Miroslav Bauer , Miroslav Simek readme = README.md