OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """The metaclasses used by the mojo python bindings.""" | 5 """The metaclasses used by the mojo python bindings.""" |
6 | 6 |
7 import itertools | 7 import itertools |
8 | 8 |
9 # pylint: disable=F0401 | 9 # pylint: disable=F0401 |
10 import mojo.bindings.serialization as serialization | 10 import mojo.bindings.serialization as serialization |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 100 |
101 # Add init | 101 # Add init |
102 dictionary['__init__'] = _StructInit | 102 dictionary['__init__'] = _StructInit |
103 | 103 |
104 # Add serialization method | 104 # Add serialization method |
105 serialization_object = serialization.Serialization(groups) | 105 serialization_object = serialization.Serialization(groups) |
106 def Serialize(self, handle_offset=0): | 106 def Serialize(self, handle_offset=0): |
107 return serialization_object.Serialize(self, handle_offset) | 107 return serialization_object.Serialize(self, handle_offset) |
108 dictionary['Serialize'] = Serialize | 108 dictionary['Serialize'] = Serialize |
109 | 109 |
| 110 def Deserialize(cls, data, handles): |
| 111 result = cls.__new__(cls) |
| 112 fields = {} |
| 113 serialization_object.Deserialize(fields, data, handles) |
| 114 result._fields = fields |
| 115 return result |
| 116 dictionary['Deserialize'] = classmethod(Deserialize) |
| 117 |
110 return type.__new__(mcs, name, bases, dictionary) | 118 return type.__new__(mcs, name, bases, dictionary) |
111 | 119 |
112 # Prevent adding new attributes, or mutating constants. | 120 # Prevent adding new attributes, or mutating constants. |
113 def __setattr__(mcs, key, value): | 121 def __setattr__(mcs, key, value): |
114 raise AttributeError, 'can\'t set attribute' | 122 raise AttributeError, 'can\'t set attribute' |
115 | 123 |
116 # Prevent deleting constants. | 124 # Prevent deleting constants. |
117 def __delattr__(mcs, key): | 125 def __delattr__(mcs, key): |
118 raise AttributeError, 'can\'t delete attribute' | 126 raise AttributeError, 'can\'t delete attribute' |
119 | 127 |
(...skipping 11 matching lines...) Expand all Loading... |
131 def Get(self): | 139 def Get(self): |
132 if field.name not in self._fields: | 140 if field.name not in self._fields: |
133 self._fields[field.name] = field.GetDefaultValue() | 141 self._fields[field.name] = field.GetDefaultValue() |
134 return self._fields[field.name] | 142 return self._fields[field.name] |
135 | 143 |
136 # pylint: disable=W0212 | 144 # pylint: disable=W0212 |
137 def Set(self, value): | 145 def Set(self, value): |
138 self._fields[field.name] = field.field_type.Convert(value) | 146 self._fields[field.name] = field.field_type.Convert(value) |
139 | 147 |
140 return property(Get, Set) | 148 return property(Get, Set) |
OLD | NEW |