Skip to content

Commit 2d246b4

Browse files
committed
chore: rebase changes from PR #20 after modernizing 'master'
@ctheune PTAL -- this seems to cause a hang for me in the tests.
1 parent 1af1574 commit 2d246b4

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

peppercorn/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self):
3232
super().__init__("Not enough end markers")
3333

3434

35-
def parse(tokens):
35+
def parse(tokens, unique_key_separator=None):
3636
""" Infer a data structure from the ordered set of fields and
3737
return it."""
3838
target = typ = None
@@ -41,14 +41,21 @@ def parse(tokens):
4141

4242
for token in tokens:
4343
key, val = token
44+
45+
if unique_key_separator:
46+
key = key.rsplit(unique_key_separator, maxsplit=1)[0]
47+
4448
if key == START:
4549
stack.append((target, typ, out))
4650
target, typ = data_type(val)
51+
4752
if typ in TYPS:
4853
out = []
4954
else:
5055
raise UnknownStartMarker(token)
56+
5157
elif key == END:
58+
5259
if typ == SEQUENCE:
5360
parsed = [v for (k, v) in out]
5461
elif typ == MAPPING:
@@ -59,13 +66,15 @@ def parse(tokens):
5966
parsed = None
6067
else:
6168
raise TooManyEndMarkers()
69+
6270
prev_target, prev_typ, out = stack.pop()
6371
if parsed is not None:
6472
out.append((target, parsed))
6573
target = prev_target
6674
typ = prev_typ
75+
6776
else:
68-
out.append(token)
77+
out.append((key, val))
6978

7079
if stack:
7180
raise NotEnoughEndMarkers()

peppercorn/tests.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,50 @@ def test_insufficient_end_markers():
248248

249249
with pytest.raises(NotEnoughEndMarkers):
250250
parse(iem_fields)
251+
252+
253+
def test_bare_with_marker(fields):
254+
from peppercorn import parse
255+
256+
i = 0
257+
def next_id():
258+
nonlocal i
259+
i += 1
260+
return str(i)
261+
262+
fields_with_marker = [
263+
(key + ":" + next_id(), value)
264+
for key, value in fields
265+
]
266+
267+
result = parse(fields_with_marker, unique_key_separator=":")
268+
269+
_assertFieldsResult(result)
270+
271+
272+
def test_bare_without_marker(fields):
273+
# This is proof that ":" isn't something special when we don't
274+
# provide a unique key separator
275+
from peppercorn import START, END
276+
from peppercorn import parse
277+
278+
fields_wo_marker = []
279+
280+
for key, value in fields:
281+
if key not in [START, END]:
282+
key = key + ":something"
283+
fields.append((key, value))
284+
285+
result = self._callFUT(fields_wo_marker)
286+
287+
assert result == {
288+
'series': {
289+
'name:something':'date series 1',
290+
'dates': [
291+
['10', '12', '2008'],
292+
['10', '12', '2009'],
293+
],
294+
},
295+
'name:something': 'project1',
296+
'title:something': 'Cool project',
297+
}

0 commit comments

Comments
 (0)