~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/chk_serializer.py

  • Committer: John Arbash Meinel
  • Date: 2009-06-04 16:50:33 UTC
  • mto: This revision was merged to the branch mainline in revision 4410.
  • Revision ID: john@arbash-meinel.com-20090604165033-bfdo0lyf4yt4vjcz
We don't need a base Coder class, because Decoder._update_tail is different than Encoder._update_tail.
(one adds, one subtracts from self.size).
So we now have 2 versions of the macro, and the test suite stops crashing... :)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Serializer object for CHK based inventory storage."""
18
18
 
 
19
from cStringIO import (
 
20
    StringIO,
 
21
    )
 
22
 
19
23
from bzrlib import (
20
24
    bencode,
21
25
    cache_utf8,
22
26
    inventory,
 
27
    osutils,
23
28
    revision as _mod_revision,
 
29
    xml5,
24
30
    xml6,
25
 
    xml7,
26
31
    )
27
32
 
28
33
 
29
34
def _validate_properties(props, _decode=cache_utf8._utf8_decode):
30
35
    # TODO: we really want an 'isascii' check for key
31
 
    # Cast the utf8 properties into Unicode 'in place'
32
 
    for key, value in props.iteritems():
33
 
        props[key] = _decode(value)[0]
34
 
    return props
 
36
    unicode_props = dict([(key, _decode(value)[0])
 
37
                          for key, value in props.iteritems()])
 
38
    return unicode_props
35
39
 
36
40
 
37
41
def _is_format_10(value):
45
49
    """Simple revision serializer based around bencode.
46
50
    """
47
51
 
48
 
    squashes_xml_invalid_characters = False
49
 
 
50
52
    # Maps {key:(Revision attribute, bencode_type, validator)}
51
53
    # This tells us what kind we expect bdecode to create, what variable on
52
54
    # Revision we should be using, and a function to call to validate/transform
58
60
               'timezone': ('timezone', int, None),
59
61
               'timestamp': ('timestamp', str, float),
60
62
               'revision-id': ('revision_id', str, None),
61
 
               'parent-ids': ('parent_ids', list, None),
 
63
               'parent-ids': ('parent_ids', list, tuple),
62
64
               'inventory-sha1': ('inventory_sha1', str, None),
63
65
               'message': ('message', str, cache_utf8.decode),
64
66
               'properties': ('properties', dict, _validate_properties),
104
106
        ret = bencode.bdecode(text)
105
107
        if not isinstance(ret, list):
106
108
            raise ValueError("invalid revision text")
107
 
        schema = self._schema
 
109
        schema = dict(self._schema)
 
110
        schema_pop = schema.pop
108
111
        # timezone is allowed to be missing, but should be set
109
112
        bits = {'timezone': None}
110
113
        for key, value in ret:
111
114
            # Will raise KeyError if not a valid part of the schema, or an
112
115
            # entry is given 2 times.
113
 
            var_name, expected_type, validator = schema[key]
 
116
            var_name, expected_type, validator = schema_pop(key)
114
117
            if value.__class__ is not expected_type:
115
118
                raise ValueError('key %s did not conform to the expected type'
116
119
                                 ' %s, but was %s'
118
121
            if validator is not None:
119
122
                value = validator(value)
120
123
            bits[var_name] = value
121
 
        if len(bits) != len(schema):
122
 
            missing = [key for key, (var_name, _, _) in schema.iteritems()
123
 
                       if var_name not in bits]
124
 
            raise ValueError('Revision text was missing expected keys %s.'
125
 
                             ' text %r' % (missing, text))
126
 
        del bits[None]  # Get rid of 'format' since it doesn't get mapped
 
124
        if schema:
 
125
            if schema.keys() != ['timezone']:
 
126
                raise ValueError('Revision text was missing expected keys %s.'
 
127
                                 ' text %r' % (schema.keys(), text))
 
128
        del bits[None]  # Get rid of bits that don't get mapped
127
129
        rev = _mod_revision.Revision(**bits)
128
130
        return rev
129
131
 
131
133
        return self.read_revision_from_string(f.read())
132
134
 
133
135
 
134
 
class CHKSerializerSubtree(BEncodeRevisionSerializer1, xml7.Serializer_v7):
 
136
class CHKSerializerSubtree(BEncodeRevisionSerializer1, xml6.Serializer_v6):
135
137
    """A CHKInventory based serializer that supports tree references"""
136
138
 
137
139
    supported_kinds = set(['file', 'directory', 'symlink', 'tree-reference'])
139
141
    revision_format_num = None
140
142
    support_altered_by_hack = False
141
143
 
142
 
    def _unpack_entry(self, elt, entry_cache=None, return_from_cache=False):
 
144
    def _unpack_entry(self, elt):
143
145
        kind = elt.tag
144
146
        if not kind in self.supported_kinds:
145
147
            raise AssertionError('unsupported entry kind %s' % kind)
152
154
            return inventory.TreeReference(file_id, name, parent_id, revision,
153
155
                                           reference_revision)
154
156
        else:
155
 
            return xml7.Serializer_v7._unpack_entry(self, elt,
156
 
                entry_cache=entry_cache, return_from_cache=return_from_cache)
 
157
            return xml6.Serializer_v6._unpack_entry(self, elt)
157
158
 
158
159
    def __init__(self, node_size, search_key_name):
159
160
        self.maximum_size = node_size
160
161
        self.search_key_name = search_key_name
161
162
 
162
163
 
163
 
class CHKSerializer(xml6.Serializer_v6):
 
164
class CHKSerializer(xml5.Serializer_v5):
164
165
    """A CHKInventory based serializer with 'plain' behaviour."""
165
166
 
166
167
    format_num = '9'