Skip to content

Commit

Permalink
Merge pull request #15 from djhoese/bugfix-full-match
Browse files Browse the repository at this point in the history
Fix parse accepting strings with trailing characters
  • Loading branch information
djhoese committed Oct 9, 2019
2 parents d6c0b81 + f7f2aa2 commit ad03b59
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
6 changes: 3 additions & 3 deletions .stickler.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
linters:
flake8:
fixer: true
fixers:
enable: true
python: 3
config: setup.cfg

3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ matrix:
os: osx
language: generic
install:
- git clone --depth 1 git://github.com/astropy/ci-helpers.git
#- git clone --depth 1 git://github.com/astropy/ci-helpers.git
- git clone --depth 1 -b all-the-fixes git://github.com/djhoese/ci-helpers.git
- source ci-helpers/travis/setup_conda.sh
script:
- coverage run --source=trollsift setup.py test
Expand Down
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ environment:
NUMPY_VERSION: "stable"

install:
- "git clone --depth 1 git://github.com/astropy/ci-helpers.git"
# - "git clone --depth 1 git://github.com/astropy/ci-helpers.git"
- "git clone --depth 1 -b all-the-fixes git://github.com/djhoese/ci-helpers.git"
- "powershell ci-helpers/appveyor/install-miniconda.ps1"
- "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
- "activate test"
- "conda activate test"

build: false # Not a C# project, build stuff at the test step instead.

Expand Down
38 changes: 29 additions & 9 deletions trollsift/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ def __init__(self, fmt):
def __str__(self):
return self.fmt

def parse(self, stri):
def parse(self, stri, full_match=True):
'''Parse keys and corresponding values from *stri* using format
described in *fmt* string.
'''
return parse(self.fmt, stri)
return parse(self.fmt, stri, full_match=full_match)

def compose(self, keyvals):
'''Return string composed according to *fmt* string and filled
Expand Down 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=full_match)
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 ad03b59

Please sign in to comment.