Skip to content

Commit

Permalink
bugfix RegexProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasprobst committed Oct 10, 2023
1 parent 442ec06 commit 3a2b3c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
12 changes: 10 additions & 2 deletions h5rdmtoolbox/conventions/generate_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ def get_regex_name():


class RegexProcessor:
"""Process regex validator.
Parameters
----------
standard_attribute: Dict
Standard attribute dictionary, e.g. {'validator': 'regex(r"^[a-zA-Z0-9_]*$")'}
"""
def __init__(self, standard_attribute: Dict):
regex_validator = get_regex_name()
validator = standard_attribute['validator']
self.name = regex_validator
match = re.search(r'regex\((.*?)\)', validator)
re_pattern = match.group(1)
re_pattern = validator.split('regex(', 1)[1].rsplit(')', 1)[0]
# match = re.search(r'regex\((.*?)\)', validator)
# re_pattern = match.group(1)

self.standard_attribute = standard_attribute.copy()
if re_pattern.startswith("r'") and re_pattern.endswith("'"):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = h5rdmtoolbox
version = 0.11.1
version = 0.11.2
author = Matthias Probst
author_email = matthias.probst@kit.edu
description = Supporting a FAIR Research Data lifecycle using Python and HDF5.
Expand Down
9 changes: 9 additions & 0 deletions tests/conventions/test_toolbox_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class MyStr(BaseModel):
self.assertListEqual(['1', '2'], h5.keywords)
cv.delete()

def test_validate_regex(self):
from h5rdmtoolbox.conventions.generate_utils import RegexProcessor

rp = RegexProcessor({'validator': 'regex(r"^[a-zA-Z0-9_]*$")'})
self.assertEqual('r"^[a-zA-Z0-9_]*$"', rp.re_pattern)

rp = RegexProcessor({'validator': '$regex(^[a-zA-Z].*(?<!\s)$)'})
self.assertEqual('^[a-zA-Z].*(?<!\s)$', rp.re_pattern)

def test_validate_orcid(self):
class Validator(BaseModel):
orcid: toolbox_validators.orcid
Expand Down

0 comments on commit 3a2b3c3

Please sign in to comment.