Chromium Code Reviews| Index: mojo/public/python/mojo_bindings/reflection.py |
| diff --git a/mojo/public/python/mojo_bindings/reflection.py b/mojo/public/python/mojo_bindings/reflection.py |
| index 6c4767b7b6f0146bb9bea5e39214b394064a9996..ba570b0aae5840e7e1c7c4e16608f93b6b5b835a 100644 |
| --- a/mojo/public/python/mojo_bindings/reflection.py |
| +++ b/mojo/public/python/mojo_bindings/reflection.py |
| @@ -135,6 +135,94 @@ class MojoStructType(type): |
| raise AttributeError('can\'t delete attribute') |
| +class MojoUnionType(type): |
| + |
| + def __new__(mcs, name, bases, dictionary): |
| + 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.
|
| + descriptor = dictionary.pop('DESCRIPTOR', {}) |
| + |
| + fields = descriptor.get('fields', []) |
| + tag_names = {field.index: field.name for field in fields} |
| + tags = {field.name: field.index for field in fields} |
| + def _BuildUnionProperty(field): |
| + |
| + # pylint: disable=W0212 |
| + def Get(self): |
| + if self._tag != tags[field.name]: |
| + raise AttributeError('%s is not currently set' % field.name, |
| + 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
|
| + return self._data |
| + |
| + # pylint: disable=W0212 |
| + def Set(self, value): |
| + 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.
|
| + self._data = field.field_type.Convert(value) |
| + |
| + return property(Get, Set) |
| + |
| + for field in fields: |
| + dictionary[field.name] = _BuildUnionProperty(field) |
| + |
| + def UnionInit(self, **kwargs): |
| + items = kwargs.items() |
| + if len(items) == 0: |
| + return |
| + |
| + if len(items) > 1: |
| + raise TypeError('only 1 member may be set on a union.') |
| + |
| + setattr(self, items[0][0], items[0][1]) |
| + dictionary['__init__'] = UnionInit |
| + |
| + serializer = serialization.UnionSerializer(fields) |
| + def SerializeUnionInline(self, handle_offset=0): |
| + return serializer.SerializeInline(self, handle_offset) |
| + dictionary['SerializeInline'] = SerializeUnionInline |
| + |
| + def SerializeUnion(self, handle_offset=0): |
| + return serializer.Serialize(self, handle_offset) |
| + dictionary['Serialize'] = SerializeUnion |
| + |
| + def DeserializeUnion(cls, context): |
| + return serializer.Deserialize(context, cls) |
| + dictionary['Deserialize'] = classmethod(DeserializeUnion) |
| + |
| + class Tags(object): |
| + __metaclass__ = MojoEnumType |
| + VALUES = [(field.name, field.index) for field in fields] |
| + dictionary['Tags'] = Tags |
| + |
| + def GetTag(self): |
| + return self._tag |
| + dictionary['tag'] = property(GetTag, None) |
| + |
| + def GetData(self): |
| + return self._data |
| + dictionary['data'] = property(GetData, None) |
| + |
| + def UnionEq(self, other): |
| + return ( |
| + (type(self) is type(other)) |
| + and (self.tag == other.tag) |
| + and (self.data == other.data)) |
| + dictionary['__eq__'] = UnionEq |
| + |
| + def UnionNe(self, other): |
| + return not self.__eq__(other) |
| + dictionary['__ne__'] = UnionNe |
| + |
| + def UnionStr(self): |
| + return '<%s.%s(%s): %s>' % ( |
| + self.__class__.__name__, |
| + tag_names[self.tag], |
| + self.tag, |
| + self.data) |
| + dictionary['__str__'] = UnionStr |
| + dictionary['__repr__'] = UnionStr |
| + |
| + return type.__new__(mcs, name, bases, dictionary) |
| + |
| + |
| class InterfaceRequest(object): |
| """ |
| An interface request allows to send a request for an interface to a remote |