From bc5d1b15c6a2d3e76a0251d1cbad394e78c3def5 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Mon, 18 Oct 2021 17:04:44 -0300 Subject: [PATCH] Generic filter: added options to match if a field is/isn't defined. --- CHANGELOG.md | 1 + README.md | 4 ++++ kibot/fil_generic.py | 8 ++++++++ kibot/out_base.py | 4 ++++ 4 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c6e7072..f713ea8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Internal BoM: when a `Value` field can't be interpreted as a `number+unit`, and it contain at least one space, now we try to use the text before the space. This helps for cases like "10K 1%". +- Generic filter: options to match if a field is/isn't defined. ### Changed - Internal BoM: now components with different Tolerance, Voltage, Current diff --git a/README.md b/README.md index adc94ef2..5d5034b0 100644 --- a/README.md +++ b/README.md @@ -305,6 +305,8 @@ Currently the only type available is `generic`. - `column`: [string=''] Name of the column to apply the regular expression. - *field*: Alias for column. - `invert`: [boolean=false] Invert the regex match result. + - `match_if_field`: [boolean=false] Match if the field exists, no regex applied. Not affected by `invert`. + - `match_if_no_field`: [boolean=false] Match if the field doesn't exists, no regex applied. Not affected by `invert`. - `regex`: [string=''] Regular expression to match. - *regexp*: Alias for regex. - `skip_if_no_field`: [boolean=false] Skip this test if the field doesn't exist. @@ -326,6 +328,8 @@ Currently the only type available is `generic`. - `column`: [string=''] Name of the column to apply the regular expression. - *field*: Alias for column. - `invert`: [boolean=false] Invert the regex match result. + - `match_if_field`: [boolean=false] Match if the field exists, no regex applied. Not affected by `invert`. + - `match_if_no_field`: [boolean=false] Match if the field doesn't exists, no regex applied. Not affected by `invert`. - `regex`: [string=''] Regular expression to match. - *regexp*: Alias for regex. - `skip_if_no_field`: [boolean=false] Skip this test if the field doesn't exist. diff --git a/kibot/fil_generic.py b/kibot/fil_generic.py index 19dce981..fd99e344 100644 --- a/kibot/fil_generic.py +++ b/kibot/fil_generic.py @@ -124,6 +124,10 @@ class Generic(BaseFilter): # noqa: F821 if reg.skip_if_no_field and not c.is_field(reg.column): # Skip the check if the field doesn't exist continue + if reg.match_if_field and c.is_field(reg.column): + return True + if reg.match_if_no_field and not c.is_field(reg.column): + return True field_value = c.get_field_value(reg.column) res = reg.regex.search(field_value) if reg.invert: @@ -145,6 +149,10 @@ class Generic(BaseFilter): # noqa: F821 if reg.skip_if_no_field and not c.is_field(reg.column): # Skip the check if the field doesn't exist continue + if reg.match_if_field and c.is_field(reg.column): + return True + if reg.match_if_no_field and not c.is_field(reg.column): + return True field_value = c.get_field_value(reg.column) res = reg.regex.search(field_value) if reg.invert: diff --git a/kibot/out_base.py b/kibot/out_base.py index a7e15562..8ada9958 100644 --- a/kibot/out_base.py +++ b/kibot/out_base.py @@ -104,6 +104,10 @@ class BoMRegex(Optionable): """ {regex} """ self.skip_if_no_field = False """ Skip this test if the field doesn't exist """ + self.match_if_field = False + """ Match if the field exists, no regex applied. Not affected by `invert` """ + self.match_if_no_field = False + """ Match if the field doesn't exists, no regex applied. Not affected by `invert` """ self.invert = False """ Invert the regex match result """