From c9437e244d362792b17c7b588863b971720cd699 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Thu, 5 Jan 2023 09:33:15 -0300 Subject: [PATCH] [Import] Simplified the templates import - Now we detect if the name is internal - A mechanism to skip the detection was added, just in case --- README.md | 17 +++++-- docs/README.in | 17 +++++-- kibot/config_reader.py | 47 ++++++++++++------- .../JLCPCB_stencil.kibot.yaml | 1 - .../import_test_internal_1.kibot.yaml | 1 - .../import_test_internal_jlc.kibot.yaml | 1 - ...mport_test_internal_jlc_stencil.kibot.yaml | 1 - 7 files changed, 57 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 2f280b37..d341cafc 100644 --- a/README.md +++ b/README.md @@ -4660,18 +4660,29 @@ Another important detail is that global options that are lists gets the values m The last set of values found is inserted at the beginning of the list. You can collect filters for all the imported global sections. +It's recommended to always use some file extension in the *FILE_CONTAINING_THE_YAML_DEFINITIONS* name. +If you don't use any file extension and you use a relative path this name could be confused with an internal template. +See [Importing internal templates](#importing-internal-templates). +If you need to use a name without any extension and a relative path, and this name is the same used for a KiBot template use the `is_external` option: + +```yaml +import: + - file: Elecrow + is_external: true +``` + + #### Importing internal templates KiBot has some internally defined outputs, groups and filters. You can easily use them with the `import` mechanism. -Use the `file` mechanism and add the `is_internal` option. -When importing an internal template you don't need to specify its location and/or file extension. +Use the `file` mechanism and don't include the extension for the file. +When importing an internal template you don't need to specify its location. Here is an example: ```yaml import: - file: Elecrow - is_internal: true ``` This will import the definitions for the internal Elecrow configuration. diff --git a/docs/README.in b/docs/README.in index d3da06ba..fad06186 100644 --- a/docs/README.in +++ b/docs/README.in @@ -1243,18 +1243,29 @@ Another important detail is that global options that are lists gets the values m The last set of values found is inserted at the beginning of the list. You can collect filters for all the imported global sections. +It's recommended to always use some file extension in the *FILE_CONTAINING_THE_YAML_DEFINITIONS* name. +If you don't use any file extension and you use a relative path this name could be confused with an internal template. +See [Importing internal templates](#importing-internal-templates). +If you need to use a name without any extension and a relative path, and this name is the same used for a KiBot template use the `is_external` option: + +```yaml +import: + - file: Elecrow + is_external: true +``` + + #### Importing internal templates KiBot has some internally defined outputs, groups and filters. You can easily use them with the `import` mechanism. -Use the `file` mechanism and add the `is_internal` option. -When importing an internal template you don't need to specify its location and/or file extension. +Use the `file` mechanism and don't include the extension for the file. +When importing an internal template you don't need to specify its location. Here is an example: ```yaml import: - file: Elecrow - is_internal: true ``` This will import the definitions for the internal Elecrow configuration. diff --git a/kibot/config_reader.py b/kibot/config_reader.py index 54cef3ea..ce6539c0 100644 --- a/kibot/config_reader.py +++ b/kibot/config_reader.py @@ -453,6 +453,27 @@ class CfgYamlReader(object): for o_var in variants.values(): self.configure_variant_or_filter(o_var) + def check_import_file_name(self, fn, is_external): + fn = os.path.expandvars(os.path.expanduser(fn)) + is_internal = False + if not is_external and not os.path.splitext(fn)[1] and not os.path.isabs(fn): + name = fn + fn = os.path.join(GS.get_resource_path('config_templates'), fn+'.kibot.yaml') + if not os.path.isfile(fn): + fn_abs = os.path.join(dir, name) + if not os.path.isfile(fn_abs): + raise KiPlotConfigurationError("Unknown internal import file `{}` ({})".format(name, fn)) + # Bizarre case: looks like an internal + fn = fn_abs + else: + is_internal = True + else: + if not os.path.isabs(fn): + fn = os.path.join(dir, fn) + if not os.path.isfile(fn): + raise KiPlotConfigurationError("Missing import file `{}`".format(fn)) + return fn, is_internal + def _parse_import(self, imp, name, apply=True, depth=0): """ Get imports """ logger.debug("Parsing imports: {}".format(imp)) @@ -462,12 +483,12 @@ class CfgYamlReader(object): if not isinstance(imp, list): raise KiPlotConfigurationError("Incorrect `import` section (must be a list)") # Import the files - dir = os.path.dirname(os.path.abspath(name)) + os.path.dirname(os.path.abspath(name)) all_collected = CollectedImports() for entry in imp: - is_internal = False explicit_fils = explicit_vars = explicit_globals = explicit_pres = explicit_groups = False if isinstance(entry, str): + is_external = True fn = entry outs = None filters = [] @@ -477,17 +498,17 @@ class CfgYamlReader(object): groups = [] explicit_outs = True elif isinstance(entry, dict): - fn = outs = filters = vars = globals = pre = groups = is_internal = None - explicit_outs = False + fn = outs = filters = vars = globals = pre = groups = None + explicit_outs = is_external = False for k, v in entry.items(): if k == 'file': if not isinstance(v, str): raise KiPlotConfigurationError("`import.file` must be a string ({})".format(str(v))) fn = v - elif k == 'is_internal': + elif k == 'is_external': if not isinstance(v, bool): - raise KiPlotConfigurationError("`import.is_internal` must be a true/false ({})".format(str(v))) - is_internal = v + raise KiPlotConfigurationError("`import.is_external` must be a true/false ({})".format(str(v))) + is_external = v elif k == 'outputs': outs = self._parse_import_items(k, fn, v) explicit_outs = True @@ -512,17 +533,7 @@ class CfgYamlReader(object): raise KiPlotConfigurationError("`import` entry without `file` ({})".format(str(entry))) else: raise KiPlotConfigurationError("`import` items must be strings or dicts ({})".format(str(entry))) - if is_internal: - name = fn - fn = os.path.join(GS.get_resource_path('config_templates'), fn+'.kibot.yaml') - if not os.path.isfile(fn): - raise KiPlotConfigurationError("Unknown internal import file `{}` ({})".format(name, fn)) - else: - fn = os.path.expandvars(os.path.expanduser(fn)) - if not os.path.isabs(fn): - fn = os.path.join(dir, fn) - if not os.path.isfile(fn): - raise KiPlotConfigurationError("Missing import file `{}`".format(fn)) + fn, is_internal = self.check_import_file_name(fn, is_external) fn_rel = os.path.relpath(fn) data = self.load_yaml(open(fn)) if 'import' in data: diff --git a/kibot/resources/config_templates/JLCPCB_stencil.kibot.yaml b/kibot/resources/config_templates/JLCPCB_stencil.kibot.yaml index 5a8088e4..f6549763 100644 --- a/kibot/resources/config_templates/JLCPCB_stencil.kibot.yaml +++ b/kibot/resources/config_templates/JLCPCB_stencil.kibot.yaml @@ -6,7 +6,6 @@ kibot: import: - file: JLCPCB - is_internal: true outputs: # We exclude the _JLCPCB_compress - _JLCPCB_gerbers diff --git a/tests/yaml_samples/import_test_internal_1.kibot.yaml b/tests/yaml_samples/import_test_internal_1.kibot.yaml index 8b64007d..2c6fed1c 100644 --- a/tests/yaml_samples/import_test_internal_1.kibot.yaml +++ b/tests/yaml_samples/import_test_internal_1.kibot.yaml @@ -3,4 +3,3 @@ kibot: import: - file: Elecrow - is_internal: true diff --git a/tests/yaml_samples/import_test_internal_jlc.kibot.yaml b/tests/yaml_samples/import_test_internal_jlc.kibot.yaml index 9b8d711e..1cc88611 100644 --- a/tests/yaml_samples/import_test_internal_jlc.kibot.yaml +++ b/tests/yaml_samples/import_test_internal_jlc.kibot.yaml @@ -3,4 +3,3 @@ kibot: import: - file: JLCPCB - is_internal: true diff --git a/tests/yaml_samples/import_test_internal_jlc_stencil.kibot.yaml b/tests/yaml_samples/import_test_internal_jlc_stencil.kibot.yaml index a1ae7e6e..99795b0e 100644 --- a/tests/yaml_samples/import_test_internal_jlc_stencil.kibot.yaml +++ b/tests/yaml_samples/import_test_internal_jlc_stencil.kibot.yaml @@ -3,4 +3,3 @@ kibot: import: - file: JLCPCB_stencil - is_internal: true