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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 | 128 |
| 129 # Prevent adding new attributes, or mutating constants. | 129 # Prevent adding new attributes, or mutating constants. |
| 130 def __setattr__(cls, key, value): | 130 def __setattr__(cls, key, value): |
| 131 raise AttributeError('can\'t set attribute') | 131 raise AttributeError('can\'t set attribute') |
| 132 | 132 |
| 133 # Prevent deleting constants. | 133 # Prevent deleting constants. |
| 134 def __delattr__(cls, key): | 134 def __delattr__(cls, key): |
| 135 raise AttributeError('can\'t delete attribute') | 135 raise AttributeError('can\'t delete attribute') |
| 136 | 136 |
| 137 | 137 |
| 138 class MojoUnionType(type): | |
| 139 | |
| 140 def __new__(mcs, name, bases, dictionary): | |
| 141 dictionary['__slots__'] = ('_fields', '_tag', '_data') | |
|
qsr
2015/07/17 10:53:58
Instead of keeping the _tag, you can keep a refere
azani
2015/07/17 20:38:15
Done.
| |
| 142 descriptor = dictionary.pop('DESCRIPTOR', {}) | |
| 143 | |
| 144 fields = descriptor.get('fields', []) | |
| 145 tag_names = {field.index: field.name for field in fields} | |
| 146 tags = {field.name: field.index for field in fields} | |
| 147 def _BuildUnionProperty(field): | |
| 148 | |
| 149 # pylint: disable=W0212 | |
| 150 def Get(self): | |
| 151 if self._tag != tags[field.name]: | |
| 152 raise AttributeError('%s is not currently set' % field.name, | |
| 153 field.name, tag_names[self._tag]) | |
|
qsr
2015/07/17 10:53:58
What do the 2nd and 3rd arguments do?
azani
2015/07/17 20:38:15
All the arguments are printed when you print the A
| |
| 154 return self._data | |
| 155 | |
| 156 # pylint: disable=W0212 | |
| 157 def Set(self, value): | |
| 158 self._tag = tags[field.name] | |
|
qsr
2015/07/17 10:53:58
Why is tags[field.name] different than field.index
azani
2015/07/17 20:38:15
Done.
| |
| 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 def UnionInit(self, **kwargs): | |
| 167 items = kwargs.items() | |
| 168 if len(items) == 0: | |
| 169 return | |
| 170 | |
| 171 if len(items) > 1: | |
| 172 raise TypeError('only 1 member may be set on a union.') | |
| 173 | |
| 174 setattr(self, items[0][0], items[0][1]) | |
| 175 dictionary['__init__'] = UnionInit | |
| 176 | |
| 177 serializer = serialization.UnionSerializer(fields) | |
| 178 def SerializeUnionInline(self, handle_offset=0): | |
| 179 return serializer.SerializeInline(self, handle_offset) | |
| 180 dictionary['SerializeInline'] = SerializeUnionInline | |
| 181 | |
| 182 def SerializeUnion(self, handle_offset=0): | |
| 183 return serializer.Serialize(self, handle_offset) | |
| 184 dictionary['Serialize'] = SerializeUnion | |
| 185 | |
| 186 def DeserializeUnion(cls, context): | |
| 187 return serializer.Deserialize(context, cls) | |
| 188 dictionary['Deserialize'] = classmethod(DeserializeUnion) | |
| 189 | |
| 190 class Tags(object): | |
| 191 __metaclass__ = MojoEnumType | |
| 192 VALUES = [(field.name, field.index) for field in fields] | |
| 193 dictionary['Tags'] = Tags | |
| 194 | |
| 195 def GetTag(self): | |
| 196 return self._tag | |
| 197 dictionary['tag'] = property(GetTag, None) | |
| 198 | |
| 199 def GetData(self): | |
| 200 return self._data | |
| 201 dictionary['data'] = property(GetData, None) | |
| 202 | |
| 203 def UnionEq(self, other): | |
| 204 return ( | |
| 205 (type(self) is type(other)) | |
| 206 and (self.tag == other.tag) | |
| 207 and (self.data == other.data)) | |
| 208 dictionary['__eq__'] = UnionEq | |
| 209 | |
| 210 def UnionNe(self, other): | |
| 211 return not self.__eq__(other) | |
| 212 dictionary['__ne__'] = UnionNe | |
| 213 | |
| 214 def UnionStr(self): | |
| 215 return '<%s.%s(%s): %s>' % ( | |
| 216 self.__class__.__name__, | |
| 217 tag_names[self.tag], | |
| 218 self.tag, | |
| 219 self.data) | |
| 220 dictionary['__str__'] = UnionStr | |
| 221 dictionary['__repr__'] = UnionStr | |
| 222 | |
| 223 return type.__new__(mcs, name, bases, dictionary) | |
| 224 | |
| 225 | |
| 138 class InterfaceRequest(object): | 226 class InterfaceRequest(object): |
| 139 """ | 227 """ |
| 140 An interface request allows to send a request for an interface to a remote | 228 An interface request allows to send a request for an interface to a remote |
| 141 object and start using it immediately. | 229 object and start using it immediately. |
| 142 """ | 230 """ |
| 143 | 231 |
| 144 def __init__(self, handle): | 232 def __init__(self, handle): |
| 145 self._handle = handle | 233 self._handle = handle |
| 146 | 234 |
| 147 def IsPending(self): | 235 def IsPending(self): |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 if type(self) is not type(other): | 292 if type(self) is not type(other): |
| 205 return False | 293 return False |
| 206 for field in fields: | 294 for field in fields: |
| 207 if getattr(self, field.name) != getattr(other, field.name): | 295 if getattr(self, field.name) != getattr(other, field.name): |
| 208 return False | 296 return False |
| 209 return True | 297 return True |
| 210 return _Eq | 298 return _Eq |
| 211 | 299 |
| 212 def _StructNe(self, other): | 300 def _StructNe(self, other): |
| 213 return not self.__eq__(other) | 301 return not self.__eq__(other) |
| OLD | NEW |