Skip to content

Commit

Permalink
Merge d498ec0 into 8667c86
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese authored Nov 1, 2018
2 parents 8667c86 + d498ec0 commit 9244f59
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions trollsift/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ class RegexFormatter(string.Formatter):
ESCAPE_CHARACTERS = [x for x in string.punctuation if x not in '\\%']
ESCAPE_SETS = [(c, '\{}'.format(c)) for c in ESCAPE_CHARACTERS]

def __init__(self):
# hold on to fields we've seen already so we can reuse their
# definitions in the regex
self._cached_fields = {}
super(RegexFormatter, self).__init__()

def format(*args, **kwargs):
try:
# super() doesn't seem to work here
ret_val = string.Formatter.format(*args, **kwargs)
finally:
self = args[0] # just matching the parent class
self._cached_fields.clear()
return ret_val

def _escape(self, s):
"""Escape bad characters for regular expressions.
Expand Down Expand Up @@ -260,6 +275,14 @@ def regex_field(self, field_name, value, format_spec):
if value != self.UNPROVIDED_VALUE:
return super(RegexFormatter, self).format_field(value, format_spec)

if self._cached_fields.get(field_name, format_spec) != format_spec:
raise ValueError("Can't specify the same field_name with "
"different formats: {}".format(field_name))
elif field_name in self._cached_fields:
return r'(?P={})'.format(field_name)
else:
self._cached_fields[field_name] = format_spec

# Replace format spec with glob patterns (*, ?, etc)
if not format_spec:
return r'(?P<{}>.*)'.format(field_name)
Expand Down
8 changes: 8 additions & 0 deletions trollsift/tests/integrationtests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ def test_parse_olci_l1b(self):
result = p.parse(filename)
self.assertDictEqual(result, data)

def test_parse_duplicate_fields(self):
"""Test parsing a pattern that has duplicate fields."""
fmt = '{version_number:1s}/filename_with_version_number_{version_number:1s}.tif'
filename = '1/filename_with_version_number_1.tif'
p = Parser(fmt)
result = p.parse(filename)
self.assertEqual(result['version_number'], '1')


def suite():
"""The suite for test_parser
Expand Down

0 comments on commit 9244f59

Please sign in to comment.