[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 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 |
| 3 | # found in the LICENSE file. |
| 4 | |
[email protected] | df74dc4 | 2012-04-03 07:08:04 | [diff] [blame] | 5 | import copy |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 6 | import os.path |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 7 | import re |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 8 | |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 9 | class ParseException(Exception): |
| 10 | """Thrown when data in the model is invalid. |
| 11 | """ |
| 12 | def __init__(self, parent, message): |
| 13 | hierarchy = _GetModelHierarchy(parent) |
| 14 | hierarchy.append(message) |
| 15 | Exception.__init__( |
| 16 | self, 'Model parse exception at:\n' + '\n'.join(hierarchy)) |
| 17 | |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 18 | class Model(object): |
| 19 | """Model of all namespaces that comprise an API. |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 20 | |
| 21 | Properties: |
| 22 | - |namespaces| a map of a namespace name to its model.Namespace |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 23 | """ |
| 24 | def __init__(self): |
| 25 | self.namespaces = {} |
| 26 | |
| 27 | def AddNamespace(self, json, source_file): |
[email protected] | a1f77497 | 2012-04-17 02:11:09 | [diff] [blame] | 28 | """Add a namespace's json to the model and returns the namespace. |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 29 | """ |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 30 | namespace = Namespace(json, source_file) |
| 31 | self.namespaces[namespace.name] = namespace |
| 32 | return namespace |
| 33 | |
| 34 | class Namespace(object): |
| 35 | """An API namespace. |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 36 | |
| 37 | Properties: |
| 38 | - |name| the name of the namespace |
[email protected] | 712eca0f | 2012-02-21 01:13:07 | [diff] [blame] | 39 | - |unix_name| the unix_name of the namespace |
| 40 | - |source_file| the file that contained the namespace definition |
| 41 | - |source_file_dir| the directory component of |source_file| |
| 42 | - |source_file_filename| the filename component of |source_file| |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 43 | - |types| a map of type names to their model.Type |
| 44 | - |functions| a map of function names to their model.Function |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 45 | - |events| a map of event names to their model.Function |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 46 | - |properties| a map of property names to their model.Property |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 47 | """ |
| 48 | def __init__(self, json, source_file): |
| 49 | self.name = json['namespace'] |
[email protected] | 4ba6bdc | 2012-06-18 20:40:14 | [diff] [blame] | 50 | self.unix_name = UnixName(self.name) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 51 | self.source_file = source_file |
| 52 | self.source_file_dir, self.source_file_filename = os.path.split(source_file) |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 53 | self.parent = None |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 54 | _AddTypes(self, json, self) |
| 55 | _AddFunctions(self, json, self) |
| 56 | _AddEvents(self, json, self) |
| 57 | _AddProperties(self, json, self) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 58 | |
| 59 | class Type(object): |
| 60 | """A Type defined in the json. |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 61 | |
| 62 | Properties: |
| 63 | - |name| the type name |
| 64 | - |description| the description of the type (if provided) |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 65 | - |properties| a map of property unix_names to their model.Property |
| 66 | - |functions| a map of function names to their model.Function |
[email protected] | d32b8e5 | 2012-07-23 18:40:08 | [diff] [blame] | 67 | - |events| a map of event names to their model.Event |
[email protected] | 25cbf601 | 2012-02-28 05:51:44 | [diff] [blame] | 68 | - |from_client| indicates that instances of the Type can originate from the |
| 69 | users of generated code, such as top-level types and function results |
| 70 | - |from_json| indicates that instances of the Type can originate from the |
| 71 | JSON (as described by the schema), such as top-level types and function |
| 72 | parameters |
[email protected] | cf6d0b3 | 2012-04-12 04:30:22 | [diff] [blame] | 73 | - |type_| the PropertyType of this Type |
| 74 | - |item_type| if this is an array, the type of items in the array |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 75 | - |simple_name| the name of this Type without a namespace |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 76 | """ |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 77 | def __init__(self, parent, name, json, namespace): |
[email protected] | cf6d0b3 | 2012-04-12 04:30:22 | [diff] [blame] | 78 | if json.get('type') == 'array': |
| 79 | self.type_ = PropertyType.ARRAY |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 80 | self.item_type = Property(self, |
| 81 | name + "Element", |
| 82 | json['items'], |
| 83 | namespace, |
[email protected] | cf6d0b3 | 2012-04-12 04:30:22 | [diff] [blame] | 84 | from_json=True, |
| 85 | from_client=True) |
[email protected] | e7ae7017 | 2012-08-29 04:07:13 | [diff] [blame] | 86 | elif 'enum' in json: |
| 87 | self.enum_values = [] |
| 88 | for value in json['enum']: |
| 89 | self.enum_values.append(value) |
| 90 | self.type_ = PropertyType.ENUM |
[email protected] | 0ba7b4f | 2012-09-25 01:00:41 | [diff] [blame] | 91 | elif json.get('type') == 'string': |
| 92 | self.type_ = PropertyType.STRING |
[email protected] | cf6d0b3 | 2012-04-12 04:30:22 | [diff] [blame] | 93 | else: |
| 94 | if not ( |
| 95 | 'properties' in json or |
| 96 | 'additionalProperties' in json or |
[email protected] | e975d11 | 2012-07-31 22:12:43 | [diff] [blame] | 97 | 'functions' in json or |
| 98 | 'events' in json): |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 99 | raise ParseException(self, name + " has no properties or functions") |
[email protected] | cf6d0b3 | 2012-04-12 04:30:22 | [diff] [blame] | 100 | self.type_ = PropertyType.OBJECT |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 101 | self.name = name |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 102 | self.simple_name = _StripNamespace(self.name, namespace) |
[email protected] | 0ba7b4f | 2012-09-25 01:00:41 | [diff] [blame] | 103 | self.unix_name = UnixName(self.name) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 104 | self.description = json.get('description') |
[email protected] | 25cbf601 | 2012-02-28 05:51:44 | [diff] [blame] | 105 | self.from_json = True |
| 106 | self.from_client = True |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 107 | self.parent = parent |
[email protected] | 491e60d3 | 2012-07-20 01:03:13 | [diff] [blame] | 108 | self.instance_of = json.get('isInstanceOf', None) |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 109 | _AddFunctions(self, json, namespace) |
| 110 | _AddEvents(self, json, namespace) |
| 111 | _AddProperties(self, json, namespace, from_json=True, from_client=True) |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 112 | |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 113 | additional_properties_key = 'additionalProperties' |
| 114 | additional_properties = json.get(additional_properties_key) |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 115 | if additional_properties: |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 116 | self.properties[additional_properties_key] = Property( |
| 117 | self, |
| 118 | additional_properties_key, |
| 119 | additional_properties, |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 120 | namespace, |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 121 | is_additional_properties=True) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 122 | |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 123 | class Function(object): |
| 124 | """A Function defined in the API. |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 125 | |
| 126 | Properties: |
| 127 | - |name| the function name |
| 128 | - |params| a list of parameters to the function (order matters). A separate |
| 129 | parameter is used for each choice of a 'choices' parameter. |
| 130 | - |description| a description of the function (if provided) |
| 131 | - |callback| the callback parameter to the function. There should be exactly |
| 132 | one |
[email protected] | 4b3f785 | 2012-07-17 06:33:30 | [diff] [blame] | 133 | - |optional| whether the Function is "optional"; this only makes sense to be |
| 134 | present when the Function is representing a callback property. |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 135 | - |simple_name| the name of this Function without a namespace |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 136 | """ |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 137 | def __init__(self, |
| 138 | parent, |
| 139 | json, |
| 140 | namespace, |
| 141 | from_json=False, |
| 142 | from_client=False): |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 143 | self.name = json['name'] |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 144 | self.simple_name = _StripNamespace(self.name, namespace) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 145 | self.params = [] |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 146 | self.description = json.get('description') |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 147 | self.callback = None |
[email protected] | 4b3f785 | 2012-07-17 06:33:30 | [diff] [blame] | 148 | self.optional = json.get('optional', False) |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 149 | self.parent = parent |
[email protected] | a9ead75 | 2012-05-24 02:08:45 | [diff] [blame] | 150 | self.nocompile = json.get('nocompile') |
[email protected] | c071835 | 2012-08-21 02:34:24 | [diff] [blame] | 151 | options = json.get('options', {}) |
| 152 | self.conditions = options.get('conditions', []) |
| 153 | self.actions = options.get('actions', []) |
| 154 | self.supports_listeners = options.get('supportsListeners', True) |
| 155 | self.supports_rules = options.get('supportsRules', False) |
| 156 | def GeneratePropertyFromParam(p): |
| 157 | return Property(self, |
| 158 | p['name'], p, |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 159 | namespace, |
[email protected] | c071835 | 2012-08-21 02:34:24 | [diff] [blame] | 160 | from_json=from_json, |
| 161 | from_client=from_client) |
[email protected] | db94399 | 2012-08-02 14:02:54 | [diff] [blame] | 162 | |
[email protected] | c071835 | 2012-08-21 02:34:24 | [diff] [blame] | 163 | self.filters = [GeneratePropertyFromParam(filter) |
| 164 | for filter in json.get('filters', [])] |
[email protected] | db94399 | 2012-08-02 14:02:54 | [diff] [blame] | 165 | callback_param = None |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 166 | for param in json.get('parameters', []): |
[email protected] | db94399 | 2012-08-02 14:02:54 | [diff] [blame] | 167 | |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 168 | if param.get('type') == 'function': |
[email protected] | db94399 | 2012-08-02 14:02:54 | [diff] [blame] | 169 | if callback_param: |
| 170 | # No ParseException because the webstore has this. |
| 171 | # Instead, pretend all intermediate callbacks are properties. |
| 172 | self.params.append(GeneratePropertyFromParam(callback_param)) |
| 173 | callback_param = param |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 174 | else: |
[email protected] | db94399 | 2012-08-02 14:02:54 | [diff] [blame] | 175 | self.params.append(GeneratePropertyFromParam(param)) |
| 176 | |
| 177 | if callback_param: |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 178 | self.callback = Function(self, |
| 179 | callback_param, |
| 180 | namespace, |
| 181 | from_client=True) |
[email protected] | db94399 | 2012-08-02 14:02:54 | [diff] [blame] | 182 | |
[email protected] | 491e60d3 | 2012-07-20 01:03:13 | [diff] [blame] | 183 | self.returns = None |
| 184 | if 'returns' in json: |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 185 | self.returns = Property(self, 'return', json['returns'], namespace) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 186 | |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 187 | class Property(object): |
| 188 | """A property of a type OR a parameter to a function. |
| 189 | |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 190 | Properties: |
| 191 | - |name| name of the property as in the json. This shouldn't change since |
| 192 | it is the key used to access DictionaryValues |
| 193 | - |unix_name| the unix_style_name of the property. Used as variable name |
| 194 | - |optional| a boolean representing whether the property is optional |
| 195 | - |description| a description of the property (if provided) |
| 196 | - |type_| the model.PropertyType of this property |
[email protected] | 1d0f57e | 2012-07-31 22:47:50 | [diff] [blame] | 197 | - |compiled_type| the model.PropertyType that this property should be |
| 198 | compiled to from the JSON. Defaults to |type_|. |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 199 | - |ref_type| the type that the REF property is referencing. Can be used to |
| 200 | map to its model.Type |
| 201 | - |item_type| a model.Property representing the type of each element in an |
| 202 | ARRAY |
| 203 | - |properties| the properties of an OBJECT parameter |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 204 | - |from_client| indicates that instances of the Type can originate from the |
| 205 | users of generated code, such as top-level types and function results |
| 206 | - |from_json| indicates that instances of the Type can originate from the |
| 207 | JSON (as described by the schema), such as top-level types and function |
| 208 | parameters |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 209 | - |simple_name| the name of this Property without a namespace |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 210 | """ |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 211 | |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 212 | def __init__(self, |
| 213 | parent, |
| 214 | name, |
| 215 | json, |
| 216 | namespace, |
| 217 | is_additional_properties=False, |
| 218 | from_json=False, |
| 219 | from_client=False): |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 220 | self.name = name |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 221 | self.simple_name = _StripNamespace(self.name, namespace) |
[email protected] | 4ba6bdc | 2012-06-18 20:40:14 | [diff] [blame] | 222 | self._unix_name = UnixName(self.name) |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 223 | self._unix_name_used = False |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 224 | self.optional = json.get('optional', False) |
[email protected] | 3072d07 | 2012-07-19 00:35:33 | [diff] [blame] | 225 | self.functions = {} |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 226 | self.has_value = False |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 227 | self.description = json.get('description') |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 228 | self.parent = parent |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 229 | self.from_json = from_json |
| 230 | self.from_client = from_client |
[email protected] | 491e60d3 | 2012-07-20 01:03:13 | [diff] [blame] | 231 | self.instance_of = json.get('isInstanceOf', None) |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 232 | _AddProperties(self, json, namespace) |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 233 | if is_additional_properties: |
| 234 | self.type_ = PropertyType.ADDITIONAL_PROPERTIES |
| 235 | elif '$ref' in json: |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 236 | self.ref_type = json['$ref'] |
| 237 | self.type_ = PropertyType.REF |
[email protected] | 9d27318 | 2012-07-11 21:03:26 | [diff] [blame] | 238 | elif 'enum' in json and json.get('type') == 'string': |
| 239 | # Non-string enums (as in the case of [legalValues=(1,2)]) should fall |
| 240 | # through to the next elif. |
[email protected] | 2111fff9b | 2012-02-22 12:06:51 | [diff] [blame] | 241 | self.enum_values = [] |
| 242 | for value in json['enum']: |
| 243 | self.enum_values.append(value) |
| 244 | self.type_ = PropertyType.ENUM |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 245 | elif 'type' in json: |
[email protected] | 1d0f57e | 2012-07-31 22:47:50 | [diff] [blame] | 246 | self.type_ = self._JsonTypeToPropertyType(json['type']) |
| 247 | if self.type_ == PropertyType.ARRAY: |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 248 | self.item_type = Property(self, |
| 249 | name + "Element", |
| 250 | json['items'], |
| 251 | namespace, |
| 252 | from_json=from_json, |
| 253 | from_client=from_client) |
[email protected] | 1d0f57e | 2012-07-31 22:47:50 | [diff] [blame] | 254 | elif self.type_ == PropertyType.OBJECT: |
[email protected] | 25cbf601 | 2012-02-28 05:51:44 | [diff] [blame] | 255 | # These members are read when this OBJECT Property is used as a Type |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 256 | type_ = Type(self, self.name, json, namespace) |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 257 | # self.properties will already have some value from |_AddProperties|. |
| 258 | self.properties.update(type_.properties) |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 259 | self.functions = type_.functions |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 260 | elif 'choices' in json: |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 261 | if not json['choices'] or len(json['choices']) == 0: |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 262 | raise ParseException(self, 'Choices has no choices') |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 263 | self.choices = {} |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 264 | self.type_ = PropertyType.CHOICES |
[email protected] | 1d0f57e | 2012-07-31 22:47:50 | [diff] [blame] | 265 | self.compiled_type = self.type_ |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 266 | for choice_json in json['choices']: |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 267 | choice = Property(self, |
| 268 | self.name, |
| 269 | choice_json, |
| 270 | namespace, |
| 271 | from_json=from_json, |
| 272 | from_client=from_client) |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 273 | choice.unix_name = UnixName(self.name + choice.type_.name) |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 274 | # The existence of any single choice is optional |
| 275 | choice.optional = True |
| 276 | self.choices[choice.type_] = choice |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 277 | elif 'value' in json: |
| 278 | self.has_value = True |
| 279 | self.value = json['value'] |
| 280 | if type(self.value) == int: |
| 281 | self.type_ = PropertyType.INTEGER |
[email protected] | 1d0f57e | 2012-07-31 22:47:50 | [diff] [blame] | 282 | self.compiled_type = self.type_ |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 283 | else: |
| 284 | # TODO(kalman): support more types as necessary. |
| 285 | raise ParseException( |
| 286 | self, '"%s" is not a supported type' % type(self.value)) |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 287 | else: |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 288 | raise ParseException( |
| 289 | self, 'Property has no type, $ref, choices, or value') |
[email protected] | 1d0f57e | 2012-07-31 22:47:50 | [diff] [blame] | 290 | if 'compiled_type' in json: |
| 291 | if 'type' in json: |
| 292 | self.compiled_type = self._JsonTypeToPropertyType(json['compiled_type']) |
| 293 | else: |
| 294 | raise ParseException(self, 'Property has compiled_type but no type') |
| 295 | else: |
| 296 | self.compiled_type = self.type_ |
| 297 | |
| 298 | def _JsonTypeToPropertyType(self, json_type): |
| 299 | try: |
| 300 | return { |
| 301 | 'any': PropertyType.ANY, |
| 302 | 'array': PropertyType.ARRAY, |
| 303 | 'binary': PropertyType.BINARY, |
| 304 | 'boolean': PropertyType.BOOLEAN, |
| 305 | 'integer': PropertyType.INTEGER, |
| 306 | 'int64': PropertyType.INT64, |
| 307 | 'function': PropertyType.FUNCTION, |
| 308 | 'number': PropertyType.DOUBLE, |
| 309 | 'object': PropertyType.OBJECT, |
| 310 | 'string': PropertyType.STRING, |
| 311 | }[json_type] |
| 312 | except KeyError: |
| 313 | raise NotImplementedError('Type %s not recognized' % json_type) |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 314 | |
| 315 | def GetUnixName(self): |
| 316 | """Gets the property's unix_name. Raises AttributeError if not set. |
| 317 | """ |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 318 | if not self._unix_name: |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 319 | raise AttributeError('No unix_name set on %s' % self.name) |
| 320 | self._unix_name_used = True |
| 321 | return self._unix_name |
| 322 | |
| 323 | def SetUnixName(self, unix_name): |
| 324 | """Set the property's unix_name. Raises AttributeError if the unix_name has |
| 325 | already been used (GetUnixName has been called). |
| 326 | """ |
| 327 | if unix_name == self._unix_name: |
| 328 | return |
| 329 | if self._unix_name_used: |
| 330 | raise AttributeError( |
| 331 | 'Cannot set the unix_name on %s; ' |
| 332 | 'it is already used elsewhere as %s' % |
| 333 | (self.name, self._unix_name)) |
| 334 | self._unix_name = unix_name |
| 335 | |
[email protected] | 712eca0f | 2012-02-21 01:13:07 | [diff] [blame] | 336 | def Copy(self): |
| 337 | """Makes a copy of this model.Property object and allow the unix_name to be |
| 338 | set again. |
| 339 | """ |
| 340 | property_copy = copy.copy(self) |
| 341 | property_copy._unix_name_used = False |
| 342 | return property_copy |
| 343 | |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 344 | unix_name = property(GetUnixName, SetUnixName) |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 345 | |
[email protected] | fe02768 | 2012-08-21 01:51:34 | [diff] [blame] | 346 | class _PropertyTypeInfo(object): |
| 347 | """This class is not an inner class of |PropertyType| so it can be pickled. |
| 348 | """ |
| 349 | def __init__(self, is_fundamental, name): |
| 350 | self.is_fundamental = is_fundamental |
| 351 | self.name = name |
| 352 | |
| 353 | def __repr__(self): |
| 354 | return self.name |
| 355 | |
[email protected] | dc5f2fecf | 2012-08-31 01:55:51 | [diff] [blame] | 356 | def __eq__(self, other): |
| 357 | return isinstance(other, _PropertyTypeInfo) and self.name == other.name |
| 358 | |
[email protected] | 15f08dd | 2012-01-27 07:29:48 | [diff] [blame] | 359 | class PropertyType(object): |
| 360 | """Enum of different types of properties/parameters. |
| 361 | """ |
[email protected] | fe02768 | 2012-08-21 01:51:34 | [diff] [blame] | 362 | INTEGER = _PropertyTypeInfo(True, "INTEGER") |
| 363 | INT64 = _PropertyTypeInfo(True, "INT64") |
| 364 | DOUBLE = _PropertyTypeInfo(True, "DOUBLE") |
| 365 | BOOLEAN = _PropertyTypeInfo(True, "BOOLEAN") |
| 366 | STRING = _PropertyTypeInfo(True, "STRING") |
| 367 | ENUM = _PropertyTypeInfo(False, "ENUM") |
| 368 | ARRAY = _PropertyTypeInfo(False, "ARRAY") |
| 369 | REF = _PropertyTypeInfo(False, "REF") |
| 370 | CHOICES = _PropertyTypeInfo(False, "CHOICES") |
| 371 | OBJECT = _PropertyTypeInfo(False, "OBJECT") |
| 372 | FUNCTION = _PropertyTypeInfo(False, "FUNCTION") |
| 373 | BINARY = _PropertyTypeInfo(False, "BINARY") |
| 374 | ANY = _PropertyTypeInfo(False, "ANY") |
| 375 | ADDITIONAL_PROPERTIES = _PropertyTypeInfo(False, "ADDITIONAL_PROPERTIES") |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 376 | |
[email protected] | 4ba6bdc | 2012-06-18 20:40:14 | [diff] [blame] | 377 | def UnixName(name): |
[email protected] | 712eca0f | 2012-02-21 01:13:07 | [diff] [blame] | 378 | """Returns the unix_style name for a given lowerCamelCase string. |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 379 | """ |
[email protected] | df74dc4 | 2012-04-03 07:08:04 | [diff] [blame] | 380 | # First replace any lowerUpper patterns with lower_Upper. |
| 381 | s1 = re.sub('([a-z])([A-Z])', r'\1_\2', name) |
| 382 | # Now replace any ACMEWidgets patterns with ACME_Widgets |
| 383 | s2 = re.sub('([A-Z]+)([A-Z][a-z])', r'\1_\2', s1) |
| 384 | # Finally, replace any remaining periods, and make lowercase. |
| 385 | return s2.replace('.', '_').lower() |
[email protected] | cfe48490 | 2012-02-15 14:52:32 | [diff] [blame] | 386 | |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 387 | def _StripNamespace(name, namespace): |
| 388 | if name.startswith(namespace.name + '.'): |
| 389 | return name[len(namespace.name + '.'):] |
| 390 | return name |
| 391 | |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 392 | def _GetModelHierarchy(entity): |
[email protected] | feba21e | 2012-03-02 15:05:27 | [diff] [blame] | 393 | """Returns the hierarchy of the given model entity.""" |
| 394 | hierarchy = [] |
| 395 | while entity: |
| 396 | try: |
| 397 | hierarchy.append(entity.name) |
| 398 | except AttributeError: |
| 399 | hierarchy.append(repr(entity)) |
| 400 | entity = entity.parent |
| 401 | hierarchy.reverse() |
| 402 | return hierarchy |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 403 | |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 404 | def _AddTypes(model, json, namespace): |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 405 | """Adds Type objects to |model| contained in the 'types' field of |json|. |
| 406 | """ |
| 407 | model.types = {} |
| 408 | for type_json in json.get('types', []): |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 409 | type_ = Type(model, type_json['id'], type_json, namespace) |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 410 | model.types[type_.name] = type_ |
| 411 | |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 412 | def _AddFunctions(model, json, namespace): |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 413 | """Adds Function objects to |model| contained in the 'functions' field of |
| 414 | |json|. |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 415 | """ |
| 416 | model.functions = {} |
| 417 | for function_json in json.get('functions', []): |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 418 | function = Function(model, function_json, namespace, from_json=True) |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 419 | model.functions[function.name] = function |
| 420 | |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 421 | def _AddEvents(model, json, namespace): |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 422 | """Adds Function objects to |model| contained in the 'events' field of |json|. |
| 423 | """ |
| 424 | model.events = {} |
| 425 | for event_json in json.get('events', []): |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 426 | event = Function(model, event_json, namespace, from_client=True) |
[email protected] | b741e8f | 2012-07-16 21:47:24 | [diff] [blame] | 427 | model.events[event.name] = event |
| 428 | |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 429 | def _AddProperties(model, |
| 430 | json, |
| 431 | namespace, |
| 432 | from_json=False, |
| 433 | from_client=False): |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 434 | """Adds model.Property objects to |model| contained in the 'properties' field |
| 435 | of |json|. |
| 436 | """ |
| 437 | model.properties = {} |
| 438 | for name, property_json in json.get('properties', {}).items(): |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 439 | model.properties[name] = Property( |
| 440 | model, |
| 441 | name, |
| 442 | property_json, |
[email protected] | 0b255f00 | 2012-10-05 01:58:47 | [diff] [blame^] | 443 | namespace, |
[email protected] | 116f0a3a | 2012-04-19 04:22:38 | [diff] [blame] | 444 | from_json=from_json, |
| 445 | from_client=from_client) |