Skip to content

Commit

Permalink
Fix parsing of inline opening brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
lasa01 committed May 4, 2020
1 parent 74fb682 commit ba557e5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
35 changes: 35 additions & 0 deletions tests/test_vdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,41 @@ def test_single_line_empty_block(self):
self.assertEqual(vdf.loads(INPUT), EXPECTED)
self.assertEqual(vdf.loads(INPUT, escaped=False), EXPECTED)

def test_inline_opening_bracker(self):
INPUT = '''
"root1" {
"key1" {
}
key2 "value2"
"key3" value3
}
root2 { }
root3 {
"key1" "value1"
key2 {
}
"key3" value3
}
'''

EXPECTED = {
'root1': {
'key1': {},
'key2': 'value2',
'key3': 'value3',
},
'root2': {},
'root3': {
'key1': 'value1',
'key2': {},
'key3': 'value3',
}
}

self.assertEqual(vdf.loads(INPUT), EXPECTED)
self.assertEqual(vdf.loads(INPUT, escaped=False), EXPECTED)

class testcase_VDF_other(unittest.TestCase):
def test_dumps_pretty_output(self):
tests = [
Expand Down
7 changes: 4 additions & 3 deletions vdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def parse(fp, mapper=dict, merge_duplicate_keys=True, escaped=True):
r'([ \t]*('
r'"(?P<qval>(?:\\.|[^\\"])*)(?P<vq_end>")?'
r'|(?P<val>(?:(?<!/)/(?!/)|[a-z0-9\-\_\\\?\*\.$<>])+)'
r'|(?P<eblock>{[ \t]*})'
r'|(?P<sblock>{[ \t]*)(?P<eblock>})?'
r'))?',
flags=re.I)

Expand Down Expand Up @@ -149,9 +149,10 @@ def parse(fp, mapper=dict, merge_duplicate_keys=True, escaped=True):
stack[-1][key] = _m

if match.group('eblock') is None:
# only expect a bracket if the block is not closed on the same line
# only expect a bracket if it's not already closed or on the same line
stack.append(_m)
expect_bracket = True
if match.group('sblock') is None:
expect_bracket = True

# we've matched a simple keyvalue pair, map it to the last dict obj in the stack
else:
Expand Down

0 comments on commit ba557e5

Please sign in to comment.