Chromium Code Reviews| 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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 | 101 |
| 102 # Add init | 102 # Add init |
| 103 dictionary['__init__'] = _StructInit(fields) | 103 dictionary['__init__'] = _StructInit(fields) |
| 104 | 104 |
| 105 # Add serialization method | 105 # Add serialization method |
| 106 serialization_object = serialization.Serialization(groups) | 106 serialization_object = serialization.Serialization(groups) |
| 107 def Serialize(self, handle_offset=0): | 107 def Serialize(self, handle_offset=0): |
| 108 return serialization_object.Serialize(self, handle_offset) | 108 return serialization_object.Serialize(self, handle_offset) |
| 109 dictionary['Serialize'] = Serialize | 109 dictionary['Serialize'] = Serialize |
| 110 | 110 |
| 111 dictionary['GetS'] = classmethod(lambda s: serialization_object) | |
|
qsr
2015/07/16 09:35:25
What is this? I didn't find where it was used? By
azani
2015/07/16 21:57:38
Sorry, that was here for debugging.
| |
| 112 | |
| 111 # pylint: disable=W0212 | 113 # pylint: disable=W0212 |
| 112 def AsDict(self): | 114 def AsDict(self): |
| 113 return self._fields | 115 return self._fields |
| 114 dictionary['AsDict'] = AsDict | 116 dictionary['AsDict'] = AsDict |
| 115 | 117 |
| 116 def Deserialize(cls, context): | 118 def Deserialize(cls, context): |
| 117 result = cls.__new__(cls) | 119 result = cls.__new__(cls) |
| 118 fields = {} | 120 fields = {} |
| 119 serialization_object.Deserialize(fields, context) | 121 serialization_object.Deserialize(fields, context) |
| 120 result._fields = fields | 122 result._fields = fields |
| 121 return result | 123 return result |
| 122 dictionary['Deserialize'] = classmethod(Deserialize) | 124 dictionary['Deserialize'] = classmethod(Deserialize) |
| 123 | 125 |
| 124 dictionary['__eq__'] = _StructEq(fields) | 126 dictionary['__eq__'] = _StructEq(fields) |
| 125 dictionary['__ne__'] = _StructNe | 127 dictionary['__ne__'] = _StructNe |
| 126 | 128 |
| 127 return type.__new__(mcs, name, bases, dictionary) | 129 return type.__new__(mcs, name, bases, dictionary) |
| 128 | 130 |
| 129 # Prevent adding new attributes, or mutating constants. | 131 # Prevent adding new attributes, or mutating constants. |
| 130 def __setattr__(cls, key, value): | 132 def __setattr__(cls, key, value): |
| 131 raise AttributeError('can\'t set attribute') | 133 raise AttributeError('can\'t set attribute') |
| 132 | 134 |
| 133 # Prevent deleting constants. | 135 # Prevent deleting constants. |
| 134 def __delattr__(cls, key): | 136 def __delattr__(cls, key): |
| 135 raise AttributeError('can\'t delete attribute') | 137 raise AttributeError('can\'t delete attribute') |
| 136 | 138 |
| 137 | 139 |
| 140 class MojoUnionType(type): | |
| 141 | |
| 142 def __new__(mcs, name, bases, dictionary): | |
| 143 dictionary['__slots__'] = ('_fields', '_tags', '_tag', '_data') | |
| 144 descriptor = dictionary.pop('DESCRIPTOR', {}) | |
| 145 | |
| 146 fields = descriptor.get('fields', []) | |
| 147 def _BuildUnionProperty(field): | |
| 148 | |
| 149 # pylint: disable=W0212 | |
| 150 def Get(self): | |
| 151 if self._tag != self._tags[field.name]: | |
|
qsr
2015/07/16 09:35:25
I might be wrong, but I think you do not need to p
azani
2015/07/16 21:57:38
Done.
| |
| 152 raise AttributeError('%s is not currently set' % field.name, | |
| 153 field.name, self._tag_names[self._tag]) | |
| 154 return self._data | |
| 155 | |
| 156 # pylint: disable=W0212 | |
| 157 def Set(self, value): | |
| 158 self._tag = self._tags[field.name] | |
| 159 self._data = field.field_type.Convert(value) | |
| 160 | |
| 161 return property(Get, Set) | |
| 162 | |
| 163 for field in fields: | |
| 164 dictionary[field.name] = _BuildUnionProperty(field) | |
| 165 | |
| 166 dictionary['_tags'] = {field.name: field.index for field in fields} | |
| 167 dictionary['_tag_names'] = {field.index: field.name for field in fields} | |
| 168 | |
| 169 def UnionInit(self, **kwargs): | |
| 170 items = kwargs.items() | |
| 171 if len(items) == 0: | |
| 172 return | |
| 173 | |
| 174 if len(items) > 1: | |
| 175 raise TypeError('only 1 member may be set on a union.') | |
| 176 | |
| 177 setattr(self, items[0][0], items[0][1]) | |
| 178 dictionary['__init__'] = UnionInit | |
| 179 | |
| 180 serializer = serialization.UnionSerializer(fields) | |
| 181 def SerializeUnionInline(self, handle_offset=0): | |
| 182 return serializer.SerializeInline(self, handle_offset) | |
| 183 dictionary['SerializeInline'] = SerializeUnionInline | |
| 184 | |
| 185 def SerializeUnion(self, handle_offset=0): | |
| 186 return serializer.Serialize(self, handle_offset) | |
| 187 dictionary['Serialize'] = SerializeUnion | |
| 188 | |
| 189 def DeserializeUnion(cls, context): | |
| 190 return serializer.Deserialize(context, cls) | |
| 191 dictionary['Deserialize'] = classmethod(DeserializeUnion) | |
| 192 | |
| 193 def GetTag(self): | |
| 194 return self._tag | |
| 195 dictionary['tag'] = property(GetTag, None) | |
|
qsr
2015/07/16 09:35:25
What can I compare tag with? I think you are missi
azani
2015/07/16 21:57:38
Done.
| |
| 196 | |
| 197 def GetData(self): | |
| 198 return self._data | |
| 199 dictionary['data'] = property(GetData, None) | |
| 200 | |
| 201 def UnionEq(self, other): | |
| 202 return ( | |
| 203 (type(self) is type(other)) | |
| 204 and (self.tag == other.tag) | |
| 205 and (self.data == other.data)) | |
| 206 dictionary['__eq__'] = UnionEq | |
| 207 | |
| 208 def UnionNe(self, other): | |
| 209 return not self.__eq__(other) | |
| 210 dictionary['__ne__'] = UnionNe | |
| 211 | |
| 212 def UnionStr(self): | |
| 213 return '<%s.%s(%s): %s>' % ( | |
| 214 self.__class__.__name__, | |
| 215 self._tag_names[self.tag], | |
| 216 self.tag, | |
| 217 self.data) | |
| 218 dictionary['__str__'] = UnionStr | |
| 219 dictionary['__repr__'] = UnionStr | |
| 220 | |
| 221 return type.__new__(mcs, name, bases, dictionary) | |
| 222 | |
| 223 | |
| 224 class UnionData(object): | |
| 225 __slots__ = ('data', 'tag') | |
|
qsr
2015/07/16 09:35:25
What is this used for.
azani
2015/07/16 21:57:38
Sorry, left over from previous iteration.
| |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 138 class InterfaceRequest(object): | 230 class InterfaceRequest(object): |
| 139 """ | 231 """ |
| 140 An interface request allows to send a request for an interface to a remote | 232 An interface request allows to send a request for an interface to a remote |
| 141 object and start using it immediately. | 233 object and start using it immediately. |
| 142 """ | 234 """ |
| 143 | 235 |
| 144 def __init__(self, handle): | 236 def __init__(self, handle): |
| 145 self._handle = handle | 237 self._handle = handle |
| 146 | 238 |
| 147 def IsPending(self): | 239 def IsPending(self): |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 if type(self) is not type(other): | 296 if type(self) is not type(other): |
| 205 return False | 297 return False |
| 206 for field in fields: | 298 for field in fields: |
| 207 if getattr(self, field.name) != getattr(other, field.name): | 299 if getattr(self, field.name) != getattr(other, field.name): |
| 208 return False | 300 return False |
| 209 return True | 301 return True |
| 210 return _Eq | 302 return _Eq |
| 211 | 303 |
| 212 def _StructNe(self, other): | 304 def _StructNe(self, other): |
| 213 return not self.__eq__(other) | 305 return not self.__eq__(other) |
| OLD | NEW |