From a9d5b8cd588c6a3064797076cddc19521b44c8ce Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Fri, 30 Sep 2022 11:28:21 -0300 Subject: [PATCH] New filter `urlify` to convert URLs in fields to HTML links Closes #311 --- CHANGELOG.md | 1 + README.md | 7 +++ docs/README.in | 1 + kibot/fil_base.py | 12 ++++- kibot/fil_urlify.py | 44 +++++++++++++++++++ kibot/kicad/v5_sch.py | 2 +- kibot/misc.py | 1 + .../kicad_6/kibom-variante.kicad_sch | 2 +- tests/yaml_samples/ibom_variant_1.kibot.yaml | 3 +- 9 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 kibot/fil_urlify.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c1c3fa8..4424243b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Internal BoM: Now you can aggregate components using CSV files. (See #248) - Now you can check PCB and schematic parity using the `update_xml` preflight (See #297) +- New filter `urlify` to convert URLs in fields to HTML links (#311) ### Fixed - Problems to compress netlists. (#287) diff --git a/README.md b/README.md index 133d2b65..3955bd4a 100644 --- a/README.md +++ b/README.md @@ -893,6 +893,12 @@ filters: - `split_fields_expand`: [boolean=false] When `true` the fields in `split_fields` are added to the internal names. - `use_ref_sep_for_first`: [boolean=true] Force the reference separator use even for the first component in the list (KiCost behavior). - `value_alt_field`: [string='value_subparts'] Field containing replacements for the `Value` field. So we get real values for split parts. +- urlify: URLify + Converts URL text in fields to HTML URLs. + * Valid keys: + - `comment`: [string=''] A comment for documentation purposes. + - `fields`: [string|list(string)='Datasheet'] Fields to convert. + - `name`: [string=''] Used to identify this particular filter definition. - var_rename: Var_Rename This filter implements the VARIANT:FIELD=VALUE renamer to get FIELD=VALUE when VARIANT is in use. * Valid keys: @@ -930,6 +936,7 @@ The [tests/yaml_samples](https://github.com/INTI-CMNB/KiBot/tree/master/tests/ya #### Built-in filters +- **_datasheet_link** converts Datasheet fields containing URLs into HTML links - **_expand_text_vars** is a default `expand_text_vars` filter - **_kibom_dnc_Config** it uses the internal `dnc_list` to exclude components with - Value matching any of the keys diff --git a/docs/README.in b/docs/README.in index a751b6d1..45ee9fe6 100644 --- a/docs/README.in +++ b/docs/README.in @@ -521,6 +521,7 @@ The [tests/yaml_samples](https://github.com/INTI-CMNB/KiBot/tree/master/tests/ya #### Built-in filters +- **_datasheet_link** converts Datasheet fields containing URLs into HTML links - **_expand_text_vars** is a default `expand_text_vars` filter - **_kibom_dnc_Config** it uses the internal `dnc_list` to exclude components with - Value matching any of the keys diff --git a/kibot/fil_base.py b/kibot/fil_base.py index d84c2910..568cdd0d 100644 --- a/kibot/fil_base.py +++ b/kibot/fil_base.py @@ -7,7 +7,7 @@ from .registrable import RegFilter, Registrable, RegOutput from .optionable import Optionable from .gs import GS from .misc import (IFILT_MECHANICAL, IFILT_VAR_RENAME, IFILT_ROT_FOOTPRINT, IFILT_KICOST_RENAME, DISTRIBUTORS, - IFILT_VAR_RENAME_KICOST, IFILT_KICOST_DNP, IFILT_EXPAND_TEXT_VARS) + IFILT_VAR_RENAME_KICOST, IFILT_KICOST_DNP, IFILT_EXPAND_TEXT_VARS, IFILT_DATASHEET_LINK) from .error import KiPlotConfigurationError from .bom.columnlist import ColumnList from .macros import macros, document # noqa: F401 @@ -251,6 +251,14 @@ class BaseFilter(RegFilter): logger.debug('Creating internal filter: '+str(o_tree)) return o_tree + @staticmethod + def _create_datasheet_link(name): + o_tree = {'name': name} + o_tree['type'] = 'urlify' + o_tree['comment'] = 'Internal datasheet URL to HTML link' + logger.debug('Creating internal filter: '+str(o_tree)) + return o_tree + @staticmethod def _create_kibom_dnx(name): type = name[7:10] @@ -318,6 +326,8 @@ class BaseFilter(RegFilter): tree = BaseFilter._create_kicost_dnp(name) elif name == IFILT_EXPAND_TEXT_VARS: tree = BaseFilter._create_expand_text_vars(name) + elif name == IFILT_DATASHEET_LINK: + tree = BaseFilter._create_datasheet_link(name) else: return None filter = RegFilter.get_class_for(tree['type'])() diff --git a/kibot/fil_urlify.py b/kibot/fil_urlify.py new file mode 100644 index 00000000..adf89b67 --- /dev/null +++ b/kibot/fil_urlify.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2022 Salvador E. Tropea +# Copyright (c) 2022 Instituto Nacional de TecnologĂ­a Industrial +# License: GPL-3.0 +# Project: KiBot (formerly KiPlot) +# Description: Converts URL style text into HTML links +import re +from .optionable import Optionable +from .macros import macros, document, filter_class # noqa: F401 +from . import log + + +logger = log.get_logger() +regex = re.compile(r'(https?://\S+)') + + +@filter_class +class URLify(BaseFilter): # noqa: F821 + """ URLify + Converts URL text in fields to HTML URLs """ + def __init__(self): + super().__init__() + self._is_transform = True + with document: + self.fields = Optionable + """ [string|list(string)='Datasheet'] Fields to convert """ + self._fields_example = 'Datasheet' + + def config(self, parent): + super().config(parent) + if isinstance(self.fields, type): + self.fields = ['Datasheet'] + self.fields = Optionable.force_list(self.fields) + + def filter(self, comp): + """ Look for URLs and convert them """ + for fld in self.fields: + value = comp.get_field_value(fld) + if not value: + continue + new_value = regex.sub(r'\1', value) + if new_value != value: + logger.debugl(2, '{}: {} -> {}'.format(fld, value, new_value)) + comp.set_field(fld, new_value) diff --git a/kibot/kicad/v5_sch.py b/kibot/kicad/v5_sch.py index 6aae030f..e0f06dd1 100644 --- a/kibot/kicad/v5_sch.py +++ b/kibot/kicad/v5_sch.py @@ -1923,7 +1923,7 @@ class Schematic(object): SubElement(comp, 'value').text = c.value if c.footprint: SubElement(comp, 'footprint').text = c.footprint - if len(c.datasheet) and not (self.netlist_version == 'E' and c.datasheet != '~'): + if len(c.datasheet) and not (self.netlist_version == 'E' and c.datasheet == '~'): SubElement(comp, 'datasheet').text = c.datasheet user_fields = c.get_user_fields() if user_fields: diff --git a/kibot/misc.py b/kibot/misc.py index 5c24a784..b06cf00d 100644 --- a/kibot/misc.py +++ b/kibot/misc.py @@ -89,6 +89,7 @@ IFILT_ROT_FOOTPRINT = '_rot_footprint' IFILT_KICOST_RENAME = '_kicost_rename' IFILT_KICOST_DNP = '_kicost_dnp' IFILT_EXPAND_TEXT_VARS = '_expand_text_vars' +IFILT_DATASHEET_LINK = '_datasheet_link' # KiCad 5 GUI values for the attribute UI_THT = 0 # 1 for KiCad 6 UI_SMD = 1 # 2 for KiCad 6 diff --git a/tests/board_samples/kicad_6/kibom-variante.kicad_sch b/tests/board_samples/kicad_6/kibom-variante.kicad_sch index d2adcfa4..4a541e4a 100644 --- a/tests/board_samples/kicad_6/kibom-variante.kicad_sch +++ b/tests/board_samples/kicad_6/kibom-variante.kicad_sch @@ -77,7 +77,7 @@ (property "Footprint" "Resistor_SMD:R_0805_2012Metric" (id 2) (at 54.102 64.77 90) (effects (font (size 1.27 1.27)) hide) ) - (property "Datasheet" "~" (id 3) (at 55.88 64.77 0) + (property "Datasheet" "https://www.vishay.com/docs/28705/mcx0x0xpro.pdf" (id 3) (at 55.88 64.77 0) (effects (font (size 1.27 1.27)) hide) ) (pin "1" (uuid 49c4dcb9-cb9b-421e-b7b9-e495c75a42b7)) diff --git a/tests/yaml_samples/ibom_variant_1.kibot.yaml b/tests/yaml_samples/ibom_variant_1.kibot.yaml index e5f4c816..379efa9c 100644 --- a/tests/yaml_samples/ibom_variant_1.kibot.yaml +++ b/tests/yaml_samples/ibom_variant_1.kibot.yaml @@ -2,11 +2,11 @@ kibot: version: 1 - variants: - name: 'default' comment: 'Default KiBoM variant' type: kibom + pre_transform: _datasheet_link - name: 't1_v1' comment: 'Test 1 Variant V1' @@ -39,6 +39,7 @@ outputs: dir: BoM options: variant: 'default' + extra_fields: Datasheet - name: 'bom_internal_v1' comment: "Bill of Materials in CSV format for variant t1_v1"