FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

This commit closes #3. · T0MASD/python-rtkit@fdab397 · GitHub

Repository navigation

Commit fdab397

Browse files
committed
This commit closes z4r#3.
If '--' is not at the beginning of a line, it does not represent a valid splitting token.
1 parent 8a4599b commit fdab397

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

‎rtkit/resource.py‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class RTResponse(object):
7272
HEADER = re.compile(HEADER_PATTERN)
7373
COMMENT_PATTERN = '^#\s+.+$'
7474
COMMENT = re.compile(COMMENT_PATTERN)
75+
SECTION_PATTERN = '^--'
76+
SECTION = re.compile(SECTION_PATTERN, re.M|re.U)
7577

7678
def __init__(self, request, response):
7779
self.headers = response.headers
@@ -106,15 +108,15 @@ def __init__(self, request, response):
106108

107109
@classmethod
108110
def _parse(cls, body, decoder):
109-
r'''Return a list of RFC5322-like section
111+
r""" Return a list of RFC5322-like section
110112
>>> decode = RTResponse._decode
111-
>>> body = """
113+
>>> body = '''
112114
...
113115
... # c1
114116
... spam: 1
115117
... ham: 2,
116118
... 3
117-
... eggs:"""
119+
... eggs:'''
118120
>>> RTResponse._parse(body, decode)
119121
[[('spam', '1'), ('ham', '2, 3'), ('eggs', '')]]
120122
>>> RTResponse._parse('# spam 1 does not exist.', decode)
@@ -128,7 +130,7 @@ def _parse(cls, body, decoder):
128130
>>> decode = RTResponse._decode_comment
129131
>>> RTResponse._parse('# spam: 1\n# ham: 2', decode)
130132
[[('spam', '1'), ('ham', '2')]]
131-
'''
133+
"""
132134
section = cls._build(body)
133135
if len(section) == 1:
134136
try:
@@ -141,12 +143,12 @@ def _parse(cls, body, decoder):
141143

142144
@classmethod
143145
def _decode(cls, lines):
144-
'''Return a list of 2-tuples parsing 'k: v' and skipping comments
146+
""" Return a list of 2-tuples parsing 'k: v' and skipping comments
145147
>>> RTResponse._decode(['# c1: c2', 'spam: 1', 'ham: 2, 3', 'eggs:'])
146148
[('spam', '1'), ('ham', '2, 3'), ('eggs', '')]
147149
>>> RTResponse._decode(['<!DOCTYPE HTML PUBLIC >', '<html><head>',])
148150
[]
149-
'''
151+
"""
150152
try:
151153
lines = ifilterfalse(cls.COMMENT.match, lines)
152154
return [(k, v.strip(' '))
@@ -157,35 +159,37 @@ def _decode(cls, lines):
157159

158160
@classmethod
159161
def _decode_comment(cls, lines):
160-
'''Return a list of 2-tuples parsing '# k: v'
162+
""" Return a list of 2-tuples parsing '# k: v'
161163
>>> RTResponse._decode_comment(['# c1: c2', 'spam: 1', 'ham: 2, 3', 'eggs:'])
162164
[('c1', 'c2')]
163165
>>>
164-
'''
166+
"""
165167
lines = filter(cls.COMMENT.match, lines)
166168
return [(k.strip('# '), v.strip(' '))
167169
for k,v in [l.split(':', 1)
168170
for l in lines]]
169171

170172
@classmethod
171173
def _build(cls, body):
172-
'''Build logical lines from a RFC5322-like string
173-
>>> body = """RT/1.2.3 200 Ok
174+
""" Build logical lines from a RFC5322-like string
175+
>>> body = '''RT/1.2.3 200 Ok
174176
...
175177
... # a
176178
... b
177179
... spam: 1
178-
...
180+
...
179181
... ham: 2,
180182
... 3
181183
... --
182184
... # c
183185
... spam: 4
184186
... ham:
185-
... """
187+
... --
188+
... a -- b
189+
... '''
186190
>>> RTResponse._build(body)
187-
[['# a b', 'spam: 1', 'ham: 2, 3'], ['# c', 'spam: 4', 'ham:']]
188-
'''
191+
[['# a b', 'spam: 1', 'ham: 2, 3'], ['# c', 'spam: 4', 'ham:'], ['a -- b']]
192+
"""
189193
def build_section(section):
190194
logic_lines = []
191195
for line in filter(None, section.splitlines()):
@@ -196,4 +200,4 @@ def build_section(section):
196200
else:
197201
logic_lines.append(line)
198202
return logic_lines
199-
return [build_section(b) for b in body.split('--')]
203+
return [build_section(b) for b in cls.SECTION.split(body)]

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL