blob: e55065105a6062836c69dd4a8b9d16ea9471be82 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
from collections import OrderedDict, deque, namedtuple
import shlex
Meta = namedtuple('Meta', ['sects', 'meta'])
def _merge(l):
return ''.join(l)
def _next(l):
sect = deque()
while l and not l[0].strip().startswith('$'):
sect.append(l.popleft())
if not l:
return (('TRAIL', _merge(sect)), l)
name = l.popleft().strip()[1:]
sect = _merge(sect)
return ((name, sect), l)
def parse(s):
l = deque(s.splitlines(True))
meta = dict()
sects = OrderedDict()
while l and l[0].strip().startswith('$'):
s = shlex.split(l[0][1:])
meta[s[0]] = s[1:]
l.popleft()
while l:
s, l = _next(l)
sects[s[0]] = s[1]
return Meta(sects, meta)
|