MCPcopy
hub / github.com/pimutils/vdirsyncer / parse

Method parse

vdirsyncer/vobject.py:268–299  ·  view source on GitHub ↗
(cls, lines, multiple=False)

Source from the content-addressed store, hash-verified

266
267 @classmethod
268 def parse(cls, lines, multiple=False):
269 if isinstance(lines, bytes):
270 lines = lines.decode('utf-8')
271 if isinstance(lines, str):
272 lines = lines.splitlines()
273
274 stack = []
275 rv = []
276 try:
277 for _i, line in enumerate(lines):
278 if line.startswith('BEGIN:'):
279 c_name = line[len('BEGIN:'):].strip().upper()
280 stack.append(cls(c_name, [], []))
281 elif line.startswith('END:'):
282 component = stack.pop()
283 if stack:
284 stack[-1].subcomponents.append(component)
285 else:
286 rv.append(component)
287 else:
288 if line.strip():
289 stack[-1].props.append(line)
290 except IndexError:
291 raise ValueError('Parsing error at line {}'.format(_i + 1))
292
293 if multiple:
294 return rv
295 elif len(rv) != 1:
296 raise ValueError('Found {} components, expected one.'
297 .format(len(rv)))
298 else:
299 return rv[0]
300
301 def dump_lines(self):
302 yield 'BEGIN:{}'.format(self.name)

Callers 4

with_uidMethod · 0.45
parsedMethod · 0.45
split_collectionFunction · 0.45
join_collectionFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected