Skip to content

Commit

Permalink
Merge b786af8 into d0e5b60
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Sep 12, 2019
2 parents d0e5b60 + b786af8 commit d6c3988
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
34 changes: 27 additions & 7 deletions trollsift/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,19 @@ def format_field(self, value, format_spec):
field_name, value = value
return self.regex_field(field_name, value, format_spec)

def extract_values(self, fmt, stri):
def extract_values(self, fmt, stri, full_match=True):
"""Extract information from string matching format.
Args:
fmt (str): Python format string to match against
stri (str): String to extract information from
full_match (bool): Force the match of the whole string. Default
to ``True``.
"""
regex = self.format(fmt)
if full_match:
regex = '^' + regex + '$'
match = re.match(regex, stri)
if match is None:
raise ValueError("String does not match pattern.")
Expand All @@ -307,9 +318,10 @@ def extract_values(self, fmt, stri):


def _get_number_from_fmt(fmt):
"""
Helper function for extract_values,
figures out string length from format string.
"""Helper function for extract_values.
Figures out string length from format string.
"""
if '%' in fmt:
# its datetime
Expand Down Expand Up @@ -362,10 +374,18 @@ def get_convert_dict(fmt):
return convdef


def parse(fmt, stri):
"""Parse keys and corresponding values from *stri* using format described in *fmt* string."""
def parse(fmt, stri, full_match=True):
"""Parse keys and corresponding values from *stri* using format described in *fmt* string.
Args:
fmt (str): Python format string to match against
stri (str): String to extract information from
full_match (bool): Force the match of the whole string. Default
True.
"""
convdef = get_convert_dict(fmt)
keyvals = regex_formatter.extract_values(fmt, stri)
keyvals = regex_formatter.extract_values(fmt, stri, full_match=True)
for key in convdef.keys():
keyvals[key] = _convert(convdef[key], keyvals[key])

Expand Down
9 changes: 9 additions & 0 deletions trollsift/tests/unittests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ def test_extract_values_fails(self):
fmt = '/somedir/{directory}/hrpt_{platform:4s}{platnum:2s}_{time:%Y%m%d_%H%M}_{orbit:4d}.l1b'
self.assertRaises(ValueError, regex_formatter.extract_values, fmt, self.string)

def test_extract_values_full_match(self):
"""Test that a string must completely match."""
fmt = '{orbit:05d}'
val = regex_formatter.extract_values(fmt, '12345')
self.assertEqual(val, {'orbit': '12345'})
self.assertRaises(ValueError, regex_formatter.extract_values, fmt, '12345abc')
val = regex_formatter.extract_values(fmt, '12345abc', full_match=False)
self.assertEqual(val, {'orbit': '12345'})

def test_convert_digits(self):
self.assertEqual(_convert('d', '69022'), 69022)
self.assertRaises(ValueError, _convert, 'd', '69dsf')
Expand Down

0 comments on commit d6c3988

Please sign in to comment.