| 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 Model(object): | 9 class Model(object): |
| 10 """Model of all namespaces that comprise an API. | 10 """Model of all namespaces that comprise an API. |
| 11 | 11 |
| 12 Properties: | 12 Properties: |
| 13 - |namespaces| a map of a namespace name to its model.Namespace | 13 - |namespaces| a map of a namespace name to its model.Namespace |
| 14 """ | 14 """ |
| 15 def __init__(self): | 15 def __init__(self): |
| 16 self.namespaces = {} | 16 self.namespaces = {} |
| 17 | 17 |
| 18 def AddNamespace(self, json, source_file): | 18 def AddNamespace(self, json, source_file): |
| 19 """Add a namespace's json to the model if it doesn't have "nocompile" | 19 """Add a namespace's json to the model and returns the namespace. |
| 20 property set to true. Returns the new namespace or None if a namespace | |
| 21 wasn't added. | |
| 22 """ | 20 """ |
| 23 if json.get('nocompile', False): | |
| 24 return None | |
| 25 namespace = Namespace(json, source_file) | 21 namespace = Namespace(json, source_file) |
| 26 self.namespaces[namespace.name] = namespace | 22 self.namespaces[namespace.name] = namespace |
| 27 return namespace | 23 return namespace |
| 28 | 24 |
| 29 class Namespace(object): | 25 class Namespace(object): |
| 30 """An API namespace. | 26 """An API namespace. |
| 31 | 27 |
| 32 Properties: | 28 Properties: |
| 33 - |name| the name of the namespace | 29 - |name| the name of the namespace |
| 34 - |unix_name| the unix_name of the namespace | 30 - |unix_name| the unix_name of the namespace |
| (...skipping 12 matching lines...) Expand all Loading... |
| 47 self.functions = {} | 43 self.functions = {} |
| 48 self.parent = None | 44 self.parent = None |
| 49 # TODO(calamity): Implement properties on namespaces for shared structures | 45 # TODO(calamity): Implement properties on namespaces for shared structures |
| 50 # or constants across a namespace (e.g Windows::WINDOW_ID_NONE). | 46 # or constants across a namespace (e.g Windows::WINDOW_ID_NONE). |
| 51 for property_json in json.get('properties', []): | 47 for property_json in json.get('properties', []): |
| 52 pass | 48 pass |
| 53 for type_json in json.get('types', []): | 49 for type_json in json.get('types', []): |
| 54 type_ = Type(self, type_json['id'], type_json) | 50 type_ = Type(self, type_json['id'], type_json) |
| 55 self.types[type_.name] = type_ | 51 self.types[type_.name] = type_ |
| 56 for function_json in json.get('functions', []): | 52 for function_json in json.get('functions', []): |
| 57 if not function_json.get('nocompile', False): | 53 self.functions[function_json['name']] = Function(self, function_json) |
| 58 self.functions[function_json['name']] = Function(self, function_json) | |
| 59 | 54 |
| 60 class Type(object): | 55 class Type(object): |
| 61 """A Type defined in the json. | 56 """A Type defined in the json. |
| 62 | 57 |
| 63 Properties: | 58 Properties: |
| 64 - |name| the type name | 59 - |name| the type name |
| 65 - |description| the description of the type (if provided) | 60 - |description| the description of the type (if provided) |
| 66 - |properties| a map of property unix_names to their model.Property | 61 - |properties| a map of property unix_names to their model.Property |
| 67 - |functions| a map of function names to their model.Function | 62 - |functions| a map of function names to their model.Function |
| 68 - |from_client| indicates that instances of the Type can originate from the | 63 - |from_client| indicates that instances of the Type can originate from the |
| (...skipping 18 matching lines...) Expand all Loading... |
| 87 raise ParseException(name + " has no properties or functions") | 82 raise ParseException(name + " has no properties or functions") |
| 88 self.type_ = PropertyType.OBJECT | 83 self.type_ = PropertyType.OBJECT |
| 89 self.name = name | 84 self.name = name |
| 90 self.description = json.get('description') | 85 self.description = json.get('description') |
| 91 self.from_json = True | 86 self.from_json = True |
| 92 self.from_client = True | 87 self.from_client = True |
| 93 self.properties = {} | 88 self.properties = {} |
| 94 self.functions = {} | 89 self.functions = {} |
| 95 self.parent = parent | 90 self.parent = parent |
| 96 for function_json in json.get('functions', []): | 91 for function_json in json.get('functions', []): |
| 97 if not function_json.get('nocompile', False): | 92 self.functions[function_json['name']] = Function(self, function_json) |
| 98 self.functions[function_json['name']] = Function(self, function_json) | |
| 99 props = [] | 93 props = [] |
| 100 for prop_name, prop_json in json.get('properties', {}).items(): | 94 for prop_name, prop_json in json.get('properties', {}).items(): |
| 101 # TODO(calamity): support functions (callbacks) as properties. The model | 95 # TODO(calamity): support functions (callbacks) as properties. The model |
| 102 # doesn't support it yet because the h/cc generators don't -- this is | 96 # doesn't support it yet because the h/cc generators don't -- this is |
| 103 # because we'd need to hook it into a base::Callback or something. | 97 # because we'd need to hook it into a base::Callback or something. |
| 104 # | 98 # |
| 105 # However, pragmatically it's not necessary to support them anyway, since | 99 # However, pragmatically it's not necessary to support them anyway, since |
| 106 # the instances of functions-on-properties in the extension APIs are all | 100 # the instances of functions-on-properties in the extension APIs are all |
| 107 # handled in pure Javascript on the render process (and .: never reach | 101 # handled in pure Javascript on the render process (and .: never reach |
| 108 # C++ let alone the browser). | 102 # C++ let alone the browser). |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 """Returns the hierarchy of the given model entity.""" | 328 """Returns the hierarchy of the given model entity.""" |
| 335 hierarchy = [] | 329 hierarchy = [] |
| 336 while entity: | 330 while entity: |
| 337 try: | 331 try: |
| 338 hierarchy.append(entity.name) | 332 hierarchy.append(entity.name) |
| 339 except AttributeError: | 333 except AttributeError: |
| 340 hierarchy.append(repr(entity)) | 334 hierarchy.append(repr(entity)) |
| 341 entity = entity.parent | 335 entity = entity.parent |
| 342 hierarchy.reverse() | 336 hierarchy.reverse() |
| 343 return hierarchy | 337 return hierarchy |
| OLD | NEW |