/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/util/_bencode_py.py

  • Committer: Jelmer Vernooij
  • Date: 2018-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#
16
16
# Modifications copyright (C) 2008 Canonical Ltd
17
17
 
 
18
from __future__ import absolute_import
 
19
 
18
20
import sys
19
21
 
20
22
 
47
49
        f += 1
48
50
        newf = x.index(b'e', f)
49
51
        n = int(x[f:newf])
50
 
        if x[f:f + 2] == b'-0':
51
 
            raise ValueError
52
 
        elif x[f:f + 1] == b'0' and newf != f + 1:
53
 
            raise ValueError
54
 
        return (n, newf + 1)
 
52
        if x[f:f+2] == b'-0':
 
53
            raise ValueError
 
54
        elif x[f:f+1] == b'0' and newf != f+1:
 
55
            raise ValueError
 
56
        return (n, newf+1)
55
57
 
56
58
    def decode_string(self, x, f):
57
59
        colon = x.index(b':', f)
58
60
        n = int(x[f:colon])
59
 
        if x[f:f + 1] == b'0' and colon != f + 1:
 
61
        if x[f:f+1] == b'0' and colon != f+1:
60
62
            raise ValueError
61
63
        colon += 1
62
 
        return (x[colon:colon + n], colon + n)
 
64
        return (x[colon:colon+n], colon+n)
63
65
 
64
66
    def decode_list(self, x, f):
65
 
        r, f = [], f + 1
66
 
        while x[f:f + 1] != b'e':
67
 
            v, f = self.decode_func[x[f:f + 1]](x, f)
 
67
        r, f = [], f+1
 
68
        while x[f:f+1] != b'e':
 
69
            v, f = self.decode_func[x[f:f+1]](x, f)
68
70
            r.append(v)
69
71
        if self.yield_tuples:
70
72
            r = tuple(r)
71
73
        return (r, f + 1)
72
74
 
73
75
    def decode_dict(self, x, f):
74
 
        r, f = {}, f + 1
 
76
        r, f = {}, f+1
75
77
        lastkey = None
76
 
        while x[f:f + 1] != b'e':
 
78
        while x[f:f+1] != b'e':
77
79
            k, f = self.decode_string(x, f)
78
80
            if lastkey is not None and lastkey >= k:
79
81
                raise ValueError
80
82
            lastkey = k
81
 
            r[k], f = self.decode_func[x[f:f + 1]](x, f)
 
83
            r[k], f = self.decode_func[x[f:f+1]](x, f)
82
84
        return (r, f + 1)
83
85
 
84
86
    def bdecode(self, x):
110
112
def encode_bencached(x, r):
111
113
    r.append(x.bencoded)
112
114
 
113
 
 
114
115
def encode_bool(x, r):
115
116
    encode_int(int(x), r)
116
117
 
117
 
 
118
118
def encode_int(x, r):
119
119
    r.extend((b'i', int_to_bytes(x), b'e'))
120
120
 
121
 
 
122
121
def encode_string(x, r):
123
122
    r.extend((int_to_bytes(len(x)), b':', x))
124
123
 
125
 
 
126
124
def encode_list(x, r):
127
125
    r.append(b'l')
128
126
    for i in x:
129
127
        encode_func[type(i)](i, r)
130
128
    r.append(b'e')
131
129
 
132
 
 
133
130
def encode_dict(x, r):
134
131
    r.append(b'd')
135
132
    ilist = sorted(x.items())
138
135
        encode_func[type(v)](v, r)
139
136
    r.append(b'e')
140
137
 
141
 
 
142
138
encode_func = {}
143
139
encode_func[type(Bencached(0))] = encode_bencached
144
140
encode_func[int] = encode_int
145
 
def int_to_bytes(n):
146
 
    return b'%d' % n
 
141
if sys.version_info < (3,):
 
142
    encode_func[long] = encode_int
 
143
    int_to_bytes = str
 
144
else:
 
145
    def int_to_bytes(n):
 
146
        return b'%d' % n
147
147
encode_func[bytes] = encode_string
148
148
encode_func[list] = encode_list
149
149
encode_func[tuple] = encode_list
164
164
    r = []
165
165
    encode_func[type(x)](x, r)
166
166
    return b''.join(r)
 
167