| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -72,6 +72,8 @@ class RTResponse(object): | |||
| 72 | 72 | HEADER = re.compile(HEADER_PATTERN) | |
| 73 | 73 | COMMENT_PATTERN = '^#\s+.+$' | |
| 74 | 74 | COMMENT = re.compile(COMMENT_PATTERN) | |
| 75 | + SECTION_PATTERN = '^--' | ||
| 76 | + SECTION = re.compile(SECTION_PATTERN, re.M|re.U) | ||
| 75 | 77 | ||
| 76 | 78 | def __init__(self, request, response): | |
| 77 | 79 | self.headers = response.headers | |
@@ -106,15 +108,15 @@ def __init__(self, request, response): | |||
| 106 | 108 | ||
| 107 | 109 | @classmethod | |
| 108 | 110 | def _parse(cls, body, decoder): | |
| 109 | - r'''Return a list of RFC5322-like section | ||
| 111 | + r""" Return a list of RFC5322-like section | ||
| 110 | 112 | >>> decode = RTResponse._decode | |
| 111 | - >>> body = """ | ||
| 113 | + >>> body = ''' | ||
| 112 | 114 | ... | |
| 113 | 115 | ... # c1 | |
| 114 | 116 | ... spam: 1 | |
| 115 | 117 | ... ham: 2, | |
| 116 | 118 | ... 3 | |
| 117 | - ... eggs:""" | ||
| 119 | + ... eggs:''' | ||
| 118 | 120 | >>> RTResponse._parse(body, decode) | |
| 119 | 121 | [[('spam', '1'), ('ham', '2, 3'), ('eggs', '')]] | |
| 120 | 122 | >>> RTResponse._parse('# spam 1 does not exist.', decode) | |
@@ -128,7 +130,7 @@ def _parse(cls, body, decoder): | |||
| 128 | 130 | >>> decode = RTResponse._decode_comment | |
| 129 | 131 | >>> RTResponse._parse('# spam: 1\n# ham: 2', decode) | |
| 130 | 132 | [[('spam', '1'), ('ham', '2')]] | |
| 131 | - ''' | ||
| 133 | + """ | ||
| 132 | 134 | section = cls._build(body) | |
| 133 | 135 | if len(section) == 1: | |
| 134 | 136 | try: | |
@@ -141,12 +143,12 @@ def _parse(cls, body, decoder): | |||
| 141 | 143 | ||
| 142 | 144 | @classmethod | |
| 143 | 145 | 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 | ||
| 145 | 147 | >>> RTResponse._decode(['# c1: c2', 'spam: 1', 'ham: 2, 3', 'eggs:']) | |
| 146 | 148 | [('spam', '1'), ('ham', '2, 3'), ('eggs', '')] | |
| 147 | 149 | >>> RTResponse._decode(['<!DOCTYPE HTML PUBLIC >', '<html><head>',]) | |
| 148 | 150 | [] | |
| 149 | - ''' | ||
| 151 | + """ | ||
| 150 | 152 | try: | |
| 151 | 153 | lines = ifilterfalse(cls.COMMENT.match, lines) | |
| 152 | 154 | return [(k, v.strip(' ')) | |
@@ -157,35 +159,37 @@ def _decode(cls, lines): | |||
| 157 | 159 | ||
| 158 | 160 | @classmethod | |
| 159 | 161 | 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' | ||
| 161 | 163 | >>> RTResponse._decode_comment(['# c1: c2', 'spam: 1', 'ham: 2, 3', 'eggs:']) | |
| 162 | 164 | [('c1', 'c2')] | |
| 163 | 165 | >>> | |
| 164 | - ''' | ||
| 166 | + """ | ||
| 165 | 167 | lines = filter(cls.COMMENT.match, lines) | |
| 166 | 168 | return [(k.strip('# '), v.strip(' ')) | |
| 167 | 169 | for k,v in [l.split(':', 1) | |
| 168 | 170 | for l in lines]] | |
| 169 | 171 | ||
| 170 | 172 | @classmethod | |
| 171 | 173 | 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 | ||
| 174 | 176 | ... | |
| 175 | 177 | ... # a | |
| 176 | 178 | ... b | |
| 177 | 179 | ... spam: 1 | |
| 178 | - ... | ||
| 180 | + ... | ||
| 179 | 181 | ... ham: 2, | |
| 180 | 182 | ... 3 | |
| 181 | 183 | ... -- | |
| 182 | 184 | ... # c | |
| 183 | 185 | ... spam: 4 | |
| 184 | 186 | ... ham: | |
| 185 | - ... """ | ||
| 187 | + ... -- | ||
| 188 | + ... a -- b | ||
| 189 | + ... ''' | ||
| 186 | 190 | >>> 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 | + """ | ||
| 189 | 193 | def build_section(section): | |
| 190 | 194 | logic_lines = [] | |
| 191 | 195 | for line in filter(None, section.splitlines()): | |
@@ -196,4 +200,4 @@ def build_section(section): | |||
| 196 | 200 | else: | |
| 197 | 201 | logic_lines.append(line) | |
| 198 | 202 | 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)] | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments