Skip to content

Commit

Permalink
Added support for spaces in unquoted values
Browse files Browse the repository at this point in the history
For example, file "csgo\materials\models\props_c17\furniturewooddresser001a.vmt" has these.
  • Loading branch information
lasa01 committed Sep 26, 2020
1 parent ba557e5 commit ecac10d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
14 changes: 12 additions & 2 deletions tests/test_vdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@ def test_wierd_symbols_for_unquoted(self):
self.assertEqual(vdf.loads(INPUT), EXPECTED)
self.assertEqual(vdf.loads(INPUT, escaped=False), EXPECTED)

def test_space_for_unquoted(self):
INPUT = 'a b c d \n efg h i\t // j k\n'
EXPECTED= {
'a': 'b c d',
'efg': 'h i',
}

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

def test_merge_multiple_keys_on(self):
INPUT = '''
a
Expand Down Expand Up @@ -437,9 +447,9 @@ def test_inline_opening_bracker(self):
"key3" value3
}
root2 { }
root3 {
root3 {
"key1" "value1"
key2 {
key2 {
}
"key3" value3
Expand Down
11 changes: 9 additions & 2 deletions vdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def parse(fp, mapper=dict, merge_duplicate_keys=True, escaped=True):
re_keyvalue = re.compile(r'^("(?P<qkey>(?:\\.|[^\\"])+)"|(?P<key>#?[a-z0-9\-\_\\\?$%<>]+))'
r'([ \t]*('
r'"(?P<qval>(?:\\.|[^\\"])*)(?P<vq_end>")?'
r'|(?P<val>(?:(?<!/)/(?!/)|[a-z0-9\-\_\\\?\*\.$<>])+)'
r'|(?P<val>(?:(?<!/)/(?!/)|[a-z0-9\-\_\\\?\*\.$<> ])+)'
r'|(?P<sblock>{[ \t]*)(?P<eblock>})?'
r'))?',
flags=re.I)
Expand Down Expand Up @@ -135,7 +135,14 @@ def parse(fp, mapper=dict, merge_duplicate_keys=True, escaped=True):
(getattr(fp, 'name', '<%s>' % fp.__class__.__name__), lineno, 0, line))

key = match.group('key') if match.group('qkey') is None else match.group('qkey')
val = match.group('val') if match.group('qval') is None else match.group('qval')
if match.group('qval') is None:
val = match.group('val')
if val is not None:
val = val.rstrip()
if val == "":
val = None
else:
val = match.group('qval')

if escaped:
key = _unescape(key)
Expand Down

0 comments on commit ecac10d

Please sign in to comment.