~cjwatson/brz-loom/rename-python-package

« back to all changes in this revision

Viewing changes to loom_io.py

  • Committer: Jelmer Vernooij
  • Date: 2019-08-22 19:36:40 UTC
  • Revision ID: jelmer@jelmer.uk-20190822193640-y82m61kqdm5fx13r
Add python3 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
# The current format marker for serialised loom state.
28
28
# This belongs in a format object at some point.
29
 
_CURRENT_LOOM_FORMAT_STRING = "Loom current 1"
 
29
_CURRENT_LOOM_FORMAT_STRING = b"Loom current 1"
30
30
 
31
31
# the current loom format :
32
32
# first line is the format signature
43
43
 
44
44
    def write_threads(self, threads, stream):
45
45
        """Write threads to stream with a format header."""
46
 
        thread_content = 'Loom meta 1\n'
 
46
        thread_content = b'Loom meta 1\n'
47
47
        for thread, rev_id in threads:
48
 
            thread_content += '%s %s\n' % (rev_id, thread)
49
 
        thread_content = thread_content.encode('utf8')
 
48
            thread_content += b'%s %s\n' % (rev_id, thread.encode('utf-8'))
 
49
        thread_content = thread_content
50
50
        stream.write(thread_content)
51
51
        return breezy.osutils.sha_strings([thread_content])
52
52
 
63
63
 
64
64
    def write(self, stream):
65
65
        """Write the state object to stream."""
66
 
        lines = [_CURRENT_LOOM_FORMAT_STRING + '\n']
67
 
        lines.append(' '.join(self._state.get_parents()) + '\n')
 
66
        lines = [_CURRENT_LOOM_FORMAT_STRING + b'\n']
 
67
        lines.append(b' '.join(self._state.get_parents()) + b'\n')
68
68
        # Note that we could possibly optimise our unicode handling here.
69
69
        for thread, rev_id, parents in self._state.get_threads():
70
70
            assert len(parents) == len(self._state.get_parents())
71
71
            # leading space for conflict status
72
 
            line = " "
 
72
            line = b" "
73
73
            for parent in parents:
74
74
                if parent is not None:
75
 
                    line += "%s " % parent.decode('utf8')
 
75
                    line += b"%s " % parent
76
76
                else:
77
 
                    line += " "
78
 
            line += ": "
79
 
            lines.append('%s%s %s\n' % (line, rev_id.decode('utf8'), thread))
80
 
        stream.write(''.join(lines).encode('utf8'))
 
77
                    line += b" "
 
78
            line += b": "
 
79
            lines.append(b'%s%s %s\n' % (line, rev_id, thread.encode('utf-8')))
 
80
        stream.write(b''.join(lines))
81
81
 
82
82
 
83
83
class LoomStateReader(object):
101
101
        if self._content is None:
102
102
            # Names are unicode,revids are utf8 - it's arguable whether decode
103
103
            # all and encode revids, or vice verca is better.
104
 
            self._content = self._stream.read().decode('utf8').split('\n')
 
104
            self._content = self._stream.read().split(b'\n')
105
105
            # this is where detection of different formats should go.
106
106
            # we probably want either a  factory for readers, or a strategy
107
107
            # for the reader that is looked up on this format string.
108
108
            # either way, its in the future.
109
 
            assert self._content[0] == _CURRENT_LOOM_FORMAT_STRING 
 
109
            assert self._content[0] == _CURRENT_LOOM_FORMAT_STRING, \
 
110
                    "%r != %r" % (self._content[0], _CURRENT_LOOM_FORMAT_STRING)
110
111
 
111
112
    def read_parents(self):
112
113
        """Read the parents field from the stream.
114
115
        :return: a list of parent revision ids.
115
116
        """
116
117
        self._read()
117
 
        return self._content[1].encode('utf8').split()
 
118
        return self._content[1].split()
118
119
 
119
120
    def read_thread_details(self):
120
121
        """Read the details for the threads.
128
129
        """
129
130
        result = []
130
131
        parent_count = len(self.read_parents())
131
 
        split_count = parent_count + 2
132
132
        # skip the format and parent lines, and the trailing \n line.
133
133
        for line in self._content[2:-1]:
134
 
            conflict_status, line = line.split(' ', 1)
 
134
            conflict_status, line = line.split(b' ', 1)
135
135
            parents = []
136
 
            parent = ""
 
136
            parent = b""
137
137
            while True:
138
 
                parent, line = line.split(' ', 1)
139
 
                if parent == ':':
 
138
                parent, line = line.split(b' ', 1)
 
139
                if parent == b':':
140
140
                    break
141
 
                elif parent == '':
 
141
                elif parent == b'':
142
142
                    parents.append(None)
143
143
                else:
144
 
                    parents.append(parent.encode('utf8'))
145
 
            rev_id, name = line.split(' ', 1)
146
 
            result.append((name, rev_id.encode('utf8'), parents))
 
144
                    parents.append(parent)
 
145
            rev_id, name = line.split(b' ', 1)
 
146
            result.append((name.decode('utf-8'), rev_id, parents))
147
147
        return result