| 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 from json_parse import OrderedDict | 9 from json_parse import OrderedDict |
| 10 | 10 |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 self.optional = json.get('optional', False) | 215 self.optional = json.get('optional', False) |
| 216 self.parent = parent | 216 self.parent = parent |
| 217 self.nocompile = json.get('nocompile') | 217 self.nocompile = json.get('nocompile') |
| 218 options = json.get('options', {}) | 218 options = json.get('options', {}) |
| 219 self.conditions = options.get('conditions', []) | 219 self.conditions = options.get('conditions', []) |
| 220 self.actions = options.get('actions', []) | 220 self.actions = options.get('actions', []) |
| 221 self.supports_listeners = options.get('supportsListeners', True) | 221 self.supports_listeners = options.get('supportsListeners', True) |
| 222 self.supports_rules = options.get('supportsRules', False) | 222 self.supports_rules = options.get('supportsRules', False) |
| 223 | 223 |
| 224 def GeneratePropertyFromParam(p): | 224 def GeneratePropertyFromParam(p): |
| 225 return Property.FromJSON(self, p['name'], p, namespace, origin) | 225 return Property(self, p['name'], p, namespace, origin) |
| 226 | 226 |
| 227 self.filters = [GeneratePropertyFromParam(filter) | 227 self.filters = [GeneratePropertyFromParam(filter) |
| 228 for filter in json.get('filters', [])] | 228 for filter in json.get('filters', [])] |
| 229 callback_param = None | 229 callback_param = None |
| 230 for param in json.get('parameters', []): | 230 for param in json.get('parameters', []): |
| 231 if param.get('type') == 'function': | 231 if param.get('type') == 'function': |
| 232 if callback_param: | 232 if callback_param: |
| 233 # No ParseException because the webstore has this. | 233 # No ParseException because the webstore has this. |
| 234 # Instead, pretend all intermediate callbacks are properties. | 234 # Instead, pretend all intermediate callbacks are properties. |
| 235 self.params.append(GeneratePropertyFromParam(callback_param)) | 235 self.params.append(GeneratePropertyFromParam(callback_param)) |
| 236 callback_param = param | 236 callback_param = param |
| 237 else: | 237 else: |
| 238 self.params.append(GeneratePropertyFromParam(param)) | 238 self.params.append(GeneratePropertyFromParam(param)) |
| 239 | 239 |
| 240 if callback_param: | 240 if callback_param: |
| 241 self.callback = Function(self, | 241 self.callback = Function(self, |
| 242 callback_param['name'], | 242 callback_param['name'], |
| 243 callback_param, | 243 callback_param, |
| 244 namespace, | 244 namespace, |
| 245 Origin(from_client=True)) | 245 Origin(from_client=True)) |
| 246 | 246 |
| 247 self.returns = None | 247 self.returns = None |
| 248 if 'returns' in json: | 248 if 'returns' in json: |
| 249 self.returns = Property.FromJSON( | 249 self.returns = Type(self, |
| 250 self, 'return', json['returns'], namespace, origin) | 250 '%sReturnType' % name, |
| 251 json['returns'], |
| 252 namespace, |
| 253 origin) |
| 251 | 254 |
| 252 class Property(object): | 255 class Property(object): |
| 253 """A property of a type OR a parameter to a function. | 256 """A property of a type OR a parameter to a function. |
| 254 Properties: | 257 Properties: |
| 255 - |name| name of the property as in the json. This shouldn't change since | 258 - |name| name of the property as in the json. This shouldn't change since |
| 256 it is the key used to access DictionaryValues | 259 it is the key used to access DictionaryValues |
| 257 - |unix_name| the unix_style_name of the property. Used as variable name | 260 - |unix_name| the unix_style_name of the property. Used as variable name |
| 258 - |optional| a boolean representing whether the property is optional | 261 - |optional| a boolean representing whether the property is optional |
| 259 - |description| a description of the property (if provided) | 262 - |description| a description of the property (if provided) |
| 260 - |type_| the model.Type of this property | 263 - |type_| the model.Type of this property |
| 261 - |simple_name| the name of this Property without a namespace | 264 - |simple_name| the name of this Property without a namespace |
| 262 """ | 265 """ |
| 263 | 266 def __init__(self, parent, name, json, namespace, origin): |
| 264 @staticmethod | |
| 265 def FromJSON(parent, name, json, namespace, origin): | |
| 266 """Creates a Property from JSON. | 267 """Creates a Property from JSON. |
| 267 """ | 268 """ |
| 268 opt_args = {} | 269 self.parent = parent |
| 269 if 'description' in json: | 270 self.name = name |
| 270 opt_args['description'] = json['description'] | 271 self._unix_name = UnixName(self.name) |
| 271 if 'optional' in json: | 272 self._unix_name_used = False |
| 272 opt_args['optional'] = json.get('optional') | 273 self.origin = origin |
| 273 if 'isInstanceOf' in json: | 274 self.simple_name = _StripNamespace(self.name, namespace) |
| 274 opt_args['instance_of'] = json.get('isInstanceOf') | 275 self.description = json.get('description', None) |
| 276 self.optional = json.get('optional', None) |
| 277 self.instance_of = json.get('isInstanceOf', None) |
| 275 | 278 |
| 276 # HACK: only support very specific value types. | 279 # HACK: only support very specific value types. |
| 277 is_allowed_value = ( | 280 is_allowed_value = ( |
| 278 '$ref' not in json and | 281 '$ref' not in json and |
| 279 ('type' not in json or json['type'] == 'integer' | 282 ('type' not in json or json['type'] == 'integer' |
| 280 or json['type'] == 'string')) | 283 or json['type'] == 'string')) |
| 281 | 284 |
| 285 self.value = None |
| 282 if 'value' in json and is_allowed_value: | 286 if 'value' in json and is_allowed_value: |
| 283 value = json['value'] | 287 self.value = json['value'] |
| 284 opt_args['value'] = value | |
| 285 if 'type' not in json: | 288 if 'type' not in json: |
| 286 # Sometimes the type of the value is left out, and we need to figure | 289 # Sometimes the type of the value is left out, and we need to figure |
| 287 # it out for ourselves. | 290 # it out for ourselves. |
| 288 if isinstance(value, int): | 291 if isinstance(self.value, int): |
| 289 json['type'] = 'integer' | 292 json['type'] = 'integer' |
| 290 elif isinstance(value, basestring): | 293 elif isinstance(self.value, basestring): |
| 291 json['type'] = 'string' | 294 json['type'] = 'string' |
| 292 else: | 295 else: |
| 293 # TODO(kalman): support more types as necessary. | 296 # TODO(kalman): support more types as necessary. |
| 294 raise ParseException( | 297 raise ParseException( |
| 295 parent, '"%s" is not a supported type for "value"' % type(value)) | 298 parent, |
| 299 '"%s" is not a supported type for "value"' % type(self.value)) |
| 296 | 300 |
| 297 type_ = Type(parent, name, json, namespace, origin) | 301 self.type_ = Type(parent, name, json, namespace, origin) |
| 298 return Property(parent, | |
| 299 name, | |
| 300 namespace, | |
| 301 type_, | |
| 302 origin, | |
| 303 **opt_args); | |
| 304 | |
| 305 def __init__(self, | |
| 306 parent, | |
| 307 name, | |
| 308 namespace, | |
| 309 type_, | |
| 310 origin, | |
| 311 description=None, | |
| 312 optional=False, | |
| 313 returns=None, | |
| 314 instance_of=None, | |
| 315 value=None): | |
| 316 """Directly initializes the fields of the Property. | |
| 317 """ | |
| 318 self.name = name | |
| 319 self.simple_name = _StripNamespace(self.name, namespace) | |
| 320 self._unix_name = UnixName(self.name) | |
| 321 self._unix_name_used = False | |
| 322 self.optional = optional | |
| 323 self.description = description | |
| 324 self.parent = parent | |
| 325 self.origin = origin | |
| 326 if not isinstance(type_, Type): | |
| 327 raise ValueError("not Type: %s" % type_) | |
| 328 self.type_ = type_ | |
| 329 self.returns = returns | |
| 330 if instance_of is not None: | |
| 331 self.instance_of = instance_of | |
| 332 self.value = value | |
| 333 | 302 |
| 334 def GetUnixName(self): | 303 def GetUnixName(self): |
| 335 """Gets the property's unix_name. Raises AttributeError if not set. | 304 """Gets the property's unix_name. Raises AttributeError if not set. |
| 336 """ | 305 """ |
| 337 if not self._unix_name: | 306 if not self._unix_name: |
| 338 raise AttributeError('No unix_name set on %s' % self.name) | 307 raise AttributeError('No unix_name set on %s' % self.name) |
| 339 self._unix_name_used = True | 308 self._unix_name_used = True |
| 340 return self._unix_name | 309 return self._unix_name |
| 341 | 310 |
| 342 def SetUnixName(self, unix_name): | 311 def SetUnixName(self, unix_name): |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 namespace, | 430 namespace, |
| 462 Origin(from_client=True)) | 431 Origin(from_client=True)) |
| 463 events[event.name] = event | 432 events[event.name] = event |
| 464 return events | 433 return events |
| 465 | 434 |
| 466 def _GetProperties(parent, json, namespace, origin): | 435 def _GetProperties(parent, json, namespace, origin): |
| 467 """Generates Property objects extracted from |json|. | 436 """Generates Property objects extracted from |json|. |
| 468 """ | 437 """ |
| 469 properties = OrderedDict() | 438 properties = OrderedDict() |
| 470 for name, property_json in json.get('properties', {}).items(): | 439 for name, property_json in json.get('properties', {}).items(): |
| 471 properties[name] = Property.FromJSON( | 440 properties[name] = Property(parent, name, property_json, namespace, origin) |
| 472 parent, name, property_json, namespace, origin) | |
| 473 return properties | 441 return properties |
| 474 | 442 |
| 475 class _PlatformInfo(_Enum): | 443 class _PlatformInfo(_Enum): |
| 476 def __init__(self, name): | 444 def __init__(self, name): |
| 477 _Enum.__init__(self, name) | 445 _Enum.__init__(self, name) |
| 478 | 446 |
| 479 class Platforms(object): | 447 class Platforms(object): |
| 480 """Enum of the possible platforms. | 448 """Enum of the possible platforms. |
| 481 """ | 449 """ |
| 482 CHROMEOS = _PlatformInfo("chromeos") | 450 CHROMEOS = _PlatformInfo("chromeos") |
| 483 CHROMEOS_TOUCH = _PlatformInfo("chromeos_touch") | 451 CHROMEOS_TOUCH = _PlatformInfo("chromeos_touch") |
| 484 LINUX = _PlatformInfo("linux") | 452 LINUX = _PlatformInfo("linux") |
| 485 MAC = _PlatformInfo("mac") | 453 MAC = _PlatformInfo("mac") |
| 486 WIN = _PlatformInfo("win") | 454 WIN = _PlatformInfo("win") |
| 487 | 455 |
| 488 def _GetPlatforms(json): | 456 def _GetPlatforms(json): |
| 489 if 'platforms' not in json: | 457 if 'platforms' not in json: |
| 490 return None | 458 return None |
| 491 platforms = [] | 459 platforms = [] |
| 492 for platform_name in json['platforms']: | 460 for platform_name in json['platforms']: |
| 493 for platform_enum in _Enum.GetAll(Platforms): | 461 for platform_enum in _Enum.GetAll(Platforms): |
| 494 if platform_name == platform_enum.name: | 462 if platform_name == platform_enum.name: |
| 495 platforms.append(platform_enum) | 463 platforms.append(platform_enum) |
| 496 break | 464 break |
| 497 return platforms | 465 return platforms |
| OLD | NEW |