Skip to content
Snippets Groups Projects
Verified Commit b3f14469 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

FIX: Better ID pattern, refactored a bit, added tests.

parent 874caa27
No related branches found
No related tags found
2 merge requests!178FIX: #96 Better error output for crawl.py script.,!169Umlaut in cfood
Pipeline #51519 passed with warnings
...@@ -53,16 +53,16 @@ from .utils import has_parent ...@@ -53,16 +53,16 @@ from .utils import has_parent
# by the converters: # by the converters:
SPECIAL_PROPERTIES = ("description", "name", "id", "path", SPECIAL_PROPERTIES = ("description", "name", "id", "path",
"file", "checksum", "size") "file", "checksum", "size")
# This RE is used to check whether a value is just a single variable: ID_PATTERN = r"\D[.\w]*"
SINGLE_VAR_RE = re.compile(r"^\$(\{)?(?P<varname>\w+)(\})?$") SINGLE_VAR_RE = re.compile(r"^\$(\{)?(?P<varname>" + ID_PATTERN + r")(\})?$")
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class CrawlerTemplate(Template): class CrawlerTemplate(Template):
# This adds a dot to the default pattern. # This also adds a dot to the default pattern.
# See: https://docs.python.org/3/library/string.html#template-strings # See: https://docs.python.org/3/library/string.html#template-strings
# Default flags is re.IGNORECASE # Default flags is re.IGNORECASE
braceidpattern = r"(?a:[_a-z][_\.a-z0-9]*)" braceidpattern = ID_PATTERN
def _only_max(children_with_keys): def _only_max(children_with_keys):
......
...@@ -116,12 +116,15 @@ def test_issue_93(): ...@@ -116,12 +116,15 @@ def test_issue_93():
cfood.yaml does not allow umlaut in $expression""" cfood.yaml does not allow umlaut in $expression"""
values = GeneralStore() values = GeneralStore()
expressions = [ expressions = [
"1", "foo",
"foo.bär",
"_1",
"Ä", "Ä",
"ųøîµ", "ųøîµ",
] ]
for exp in expressions: for exp in expressions:
values[exp] = f"This is {exp}" values[exp] = f"This is {exp}"
# ## Test preliminary check
# With braces # With braces
for exp in expressions: for exp in expressions:
assert replace_variables(f"${{{exp}}}", values) == f"This is {exp}" assert replace_variables(f"${{{exp}}}", values) == f"This is {exp}"
...@@ -129,15 +132,19 @@ def test_issue_93(): ...@@ -129,15 +132,19 @@ def test_issue_93():
for exp in expressions: for exp in expressions:
assert replace_variables(f"${exp}", values) == f"This is {exp}" assert replace_variables(f"${exp}", values) == f"This is {exp}"
# ## Test actual replacement
def test_crawler_template(): for exp in expressions:
temp = CrawlerTemplate("$bla") # as-is
assert temp.safe_substitute(**{"bla": "test"}) == "test" propvalue = f"${{{exp}}}"
propvalue_template = CrawlerTemplate(propvalue)
# Umlauts are not replaced, because they are not contained # from IPython import embed
# in the CrawlerTemplate's braceidpattern: # embed()
temp = CrawlerTemplate("$blä")
assert temp.safe_substitute(**{"blä": "test"}) == "$blä" assert propvalue_template.safe_substitute(**values.get_storage()) == f"This is {exp}"
temp = CrawlerTemplate("${blä}") # String embedded into context
assert temp.safe_substitute(**{"blä": "test"}) == "${blä}" propvalue = f"some text before >> ${{{exp}}} << some text after"
print(propvalue)
propvalue_template = CrawlerTemplate(propvalue)
assert (propvalue_template.safe_substitute(**values.get_storage())
== f"some text before >> This is {exp} << some text after")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment