Generic filter: added options to match if a field is/isn't defined.

This commit is contained in:
Salvador E. Tropea 2021-10-18 17:04:44 -03:00
parent 5e20206da4
commit bc5d1b15c6
4 changed files with 17 additions and 0 deletions

View File

@ -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`, - 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 and it contain at least one space, now we try to use the text before the
space. This helps for cases like "10K 1%". space. This helps for cases like "10K 1%".
- Generic filter: options to match if a field is/isn't defined.
### Changed ### Changed
- Internal BoM: now components with different Tolerance, Voltage, Current - Internal BoM: now components with different Tolerance, Voltage, Current

View File

@ -305,6 +305,8 @@ Currently the only type available is `generic`.
- `column`: [string=''] Name of the column to apply the regular expression. - `column`: [string=''] Name of the column to apply the regular expression.
- *field*: Alias for column. - *field*: Alias for column.
- `invert`: [boolean=false] Invert the regex match result. - `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. - `regex`: [string=''] Regular expression to match.
- *regexp*: Alias for regex. - *regexp*: Alias for regex.
- `skip_if_no_field`: [boolean=false] Skip this test if the field doesn't exist. - `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. - `column`: [string=''] Name of the column to apply the regular expression.
- *field*: Alias for column. - *field*: Alias for column.
- `invert`: [boolean=false] Invert the regex match result. - `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. - `regex`: [string=''] Regular expression to match.
- *regexp*: Alias for regex. - *regexp*: Alias for regex.
- `skip_if_no_field`: [boolean=false] Skip this test if the field doesn't exist. - `skip_if_no_field`: [boolean=false] Skip this test if the field doesn't exist.

View File

@ -124,6 +124,10 @@ class Generic(BaseFilter): # noqa: F821
if reg.skip_if_no_field and not c.is_field(reg.column): if reg.skip_if_no_field and not c.is_field(reg.column):
# Skip the check if the field doesn't exist # Skip the check if the field doesn't exist
continue 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) field_value = c.get_field_value(reg.column)
res = reg.regex.search(field_value) res = reg.regex.search(field_value)
if reg.invert: 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): if reg.skip_if_no_field and not c.is_field(reg.column):
# Skip the check if the field doesn't exist # Skip the check if the field doesn't exist
continue 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) field_value = c.get_field_value(reg.column)
res = reg.regex.search(field_value) res = reg.regex.search(field_value)
if reg.invert: if reg.invert:

View File

@ -104,6 +104,10 @@ class BoMRegex(Optionable):
""" {regex} """ """ {regex} """
self.skip_if_no_field = False self.skip_if_no_field = False
""" Skip this test if the field doesn't exist """ """ 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 self.invert = False
""" Invert the regex match result """ """ Invert the regex match result """