~ubuntu-branches/debian/jessie/subunit/jessie

« back to all changes in this revision

Viewing changes to python/subunit/details.py

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2011-10-31 02:02:11 UTC
  • mfrom: (1.1.6) (3.2.8 sid)
  • Revision ID: package-import@ubuntu.com-20111031020211-wz4nxhqiw8k23xsj
Tags: 0.0.7-1
* New upstream release.
* Add support for multi-arch.
 + Switch to debhelper >= 8.1.3, compat level 9.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Handlers for outcome details."""
18
18
 
19
 
from cStringIO import StringIO
20
 
 
21
19
from testtools import content, content_type
22
 
 
23
 
import chunked
 
20
from testtools.compat import _b, BytesIO
 
21
 
 
22
from subunit import chunked
 
23
 
 
24
end_marker = _b("]\n")
 
25
quoted_marker = _b(" ]")
 
26
empty = _b('')
24
27
 
25
28
 
26
29
class DetailsParser(object):
31
34
    """Parser for single-part [] delimited details."""
32
35
 
33
36
    def __init__(self, state):
34
 
        self._message = ""
 
37
        self._message = _b("")
35
38
        self._state = state
36
39
 
37
40
    def lineReceived(self, line):
38
 
        if line == "]\n":
 
41
        if line == end_marker:
39
42
            self._state.endDetails()
40
43
            return
41
 
        if line[0:2] == " ]":
 
44
        if line[0:2] == quoted_marker:
42
45
            # quoted ] start
43
46
            self._message += line[1:]
44
47
        else:
77
80
        self._parse_state = self._look_for_content
78
81
 
79
82
    def _look_for_content(self, line):
80
 
        if line == "]\n":
 
83
        if line == end_marker:
81
84
            self._state.endDetails()
82
85
            return
83
86
        # TODO error handling
84
 
        field, value = line[:-1].split(' ', 1)
85
 
        main, sub = value.split('/')
 
87
        field, value = line[:-1].decode('utf8').split(' ', 1)
 
88
        try:
 
89
            main, sub = value.split('/')
 
90
        except ValueError:
 
91
            raise ValueError("Invalid MIME type %r" % value)
86
92
        self._content_type = content_type.ContentType(main, sub)
87
93
        self._parse_state = self._get_name
88
94
 
89
95
    def _get_name(self, line):
90
 
        self._name = line[:-1]
91
 
        self._body = StringIO()
 
96
        self._name = line[:-1].decode('utf8')
 
97
        self._body = BytesIO()
92
98
        self._chunk_parser = chunked.Decoder(self._body)
93
99
        self._parse_state = self._feed_chunks
94
100
 
96
102
        residue = self._chunk_parser.write(line)
97
103
        if residue is not None:
98
104
            # Line based use always ends on no residue.
99
 
            assert residue == '', 'residue: %r' % (residue,)
 
105
            assert residue == empty, 'residue: %r' % (residue,)
100
106
            body = self._body
101
107
            self._details[self._name] = content.Content(
102
108
                self._content_type, lambda:[body.getvalue()])