-
- def process(self):
- _LOG.info('{!r}: processing {} content characters'.format(
- self, len(self.content)))
- entry = None
- stack = []
- for i,line in enumerate(self.content.splitlines()):
- if line.startswith('BEGIN:'):
- _type = line.split(':', 1)[1]
- _LOG.info('{!r}: begin {}'.format(self, _type))
- stack.append(_type)
- if len(stack) == 2:
- if entry is not None:
- raise ValueError('double entry by line {}'.format(i))
- entry = _entry.Entry(type=_type, content=[])
- _LOG.info(stack)
- if entry is not None:
- entry.content.append(line)
- if line.startswith('END:'):
- _type = line.split(':', 1)[1]
- _LOG.info('{!r}: end {}'.format(self, _type))
- if not stack or _type != stack[-1]:
- raise ValueError(
- ('closing {} on line {}, but current stack is {}'
- ).format(_type, i, stack))
- stack.pop(-1)
- if len(stack) == 1:
- entry.content.append('') # trailing blankline
- entry.content = '\r\n'.join(entry.content)
- entry.process()
- self.add(entry)
- entry = None
-
- def write(self, stream):
- stream.write(self.content)