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

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__'] = ('_cur_field', '_data')
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 self.SetInternals(None, None)
166 items = kwargs.items()
167 if len(items) == 0:
168 return
169
170 if len(items) > 1:
171 raise TypeError('only 1 member may be set on a union.')
172
173 setattr(self, items[0][0], items[0][1])
174 dictionary['__init__'] = UnionInit
175
176 serializer = serialization.UnionSerializer(fields)
177 def SerializeUnionInline(self, handle_offset=0):
178 return serializer.SerializeInline(self, handle_offset)
179 dictionary['SerializeInline'] = SerializeUnionInline
180
181 def SerializeUnion(self, handle_offset=0):
182 return serializer.Serialize(self, handle_offset)
183 dictionary['Serialize'] = SerializeUnion
184
185 def DeserializeUnion(cls, context):
186 return serializer.Deserialize(context, cls)
187 dictionary['Deserialize'] = classmethod(DeserializeUnion)
188
189 class Tags(object):
190 __metaclass__ = MojoEnumType
191 VALUES = [(field.name, field.index) for field in fields]
192 dictionary['Tags'] = Tags
193
194 def GetTag(self):
195 return self._cur_field.index
196 dictionary['tag'] = property(GetTag, None)
197
198 def GetData(self):
199 return self._data
200 dictionary['data'] = property(GetData, None)
201
202 def IsUnknown(self):
203 return not self._cur_field
204 dictionary['IsUnknown'] = IsUnknown
205
206 def UnionEq(self, other):
207 return (
208 (type(self) is type(other))
209 and (self.tag == other.tag)
210 and (self.data == other.data))
211 dictionary['__eq__'] = UnionEq
212
213 def UnionNe(self, other):
214 return not self.__eq__(other)
215 dictionary['__ne__'] = UnionNe
216
217 def UnionStr(self):
218 return '<%s.%s(%s): %s>' % (
219 self.__class__.__name__,
220 self._cur_field.name,
221 self.tag,
222 self.data)
223 dictionary['__str__'] = UnionStr
224 dictionary['__repr__'] = UnionStr
225
226 def SetInternals(self, field, data):
227 self._cur_field = field
228 self._data = data
229 dictionary['SetInternals'] = SetInternals
230
231
232 return type.__new__(mcs, name, bases, dictionary)
233
234
138 class InterfaceRequest(object): 235 class InterfaceRequest(object):
139 """ 236 """
140 An interface request allows to send a request for an interface to a remote 237 An interface request allows to send a request for an interface to a remote
141 object and start using it immediately. 238 object and start using it immediately.
142 """ 239 """
143 240
144 def __init__(self, handle): 241 def __init__(self, handle):
145 self._handle = handle 242 self._handle = handle
146 243
147 def IsPending(self): 244 def IsPending(self):
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 if type(self) is not type(other): 301 if type(self) is not type(other):
205 return False 302 return False
206 for field in fields: 303 for field in fields:
207 if getattr(self, field.name) != getattr(other, field.name): 304 if getattr(self, field.name) != getattr(other, field.name):
208 return False 305 return False
209 return True 306 return True
210 return _Eq 307 return _Eq
211 308
212 def _StructNe(self, other): 309 def _StructNe(self, other):
213 return not self.__eq__(other) 310 return not self.__eq__(other)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698