rtkit.parser

class rtkit.parser.RTParser[source]

Bases: object

RFC5322 Parser - see https://tools.ietf.org/html/rfc5322

COMMENT = <_sre.SRE_Pattern object>
HEADER = <_sre.SRE_Pattern object>
SECTION = <_sre.SRE_Pattern object>
SYNTAX_COMMENT = <_sre.SRE_Pattern object>
classmethod build(body)[source]

Build logical lines from a RFC5322-like string

Returns:A list of strings
>>> body = '''RT/1.2.3 200 Ok
...
... # a
...   b
... spam: 1
...
... ham: 2,
...     3
... --
... # c
... spam: 4
... ham:
... --
... a -- b
... '''
>>> RTParser.build(body)
[[u'# a\nb', u'spam: 1', u'ham: 2,\n3'], [u'# c', u'spam: 4', u'ham:'], [u'a -- b']]
classmethod decode(lines)[source]
Returns:A list of 2-tuples parsing ‘k: v’ and skipping comments
>>> RTParser.decode(['# c1: c2', 'spam: 1', 'ham: 2, 3', 'eggs:'])
[('spam', '1'), ('ham', '2, 3'), ('eggs', '')]
>>> RTParser.decode(['<!DOCTYPE HTML PUBLIC >', '<html><head>',])
[]
classmethod decode_comment(lines)[source]
Returns:A list of 2-tuples parsing ‘# k: v’
>>> RTParser.decode_comment(['# c1: c2', 'spam: 1', 'ham: 2, 3', 'eggs:'])
[('c1', 'c2')]
>>> RTParser.decode_comment(['# Syntax error.', '>> c1: c2', 'ham: 2, 3', 'eggs:'])
[('c1', 'c2')]
classmethod parse(body, decoder)[source]
Returns:A list of RFC5322-like section
>>> decode = RTParser.decode
>>> body = '''
...
... # c1
... spam: 1
... ham: 2,
...     3
... eggs:'''
>>> RTParser.parse(body, decode)
[[('spam', '1'), ('ham', '2,\n3'), ('eggs', '')]]
>>> RTParser.parse('# spam 1 does not exist.', decode)
Traceback (most recent call last):
...
RTNotFoundError: spam 1 does not exist
>>> RTParser.parse('# Spam 1 created.', decode)
[[('id', 'spam/1')]]
>>> RTParser.parse('No matching results.', decode)
[]
>>> decode = RTParser.decode_comment
>>> RTParser.parse('# spam: 1\n# ham: 2', decode)
[[('spam', '1'), ('ham', '2')]]