Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: mojo/public/python/mojo_bindings/reflection.py

Issue 1218023006: Implement python mojo bindings unions. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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', '_cur_field', '_data')
qsr 2015/07/20 21:29:59 _fields doesn't seem to be used.
azani 2015/07/20 22:17:52 Done.
142 descriptor = dictionary.pop('DESCRIPTOR', {})
143
144 fields = descriptor.get('fields', [])
145 def _BuildUnionProperty(field):
146
147 # pylint: disable=W0212
148 def Get(self):
149 if self._cur_field != field:
150 raise AttributeError('%s is not currently set' % field.name,
151 field.name, self._cur_field.name)
152 return self._data
153
154 # pylint: disable=W0212
155 def Set(self, value):
156 self._cur_field = field
157 self._data = field.field_type.Convert(value)
158
159 return property(Get, Set)
160
161 for field in fields:
162 dictionary[field.name] = _BuildUnionProperty(field)
163
164 def UnionInit(self, **kwargs):
165 items = kwargs.items()
166 if len(items) == 0:
167 return
168
169 if len(items) > 1:
170 raise TypeError('only 1 member may be set on a union.')
171
172 setattr(self, items[0][0], items[0][1])
173 dictionary['__init__'] = UnionInit
174
175 serializer = serialization.UnionSerializer(fields)
176 def SerializeUnionInline(self, handle_offset=0):
177 return serializer.SerializeInline(self, handle_offset)
178 dictionary['SerializeInline'] = SerializeUnionInline
179
180 def SerializeUnion(self, handle_offset=0):
181 return serializer.Serialize(self, handle_offset)
182 dictionary['Serialize'] = SerializeUnion
183
184 def DeserializeUnion(cls, context):
185 return serializer.Deserialize(context, cls)
186 dictionary['Deserialize'] = classmethod(DeserializeUnion)
187
188 class Tags(object):
189 __metaclass__ = MojoEnumType
190 VALUES = [(field.name, field.index) for field in fields]
191 dictionary['Tags'] = Tags
192
193 def GetTag(self):
194 return self._cur_field.index
195 dictionary['tag'] = property(GetTag, None)
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._cur_field.name,
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
138 class InterfaceRequest(object): 224 class InterfaceRequest(object):
139 """ 225 """
140 An interface request allows to send a request for an interface to a remote 226 An interface request allows to send a request for an interface to a remote
141 object and start using it immediately. 227 object and start using it immediately.
142 """ 228 """
143 229
144 def __init__(self, handle): 230 def __init__(self, handle):
145 self._handle = handle 231 self._handle = handle
146 232
147 def IsPending(self): 233 def IsPending(self):
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 if type(self) is not type(other): 290 if type(self) is not type(other):
205 return False 291 return False
206 for field in fields: 292 for field in fields:
207 if getattr(self, field.name) != getattr(other, field.name): 293 if getattr(self, field.name) != getattr(other, field.name):
208 return False 294 return False
209 return True 295 return True
210 return _Eq 296 return _Eq
211 297
212 def _StructNe(self, other): 298 def _StructNe(self, other):
213 return not self.__eq__(other) 299 return not self.__eq__(other)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698