Chromium Code Reviews| 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 os.path | 5 import os.path |
| 6 import re | 6 import re |
| 7 import copy | 7 import copy |
| 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. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 function = Function(function_json) | 53 function = Function(function_json) |
| 54 self.functions[function.name] = function | 54 self.functions[function.name] = function |
| 55 | 55 |
| 56 class Type(object): | 56 class Type(object): |
| 57 """A Type defined in the json. | 57 """A Type defined in the json. |
| 58 | 58 |
| 59 Properties: | 59 Properties: |
| 60 - |name| the type name | 60 - |name| the type name |
| 61 - |description| the description of the type (if provided) | 61 - |description| the description of the type (if provided) |
| 62 - |properties| a map of property names to their model.Property | 62 - |properties| a map of property names to their model.Property |
| 63 - |from_api| indicates that the type is coming from the API caller | |
| 64 - |to_api| indicates that the type is going to the API caller | |
|
not at google - send to devlin
2012/02/26 23:51:53
This makes a lot more sense. I think it could stil
not at google - send to devlin
2012/02/26 23:53:34
Heh, fail, changed my mind right after writing tha
calamity
2012/02/27 04:57:30
Done. Changed to from_user/from_json
| |
| 63 """ | 65 """ |
| 64 def __init__(self, json): | 66 def __init__(self, json): |
| 65 self.name = json['id'] | 67 self.name = json['id'] |
| 66 self.description = json.get('description') | 68 self.description = json.get('description') |
| 69 self.from_api = True | |
| 70 self.to_api = True | |
| 67 self.properties = {} | 71 self.properties = {} |
| 68 for prop_name, prop_json in json['properties'].items(): | 72 for prop_name, prop_json in json['properties'].items(): |
| 69 self.properties[prop_name] = Property(prop_name, prop_json) | 73 self.properties[prop_name] = Property(prop_name, prop_json, |
| 74 from_api=True, | |
| 75 to_api=True) | |
| 70 | 76 |
| 71 class Callback(object): | 77 class Callback(object): |
| 72 """A callback parameter to a Function. | 78 """A callback parameter to a Function. |
| 73 | 79 |
| 74 Properties: | 80 Properties: |
| 75 - |params| the parameters to this callback. | 81 - |params| the parameters to this callback. |
| 76 """ | 82 """ |
| 77 def __init__(self, json): | 83 def __init__(self, json): |
| 78 params = json['parameters'] | 84 params = json['parameters'] |
| 79 self.params = [] | 85 self.params = [] |
| 80 if len(params) == 0: | 86 if len(params) == 0: |
| 81 return | 87 return |
| 82 elif len(params) == 1: | 88 elif len(params) == 1: |
| 83 param = params[0] | 89 param = params[0] |
| 84 self.params.append(Property(param['name'], param)) | 90 self.params.append(Property(param['name'], param, |
| 91 to_api=True)) | |
| 85 else: | 92 else: |
| 86 raise AssertionError("Callbacks can have at most a single parameter") | 93 raise AssertionError("Callbacks can have at most a single parameter") |
| 87 | 94 |
| 88 class Function(object): | 95 class Function(object): |
| 89 """A Function defined in the API. | 96 """A Function defined in the API. |
| 90 | 97 |
| 91 Properties: | 98 Properties: |
| 92 - |name| the function name | 99 - |name| the function name |
| 93 - |params| a list of parameters to the function (order matters). A separate | 100 - |params| a list of parameters to the function (order matters). A separate |
| 94 parameter is used for each choice of a 'choices' parameter. | 101 parameter is used for each choice of a 'choices' parameter. |
| 95 - |description| a description of the function (if provided) | 102 - |description| a description of the function (if provided) |
| 96 - |callback| the callback parameter to the function. There should be exactly | 103 - |callback| the callback parameter to the function. There should be exactly |
| 97 one | 104 one |
| 98 """ | 105 """ |
| 99 def __init__(self, json): | 106 def __init__(self, json): |
| 100 self.name = json['name'] | 107 self.name = json['name'] |
| 101 self.params = [] | 108 self.params = [] |
| 102 self.description = json['description'] | 109 self.description = json['description'] |
| 103 self.callback = None | 110 self.callback = None |
| 104 for param in json['parameters']: | 111 for param in json['parameters']: |
| 105 if param.get('type') == 'function': | 112 if param.get('type') == 'function': |
| 106 assert (not self.callback), self.name + " has more than one callback" | 113 assert (not self.callback), self.name + " has more than one callback" |
| 107 self.callback = Callback(param) | 114 self.callback = Callback(param) |
| 108 else: | 115 else: |
| 109 self.params.append(Property(param['name'], param)) | 116 self.params.append(Property(param['name'], param, |
| 110 assert (self.callback), self.name + " does not support callback" | 117 from_api=True)) |
| 111 | 118 |
| 112 class Property(object): | 119 class Property(object): |
| 113 """A property of a type OR a parameter to a function. | 120 """A property of a type OR a parameter to a function. |
| 114 | 121 |
| 122 Parameters: | |
| 123 from_api: indicates whether the property is coming from the API caller | |
| 124 to_api: indicates whether the property will to go to the API caller | |
| 125 | |
| 115 Properties: | 126 Properties: |
| 116 - |name| name of the property as in the json. This shouldn't change since | 127 - |name| name of the property as in the json. This shouldn't change since |
| 117 it is the key used to access DictionaryValues | 128 it is the key used to access DictionaryValues |
| 118 - |unix_name| the unix_style_name of the property. Used as variable name | 129 - |unix_name| the unix_style_name of the property. Used as variable name |
| 119 - |optional| a boolean representing whether the property is optional | 130 - |optional| a boolean representing whether the property is optional |
| 120 - |description| a description of the property (if provided) | 131 - |description| a description of the property (if provided) |
| 121 - |type_| the model.PropertyType of this property | 132 - |type_| the model.PropertyType of this property |
| 122 - |ref_type| the type that the REF property is referencing. Can be used to | 133 - |ref_type| the type that the REF property is referencing. Can be used to |
| 123 map to its model.Type | 134 map to its model.Type |
| 124 - |item_type| a model.Property representing the type of each element in an | 135 - |item_type| a model.Property representing the type of each element in an |
| 125 ARRAY | 136 ARRAY |
| 126 - |properties| the properties of an OBJECT parameter | 137 - |properties| the properties of an OBJECT parameter |
| 127 """ | 138 """ |
| 128 def __init__(self, name, json): | 139 def __init__(self, name, json, |
| 129 if not re.match('^[a-z][a-zA-Z0-9]*$', name): | 140 from_api=False, |
|
Yoyo Zhou
2012/02/24 23:39:47
Unless I'm reading something incorrectly, it looks
calamity
2012/02/27 04:57:30
The Property is used as a Type if it is an OBJECT.
| |
| 130 raise AssertionError('Name %s must be lowerCamelCase' % name) | 141 to_api=False): |
| 131 self.name = name | 142 self.name = name |
| 132 self._unix_name = _UnixName(self.name) | 143 self._unix_name = _UnixName(self.name) |
| 133 self._unix_name_used = False | 144 self._unix_name_used = False |
| 134 self.optional = json.get('optional', False) | 145 self.optional = json.get('optional', False) |
| 135 self.description = json.get('description') | 146 self.description = json.get('description') |
| 136 if '$ref' in json: | 147 if '$ref' in json: |
| 137 self.ref_type = json['$ref'] | 148 self.ref_type = json['$ref'] |
| 138 self.type_ = PropertyType.REF | 149 self.type_ = PropertyType.REF |
| 139 elif 'enum' in json: | 150 elif 'enum' in json: |
| 140 self.enum_values = [] | 151 self.enum_values = [] |
| 141 for value in json['enum']: | 152 for value in json['enum']: |
| 142 self.enum_values.append(value) | 153 self.enum_values.append(value) |
| 143 self.type_ = PropertyType.ENUM | 154 self.type_ = PropertyType.ENUM |
| 144 elif 'type' in json: | 155 elif 'type' in json: |
| 145 json_type = json['type'] | 156 json_type = json['type'] |
| 146 if json_type == 'string': | 157 if json_type == 'string': |
| 147 self.type_ = PropertyType.STRING | 158 self.type_ = PropertyType.STRING |
| 148 elif json_type == 'any': | 159 elif json_type == 'any': |
| 149 self.type_ = PropertyType.ANY | 160 self.type_ = PropertyType.ANY |
| 150 elif json_type == 'boolean': | 161 elif json_type == 'boolean': |
| 151 self.type_ = PropertyType.BOOLEAN | 162 self.type_ = PropertyType.BOOLEAN |
| 152 elif json_type == 'integer': | 163 elif json_type == 'integer': |
| 153 self.type_ = PropertyType.INTEGER | 164 self.type_ = PropertyType.INTEGER |
| 154 elif json_type == 'number': | 165 elif json_type == 'number': |
| 155 self.type_ = PropertyType.DOUBLE | 166 self.type_ = PropertyType.DOUBLE |
| 156 elif json_type == 'array': | 167 elif json_type == 'array': |
| 157 self.item_type = Property(name + "Element", json['items']) | 168 self.item_type = Property(name + "Element", json['items'], |
| 169 from_api, | |
| 170 to_api) | |
| 158 self.type_ = PropertyType.ARRAY | 171 self.type_ = PropertyType.ARRAY |
| 159 elif json_type == 'object': | 172 elif json_type == 'object': |
| 160 self.properties = {} | 173 self.properties = {} |
| 161 self.type_ = PropertyType.OBJECT | 174 self.type_ = PropertyType.OBJECT |
| 175 self.from_api = from_api | |
| 176 self.to_api = to_api | |
| 162 for key, val in json['properties'].items(): | 177 for key, val in json['properties'].items(): |
| 163 self.properties[key] = Property(key, val) | 178 self.properties[key] = Property(key, val, |
| 179 from_api, | |
| 180 to_api) | |
| 164 else: | 181 else: |
| 165 raise NotImplementedError(json_type) | 182 raise NotImplementedError(json_type) |
| 166 elif 'choices' in json: | 183 elif 'choices' in json: |
| 167 assert len(json['choices']), 'Choices has no choices\n%s' % json | 184 assert len(json['choices']), 'Choices has no choices\n%s' % json |
| 168 self.choices = {} | 185 self.choices = {} |
| 169 self.type_ = PropertyType.CHOICES | 186 self.type_ = PropertyType.CHOICES |
| 170 for choice_json in json['choices']: | 187 for choice_json in json['choices']: |
| 171 choice = Property(self.name, choice_json) | 188 choice = Property(self.name, choice_json, |
| 189 from_api, | |
| 190 to_api) | |
| 172 # A choice gets its unix_name set in | 191 # A choice gets its unix_name set in |
| 173 # cpp_type_generator.GetExpandedChoicesInParams | 192 # cpp_type_generator.GetExpandedChoicesInParams |
| 174 choice._unix_name = None | 193 choice._unix_name = None |
| 175 # The existence of any single choice is optional | 194 # The existence of any single choice is optional |
| 176 choice.optional = True | 195 choice.optional = True |
| 177 self.choices[choice.type_] = choice | 196 self.choices[choice.type_] = choice |
| 178 else: | 197 else: |
| 179 raise NotImplementedError(json) | 198 raise NotImplementedError(json) |
| 180 | 199 |
| 181 def GetUnixName(self): | 200 def GetUnixName(self): |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 CHOICES = _Info(False, "CHOICES") | 249 CHOICES = _Info(False, "CHOICES") |
| 231 OBJECT = _Info(False, "OBJECT") | 250 OBJECT = _Info(False, "OBJECT") |
| 232 ANY = _Info(False, "ANY") | 251 ANY = _Info(False, "ANY") |
| 233 | 252 |
| 234 def _UnixName(name): | 253 def _UnixName(name): |
| 235 """Returns the unix_style name for a given lowerCamelCase string. | 254 """Returns the unix_style name for a given lowerCamelCase string. |
| 236 """ | 255 """ |
| 237 return '_'.join([x.lower() | 256 return '_'.join([x.lower() |
| 238 for x in re.findall('[A-Z][a-z_]*', name[0].upper() + name[1:])]) | 257 for x in re.findall('[A-Z][a-z_]*', name[0].upper() + name[1:])]) |
| 239 | 258 |
| OLD | NEW |