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..7b435bdef1209f02ede14864276fa38ef6db796a 100644 |
| --- a/mojo/public/python/mojo_bindings/reflection.py |
| +++ b/mojo/public/python/mojo_bindings/reflection.py |
| @@ -108,6 +108,8 @@ class MojoStructType(type): |
| return serialization_object.Serialize(self, handle_offset) |
| dictionary['Serialize'] = Serialize |
| + 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.
|
| + |
| # pylint: disable=W0212 |
| def AsDict(self): |
| return self._fields |
| @@ -135,6 +137,96 @@ class MojoStructType(type): |
| raise AttributeError('can\'t delete attribute') |
| +class MojoUnionType(type): |
| + |
| + def __new__(mcs, name, bases, dictionary): |
| + dictionary['__slots__'] = ('_fields', '_tags', '_tag', '_data') |
| + descriptor = dictionary.pop('DESCRIPTOR', {}) |
| + |
| + fields = descriptor.get('fields', []) |
| + def _BuildUnionProperty(field): |
| + |
| + # pylint: disable=W0212 |
| + def Get(self): |
| + 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.
|
| + raise AttributeError('%s is not currently set' % field.name, |
| + field.name, self._tag_names[self._tag]) |
| + return self._data |
| + |
| + # pylint: disable=W0212 |
| + def Set(self, value): |
| + self._tag = self._tags[field.name] |
| + self._data = field.field_type.Convert(value) |
| + |
| + return property(Get, Set) |
| + |
| + for field in fields: |
| + dictionary[field.name] = _BuildUnionProperty(field) |
| + |
| + dictionary['_tags'] = {field.name: field.index for field in fields} |
| + dictionary['_tag_names'] = {field.index: field.name for field in fields} |
| + |
| + 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) |
| + |
| + def GetTag(self): |
| + return self._tag |
| + 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.
|
| + |
| + 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__, |
| + self._tag_names[self.tag], |
| + self.tag, |
| + self.data) |
| + dictionary['__str__'] = UnionStr |
| + dictionary['__repr__'] = UnionStr |
| + |
| + return type.__new__(mcs, name, bases, dictionary) |
| + |
| + |
| +class UnionData(object): |
| + __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.
|
| + |
| + |
| + |
| + |
| class InterfaceRequest(object): |
| """ |
| An interface request allows to send a request for an interface to a remote |