Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parse accepting strings with trailing characters #15

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .stickler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters:
flake8:
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