OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 import copy | 5 import copy |
6 import os.path | 6 import os.path |
7 import re | 7 import re |
8 | 8 |
9 class ParseException(Exception): | 9 class ParseException(Exception): |
10 """Thrown when data in the model is invalid. | 10 """Thrown when data in the model is invalid. |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 from_json=from_json, | 205 from_json=from_json, |
206 from_client=from_client) | 206 from_client=from_client) |
207 self.type_ = PropertyType.ARRAY | 207 self.type_ = PropertyType.ARRAY |
208 elif json_type == 'object': | 208 elif json_type == 'object': |
209 self.type_ = PropertyType.OBJECT | 209 self.type_ = PropertyType.OBJECT |
210 # These members are read when this OBJECT Property is used as a Type | 210 # These members are read when this OBJECT Property is used as a Type |
211 type_ = Type(self, self.name, json) | 211 type_ = Type(self, self.name, json) |
212 # self.properties will already have some value from |_AddProperties|. | 212 # self.properties will already have some value from |_AddProperties|. |
213 self.properties.update(type_.properties) | 213 self.properties.update(type_.properties) |
214 self.functions = type_.functions | 214 self.functions = type_.functions |
| 215 elif json_type == 'function': |
| 216 self.type_ = PropertyType.FUNCTION |
215 elif json_type == 'binary': | 217 elif json_type == 'binary': |
216 self.type_ = PropertyType.BINARY | 218 self.type_ = PropertyType.BINARY |
217 else: | 219 else: |
218 raise ParseException(self, 'type ' + json_type + ' not recognized') | 220 raise ParseException(self, 'type ' + json_type + ' not recognized') |
219 elif 'choices' in json: | 221 elif 'choices' in json: |
220 if not json['choices'] or len(json['choices']) == 0: | 222 if not json['choices'] or len(json['choices']) == 0: |
221 raise ParseException(self, 'Choices has no choices') | 223 raise ParseException(self, 'Choices has no choices') |
222 self.choices = {} | 224 self.choices = {} |
223 self.type_ = PropertyType.CHOICES | 225 self.type_ = PropertyType.CHOICES |
224 for choice_json in json['choices']: | 226 for choice_json in json['choices']: |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 | 288 |
287 INTEGER = _Info(True, "INTEGER") | 289 INTEGER = _Info(True, "INTEGER") |
288 DOUBLE = _Info(True, "DOUBLE") | 290 DOUBLE = _Info(True, "DOUBLE") |
289 BOOLEAN = _Info(True, "BOOLEAN") | 291 BOOLEAN = _Info(True, "BOOLEAN") |
290 STRING = _Info(True, "STRING") | 292 STRING = _Info(True, "STRING") |
291 ENUM = _Info(False, "ENUM") | 293 ENUM = _Info(False, "ENUM") |
292 ARRAY = _Info(False, "ARRAY") | 294 ARRAY = _Info(False, "ARRAY") |
293 REF = _Info(False, "REF") | 295 REF = _Info(False, "REF") |
294 CHOICES = _Info(False, "CHOICES") | 296 CHOICES = _Info(False, "CHOICES") |
295 OBJECT = _Info(False, "OBJECT") | 297 OBJECT = _Info(False, "OBJECT") |
| 298 FUNCTION = _Info(False, "FUNCTION") |
296 BINARY = _Info(False, "BINARY") | 299 BINARY = _Info(False, "BINARY") |
297 ANY = _Info(False, "ANY") | 300 ANY = _Info(False, "ANY") |
298 ADDITIONAL_PROPERTIES = _Info(False, "ADDITIONAL_PROPERTIES") | 301 ADDITIONAL_PROPERTIES = _Info(False, "ADDITIONAL_PROPERTIES") |
299 | 302 |
300 def UnixName(name): | 303 def UnixName(name): |
301 """Returns the unix_style name for a given lowerCamelCase string. | 304 """Returns the unix_style name for a given lowerCamelCase string. |
302 """ | 305 """ |
303 # First replace any lowerUpper patterns with lower_Upper. | 306 # First replace any lowerUpper patterns with lower_Upper. |
304 s1 = re.sub('([a-z])([A-Z])', r'\1_\2', name) | 307 s1 = re.sub('([a-z])([A-Z])', r'\1_\2', name) |
305 # Now replace any ACMEWidgets patterns with ACME_Widgets | 308 # Now replace any ACMEWidgets patterns with ACME_Widgets |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 for event_json in json.get('events', []): | 346 for event_json in json.get('events', []): |
344 event = Function(model, event_json, from_client=True) | 347 event = Function(model, event_json, from_client=True) |
345 model.events[event.name] = event | 348 model.events[event.name] = event |
346 | 349 |
347 def _AddProperties(model, json, from_json=False, from_client=False): | 350 def _AddProperties(model, json, from_json=False, from_client=False): |
348 """Adds model.Property objects to |model| contained in the 'properties' field | 351 """Adds model.Property objects to |model| contained in the 'properties' field |
349 of |json|. | 352 of |json|. |
350 """ | 353 """ |
351 model.properties = {} | 354 model.properties = {} |
352 for name, property_json in json.get('properties', {}).items(): | 355 for name, property_json in json.get('properties', {}).items(): |
353 # TODO(calamity): support functions (callbacks) as properties. The model | |
354 # doesn't support it yet because the h/cc generators don't -- this is | |
355 # because we'd need to hook it into a base::Callback or something. | |
356 # | |
357 # However, pragmatically it's not necessary to support them anyway, since | |
358 # the instances of functions-on-properties in the extension APIs are all | |
359 # handled in pure Javascript on the render process (and .: never reach | |
360 # C++ let alone the browser). | |
361 if property_json.get('type') == 'function': | |
362 continue | |
363 model.properties[name] = Property( | 356 model.properties[name] = Property( |
364 model, | 357 model, |
365 name, | 358 name, |
366 property_json, | 359 property_json, |
367 from_json=from_json, | 360 from_json=from_json, |
368 from_client=from_client) | 361 from_client=from_client) |
OLD | NEW |