33
33
_decode_utf8 = cache_utf8.decode
36
def _validate_properties(props):
36
def _validate_properties(props, _decode=_decode_utf8):
38
37
# TODO: we really want an 'isascii' check for key
39
unicode_props = dict((key, _decode_utf8(value))
40
for key, value in props.iteritems())
38
unicode_props = dict([(key, _decode(value))
39
for key, value in props.iteritems()])
41
40
return unicode_props
101
100
def read_revision_from_string(self, text):
102
101
# TODO: consider writing a Revision decoder, rather than using the
103
102
# generic bencode decoder
104
decode_utf8 = cache_utf8.decode
105
103
ret = bencode.bdecode(text)
106
104
if not isinstance(ret, list):
107
105
raise ValueError("invalid revision text")
108
106
schema = dict(self._schema)
107
schema_pop = schema.pop
108
# timezone is allowed to be missing, but should be set
109
bits = {'timezone': None}
110
110
for key, value in ret:
111
# The entry must be present in allowed keys, but only present a
113
var_name, expected_type, validator = schema.pop(key)
111
# Will raise KeyError if not a valid part of the schema, or an
112
# entry is given 2 times.
113
var_name, expected_type, validator = schema_pop(key)
114
114
if value.__class__ is not expected_type:
115
115
raise ValueError('key %s did not conform to the expected type'
116
116
' %s, but was %s'
117
117
% (key, expected_type, type(value)))
118
118
if validator is not None:
119
119
value = validator(value)
120
if var_name is not None:
121
bits[var_name] = value
122
if schema.keys() not in ([], ['timezone']):
123
raise ValueError('Revision text was missing expected keys %s,'
124
' text %r' % (schema.keys(), text))
120
bits[var_name] = value
122
if schema.keys() != ['timezone']:
123
raise ValueError('Revision text was missing expected keys %s.'
124
' text %r' % (schema.keys(), text))
125
del bits[None] # Get rid of bits that don't get mapped
125
126
rev = _mod_revision.Revision(**bits)
126
if "timezone" not in bits:
130
129
def read_revision(self, f):