Skip to content

Commit

Permalink
Merge pull request #108 from dimagi/sk/jpath-exception
Browse files Browse the repository at this point in the history
catch all parsing errors from jsonpath
  • Loading branch information
snopoke committed Oct 11, 2018
2 parents bd884b1 + 5176a07 commit 661eb24
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions commcare_export/excel_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,21 @@ def _get_safe_source_field(source_field):
def _safe_node(node):
try:
parse_jsonpath(node)
except JsonPathLexerError:
except Exception:
# quote nodes with special characters
return '"{}"'.format(node)
else:
return node

try:
parse_jsonpath(source_field)
except JsonPathLexerError:
except Exception:
source_field = '.'.join([
_safe_node(node) if node else node
for node in source_field.split('.')
])
if source_field.endswith('.'):
raise Exception("Blank node path: {}".format(source_field))

return Reference(source_field)

Expand Down
7 changes: 7 additions & 0 deletions tests/test_excel_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ def test_get_safe_source_field(self):
assert _get_safe_source_field('foo..baz[*]') == Reference('foo..baz[*]')
assert _get_safe_source_field('foo.#baz') == Reference('foo."#baz"')
assert _get_safe_source_field('foo.bar[*]..%baz') == Reference('foo.bar[*].."%baz"')
assert _get_safe_source_field('foo.bar:1.baz') == Reference('foo."bar:1".baz')

try:
assert _get_safe_source_field('foo.bar.')
assert False, "Expected exception"
except Exception:
pass

def test_compile_mappings(self):
test_cases = [
Expand Down

0 comments on commit 661eb24

Please sign in to comment.