From ddd0018f432cd0821c0756cd2dfb39b898fc697f Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Fri, 28 Oct 2022 07:51:54 -0300 Subject: [PATCH] [Populate] Handle missing mistune properly --- kibot/PcbDraw/populate.py | 18 ++++++++++++++++++ kibot/out_populate.py | 31 +++++++++++++++++-------------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/kibot/PcbDraw/populate.py b/kibot/PcbDraw/populate.py index c1a7a154..2f21f326 100644 --- a/kibot/PcbDraw/populate.py +++ b/kibot/PcbDraw/populate.py @@ -260,12 +260,30 @@ def prepare_params(params: List[str]) -> List[str]: return list(chain(*p)) +# Added for Kibot +# The rendender selection, they are imported here def create_renderer(format, initial_components): if format == "html": return Renderer(HTMLRenderer, initial_components) # type: ignore return Renderer(mdrenderer.MdRenderer, initial_components) # type: ignore +# The helper to look for a file, to avoid pulling LXML from pcbdraw +def find_data_file(name: str, extension: str, data_paths: List[str], subdir: Optional[str]=None) -> Optional[str]: + if not name.endswith(extension): + name += extension + if os.path.isfile(name): + return name + for path in data_paths: + if subdir is not None: + fname = os.path.join(path, subdir, name) + if os.path.isfile(fname): + return fname + fname = os.path.join(path, name) + if os.path.isfile(fname): + return fname + return None + # @click.command() # @click.argument("input", type=click.Path(exists=True, file_okay=True, dir_okay=False)) # @click.argument("output", type=click.Path(file_okay=False, dir_okay=True)) diff --git a/kibot/out_populate.py b/kibot/out_populate.py index 7f9b29f3..97e075c9 100644 --- a/kibot/out_populate.py +++ b/kibot/out_populate.py @@ -20,9 +20,6 @@ from .gs import GS from .kiplot import config_output, run_output from .optionable import Optionable from .out_base import VariantOptions -from .PcbDraw.populate import (load_content, get_data_path, read_template, create_renderer, parse_content, generate_html, - generate_markdown) -from .PcbDraw.plot import find_data_file from .registrable import RegOutput from .macros import macros, document, output_class # noqa: F401 from . import log @@ -72,17 +69,6 @@ class PopulateOptions(VariantOptions): raise KiPlotConfigurationError('You must specify an input markdown file') if not os.path.isfile(self.input): raise KiPlotConfigurationError('Missing input file `{}`'.format(self.input)) - # Load the template - if self.format == 'html': - data_path = get_data_path() - data_path.insert(0, os.path.join(GS.get_resource_path('pcbdraw'), 'templates')) - template_file = find_data_file(self.template, '.handlebars', data_path) - if not template_file: - raise KiPlotConfigurationError('Unable to find template file `{}`'.format(self.template)) - try: - self._template = read_template(template_file) - except IOError: - raise KiPlotConfigurationError('Failed to load file `{}`'.format(template_file)) # Initial components self.initial_components = Optionable.force_list(self.initial_components) # Validate the image patter name @@ -150,6 +136,12 @@ class PopulateOptions(VariantOptions): return content def run(self, dir_name): + # Ensure we have mistune + self.ensure_tool('mistune') + # Now we can use populate + from .PcbDraw.populate import (load_content, get_data_path, read_template, create_renderer, parse_content, + generate_html, generate_markdown, find_data_file) + is_html = self.format == 'html' # Check the renderer output is valid out = RegOutput.get_output(self.renderer) @@ -162,6 +154,17 @@ class PopulateOptions(VariantOptions): _, content = load_content(self.input) except IOError: raise KiPlotConfigurationError('Failed to load `{}`'.format(self.input)) + # Load the template + if self.format == 'html': + data_path = get_data_path() + data_path.insert(0, os.path.join(GS.get_resource_path('pcbdraw'), 'templates')) + template_file = find_data_file(self.template, '.handlebars', data_path) + if not template_file: + raise KiPlotConfigurationError('Unable to find template file `{}`'.format(self.template)) + try: + self._template = read_template(template_file) + except IOError: + raise KiPlotConfigurationError('Failed to load file `{}`'.format(template_file)) # Initialize the output file renderer renderer = create_renderer(self.format, self.initial_components) outputfile = 'index.html' if is_html else 'index.md'