From 9fdc02ecea7e22774b116acb8204d1ffb480ad0c Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Sun, 12 Jul 2020 09:41:16 -0300 Subject: [PATCH] Added aliases for options. The test case is the pdf_pcb_print.output_name, which is different than other *.output options. Now output_name is an alias for output. --- CHANGELOG.md | 2 ++ README.md | 3 ++- docs/samples/generic_plot.kiplot.yaml | 5 +++-- kiplot/config_reader.py | 14 +++++++++++--- kiplot/optionable.py | 13 ++++++++++--- kiplot/out_pdf_pcb_print.py | 11 +++++------ 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32bbf163..7cf60a2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- pdf_pcb_print.output can be used instead of pdf_pcb_print.output_name ## [0.5.0] - 2020-07-11 ### Changed diff --git a/README.md b/README.md index 3547e84e..75489d9a 100644 --- a/README.md +++ b/README.md @@ -572,7 +572,8 @@ Next time you need this list just use an alias, like this: - `name`: [string=''] Used to identify this particular output definition. - `options`: [dict] Options for the `pdf_pcb_print` output. * Valid keys: - - `output_name`: [string=''] filename for the output PDF (the name of the PCB if empty). + - `output`: [string='%f.pdf'] filename for the output PDF. + - *output_name*: Alias for output. * PDF Schematic Print (Portable Document Format) * Type: `pdf_sch_print` diff --git a/docs/samples/generic_plot.kiplot.yaml b/docs/samples/generic_plot.kiplot.yaml index 18ddb0ad..61b8f3c2 100644 --- a/docs/samples/generic_plot.kiplot.yaml +++ b/docs/samples/generic_plot.kiplot.yaml @@ -363,8 +363,9 @@ outputs: type: 'pdf_pcb_print' dir: 'Example/pdf_pcb_print_dir' options: - # [string=''] filename for the output PDF (the name of the PCB if empty) - output_name: '' + # [string='%f.pdf'] filename for the output PDF + output: '%f.pdf' + # `output_name` is an alias for `output` layers: all # PDF Schematic Print (Portable Document Format): diff --git a/kiplot/config_reader.py b/kiplot/config_reader.py index d77b1272..fabb438e 100644 --- a/kiplot/config_reader.py +++ b/kiplot/config_reader.py @@ -183,11 +183,16 @@ def print_output_options(name, cl, indent): if not num_opts: # We found one, put the title print(ind_str+'* Valid keys:') - help = getattr(obj, '_help_'+k) + help, alias, is_alias = obj.get_doc(k) + if is_alias: + help = 'Alias for '+alias + entry = ' - *{}*: ' + else: + entry = ' - `{}`: ' if help is None: help = 'Undocumented' # pragma: no cover lines = help.split('\n') - preface = ind_str+' - `{}`: '.format(k) + preface = ind_str+entry.format(k) clines = len(lines) print('{}{}{}'.format(preface, lines[0].strip(), '.' if clines == 1 else '')) ind_help = len(preface)*' ' @@ -249,7 +254,10 @@ def print_example_options(f, cls, name, indent, po): if po: obj.read_vals_from_po(po) for k, v in obj.get_attrs_gen(): - help = getattr(obj, '_help_'+k) + help, alias, is_alias = obj.get_doc(k) + if is_alias: + f.write(ind_str+'# `{}` is an alias for `{}`\n'.format(k, alias)) + continue if help: help_lines = help.split('\n') for hl in help_lines: diff --git a/kiplot/optionable.py b/kiplot/optionable.py index 9cd7432b..5483e646 100644 --- a/kiplot/optionable.py +++ b/kiplot/optionable.py @@ -52,6 +52,13 @@ class Optionable(object): if not isinstance(val, bool): raise KiPlotConfigurationError("Option `{}` must be true/false".format(key)) + def get_doc(self, name): + doc = getattr(self, '_help_'+name).strip() + if doc[0] == '{': + alias = doc[1:-1] + return getattr(self, '_help_'+alias).strip(), alias, True + return doc, name, False + @staticmethod def _typeof(v): if isinstance(v, bool): @@ -77,8 +84,8 @@ class Optionable(object): logger.warning("Unknown option `{}`".format(k)) continue # Check the data type - cur_val = getattr(self, k) - cur_doc = getattr(self, '_help_'+k).lstrip() + cur_doc, alias, is_alias = self.get_doc(k) + cur_val = getattr(self, alias) if isinstance(cur_val, bool): Optionable._check_bool(k, v) elif isinstance(cur_val, (int, float)): @@ -126,7 +133,7 @@ class Optionable(object): new_val.append(element) v = new_val # Seems to be ok, map it - setattr(self, k, v) + setattr(self, alias if is_alias else k, v) def config(self, tree): self._tree = tree diff --git a/kiplot/out_pdf_pcb_print.py b/kiplot/out_pdf_pcb_print.py index b3481779..418c2260 100644 --- a/kiplot/out_pdf_pcb_print.py +++ b/kiplot/out_pdf_pcb_print.py @@ -17,16 +17,15 @@ class PDF_Pcb_PrintOptions(BaseOptions): def __init__(self): super().__init__() with document: - self.output_name = '' - """ filename for the output PDF (the name of the PCB if empty) """ # pragma: no cover + self.output = '%f.pdf' + """ filename for the output PDF """ + self.output_name = None + """ {output} """ # pragma: no cover def run(self, output_dir, board, layers): check_script(CMD_PCBNEW_PRINT_LAYERS, URL_PCBNEW_PRINT_LAYERS, '1.4.1') # Output file name - output = self.output_name - if not output: - output = os.path.splitext(os.path.basename(GS.pcb_file))[0]+'.pdf' - output = os.path.abspath(os.path.join(output_dir, output)) + output = os.path.abspath(os.path.join(output_dir, self.expand_filename(self.output))) cmd = [CMD_PCBNEW_PRINT_LAYERS, 'export', '--output_name', output] if BasePreFlight.get_option('check_zone_fills'): cmd.append('-f')