|
| 1 | +import click |
| 2 | + |
| 3 | +from paths_cli.compiling.root_compiler import do_compile, register_plugins |
| 4 | +from paths_cli.parameters import OUTPUT_FILE |
| 5 | +from paths_cli.errors import MissingIntegrationError |
| 6 | +from paths_cli import OPSCommandPlugin |
| 7 | +from paths_cli.compiling.plugins import ( |
| 8 | + CategoryPlugin, InstanceCompilerPlugin |
| 9 | +) |
| 10 | +from paths_cli.plugin_management import NamespacePluginLoader |
| 11 | +import importlib |
| 12 | +from paths_cli.utils import get_installed_plugins |
| 13 | +from paths_cli.commands.contents import report_all_tables |
| 14 | + |
| 15 | +# this is just to handle a nicer error |
| 16 | +def import_module(module_name, format_type=None, install=None): |
| 17 | + try: |
| 18 | + mod = importlib.import_module(module_name) |
| 19 | + except ImportError: |
| 20 | + if format_type is None: |
| 21 | + format_type = module_name |
| 22 | + |
| 23 | + msg = f"Unable to find a compiler for {format_type} on your system." |
| 24 | + if install is not None: |
| 25 | + msg += f" Please install {install} to use this format." |
| 26 | + |
| 27 | + raise MissingIntegrationError(msg) |
| 28 | + return mod |
| 29 | + |
| 30 | +def load_yaml(f): |
| 31 | + yaml = import_module('yaml', format_type="YAML", install="PyYAML") |
| 32 | + return yaml.load(f.read(), Loader=yaml.FullLoader) |
| 33 | + |
| 34 | +def load_json(f): |
| 35 | + json = import_module('json') # this should never fail... std lib! |
| 36 | + return json.loads(f.read()) |
| 37 | + |
| 38 | +# This is why we can't use TOML: |
| 39 | +# https://github.com/toml-lang/toml/issues/553#issuecomment-444814690 |
| 40 | +# Conflicts with rules preventing mixed types in arrays. This seems to have |
| 41 | +# relaxed in TOML 1.0, but the toml package doesn't support the 1.0 |
| 42 | +# standard. We'll add toml in once the package supports the standard. |
| 43 | + |
| 44 | +EXTENSIONS = { |
| 45 | + 'yaml': load_yaml, |
| 46 | + 'yml': load_yaml, |
| 47 | + 'json': load_json, |
| 48 | + 'jsn': load_json, |
| 49 | + # 'toml': load_toml, |
| 50 | +} |
| 51 | + |
| 52 | +def select_loader(filename): |
| 53 | + ext = filename.split('.')[-1] |
| 54 | + try: |
| 55 | + return EXTENSIONS[ext] |
| 56 | + except KeyError: |
| 57 | + raise RuntimeError(f"Unknown file extension: {ext}") |
| 58 | + |
| 59 | +@click.command( |
| 60 | + 'compile', |
| 61 | +) |
| 62 | +@click.argument('input_file') |
| 63 | +@OUTPUT_FILE.clicked(required=True) |
| 64 | +def compile_(input_file, output_file): |
| 65 | + loader = select_loader(input_file) |
| 66 | + with open(input_file, mode='r') as f: |
| 67 | + dct = loader(f) |
| 68 | + |
| 69 | + plugin_types = (InstanceCompilerPlugin, CategoryPlugin) |
| 70 | + plugins = get_installed_plugins( |
| 71 | + default_loader=NamespacePluginLoader('paths_cli.compiling', |
| 72 | + plugin_types), |
| 73 | + plugin_types=plugin_types |
| 74 | + ) |
| 75 | + register_plugins(plugins) |
| 76 | + |
| 77 | + objs = do_compile(dct) |
| 78 | + print(f"Saving {len(objs)} user-specified objects to {output_file}....") |
| 79 | + storage = OUTPUT_FILE.get(output_file) |
| 80 | + storage.save(objs) |
| 81 | + report_all_tables(storage) |
| 82 | + |
| 83 | + |
| 84 | +PLUGIN = OPSCommandPlugin( |
| 85 | + command=compile_, |
| 86 | + section="Debug", |
| 87 | + requires_ops=(1, 0), |
| 88 | + requires_cli=(0, 3) |
| 89 | +) |
0 commit comments