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 - |serializable| indicates whether the Type should support conversion back to | |
64 the input format | |
not at google - send to devlin
2012/02/24 03:53:05
i don't understand this comment in isolation. what
calamity
2012/02/24 15:10:40
Made the changes we discussed. Settled on to_api a
| |
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.serializable = True | |
67 self.properties = {} | 70 self.properties = {} |
68 for prop_name, prop_json in json['properties'].items(): | 71 for prop_name, prop_json in json['properties'].items(): |
69 self.properties[prop_name] = Property(prop_name, prop_json) | 72 self.properties[prop_name] = Property(prop_name, prop_json, False) |
70 | 73 |
71 class Callback(object): | 74 class Callback(object): |
72 """A callback parameter to a Function. | 75 """A callback parameter to a Function. |
73 | 76 |
74 Properties: | 77 Properties: |
75 - |params| the parameters to this callback. | 78 - |params| the parameters to this callback. |
76 """ | 79 """ |
77 def __init__(self, json): | 80 def __init__(self, json): |
78 params = json['parameters'] | 81 params = json['parameters'] |
79 self.params = [] | 82 self.params = [] |
80 if len(params) == 0: | 83 if len(params) == 0: |
81 return | 84 return |
82 elif len(params) == 1: | 85 elif len(params) == 1: |
83 param = params[0] | 86 param = params[0] |
84 self.params.append(Property(param['name'], param)) | 87 self.params.append(Property(param['name'], param, True)) |
85 else: | 88 else: |
86 raise AssertionError("Callbacks can have at most a single parameter") | 89 raise AssertionError("Callbacks can have at most a single parameter") |
87 | 90 |
88 class Function(object): | 91 class Function(object): |
89 """A Function defined in the API. | 92 """A Function defined in the API. |
90 | 93 |
91 Properties: | 94 Properties: |
92 - |name| the function name | 95 - |name| the function name |
93 - |params| a list of parameters to the function (order matters). A separate | 96 - |params| a list of parameters to the function (order matters). A separate |
94 parameter is used for each choice of a 'choices' parameter. | 97 parameter is used for each choice of a 'choices' parameter. |
95 - |description| a description of the function (if provided) | 98 - |description| a description of the function (if provided) |
96 - |callback| the callback parameter to the function. There should be exactly | 99 - |callback| the callback parameter to the function. There should be exactly |
97 one | 100 one |
98 """ | 101 """ |
99 def __init__(self, json): | 102 def __init__(self, json): |
100 self.name = json['name'] | 103 self.name = json['name'] |
101 self.params = [] | 104 self.params = [] |
102 self.description = json['description'] | 105 self.description = json['description'] |
103 self.callback = None | 106 self.callback = None |
104 for param in json['parameters']: | 107 for param in json['parameters']: |
105 if param.get('type') == 'function': | 108 if param.get('type') == 'function': |
106 assert (not self.callback), self.name + " has more than one callback" | 109 assert (not self.callback), self.name + " has more than one callback" |
107 self.callback = Callback(param) | 110 self.callback = Callback(param) |
108 else: | 111 else: |
109 self.params.append(Property(param['name'], param)) | 112 self.params.append(Property(param['name'], param, False)) |
110 assert (self.callback), self.name + " does not support callback" | |
111 | 113 |
112 class Property(object): | 114 class Property(object): |
113 """A property of a type OR a parameter to a function. | 115 """A property of a type OR a parameter to a function. |
114 | 116 |
115 Properties: | 117 Properties: |
116 - |name| name of the property as in the json. This shouldn't change since | 118 - |name| name of the property as in the json. This shouldn't change since |
117 it is the key used to access DictionaryValues | 119 it is the key used to access DictionaryValues |
118 - |unix_name| the unix_style_name of the property. Used as variable name | 120 - |unix_name| the unix_style_name of the property. Used as variable name |
119 - |optional| a boolean representing whether the property is optional | 121 - |optional| a boolean representing whether the property is optional |
120 - |description| a description of the property (if provided) | 122 - |description| a description of the property (if provided) |
121 - |type_| the model.PropertyType of this property | 123 - |type_| the model.PropertyType of this property |
122 - |ref_type| the type that the REF property is referencing. Can be used to | 124 - |ref_type| the type that the REF property is referencing. Can be used to |
123 map to its model.Type | 125 map to its model.Type |
124 - |item_type| a model.Property representing the type of each element in an | 126 - |item_type| a model.Property representing the type of each element in an |
125 ARRAY | 127 ARRAY |
126 - |properties| the properties of an OBJECT parameter | 128 - |properties| the properties of an OBJECT parameter |
not at google - send to devlin
2012/02/24 03:53:05
add |serializable|
calamity
2012/02/24 15:10:40
Done.
| |
127 """ | 129 """ |
128 def __init__(self, name, json): | 130 def __init__(self, name, json, serializable_objects): |
not at google - send to devlin
2012/02/24 03:53:05
this name makes it sound like an array or somethin
Yoyo Zhou
2012/02/24 04:02:22
objects_serializable would be a clearer name, I th
| |
129 if not re.match('^[a-z][a-zA-Z0-9]*$', name): | |
130 raise AssertionError('Name %s must be lowerCamelCase' % name) | |
not at google - send to devlin
2012/02/24 03:53:05
is it worthwhile having a warning here? are there
calamity
2012/02/24 15:10:40
Minimal, at worst, names might be dodgy (possibly
| |
131 self.name = name | 131 self.name = name |
132 self._unix_name = _UnixName(self.name) | 132 self._unix_name = _UnixName(self.name) |
133 self._unix_name_used = False | 133 self._unix_name_used = False |
134 self.optional = json.get('optional', False) | 134 self.optional = json.get('optional', False) |
135 self.description = json.get('description') | 135 self.description = json.get('description') |
136 if '$ref' in json: | 136 if '$ref' in json: |
137 self.ref_type = json['$ref'] | 137 self.ref_type = json['$ref'] |
138 self.type_ = PropertyType.REF | 138 self.type_ = PropertyType.REF |
139 elif 'enum' in json: | 139 elif 'enum' in json: |
140 self.enum_values = [] | 140 self.enum_values = [] |
141 for value in json['enum']: | 141 for value in json['enum']: |
142 self.enum_values.append(value) | 142 self.enum_values.append(value) |
143 self.type_ = PropertyType.ENUM | 143 self.type_ = PropertyType.ENUM |
144 elif 'type' in json: | 144 elif 'type' in json: |
145 json_type = json['type'] | 145 json_type = json['type'] |
146 if json_type == 'string': | 146 if json_type == 'string': |
147 self.type_ = PropertyType.STRING | 147 self.type_ = PropertyType.STRING |
148 elif json_type == 'any': | 148 elif json_type == 'any': |
149 self.type_ = PropertyType.ANY | 149 self.type_ = PropertyType.ANY |
150 elif json_type == 'boolean': | 150 elif json_type == 'boolean': |
151 self.type_ = PropertyType.BOOLEAN | 151 self.type_ = PropertyType.BOOLEAN |
152 elif json_type == 'integer': | 152 elif json_type == 'integer': |
153 self.type_ = PropertyType.INTEGER | 153 self.type_ = PropertyType.INTEGER |
154 elif json_type == 'number': | 154 elif json_type == 'number': |
155 self.type_ = PropertyType.DOUBLE | 155 self.type_ = PropertyType.DOUBLE |
156 elif json_type == 'array': | 156 elif json_type == 'array': |
157 self.item_type = Property(name + "Element", json['items']) | 157 self.item_type = Property(name + "Element", json['items'], False) |
158 self.type_ = PropertyType.ARRAY | 158 self.type_ = PropertyType.ARRAY |
159 elif json_type == 'object': | 159 elif json_type == 'object': |
160 self.properties = {} | 160 self.properties = {} |
161 self.type_ = PropertyType.OBJECT | 161 self.type_ = PropertyType.OBJECT |
162 self.serializable = serializable_objects | |
Yoyo Zhou
2012/02/24 04:02:22
self.type_.serializable?
calamity
2012/02/24 15:10:40
Now that I've switched this to to_api/from_api, th
| |
162 for key, val in json['properties'].items(): | 163 for key, val in json['properties'].items(): |
163 self.properties[key] = Property(key, val) | 164 self.properties[key] = Property(key, val, serializable_objects) |
164 else: | 165 else: |
165 raise NotImplementedError(json_type) | 166 raise NotImplementedError(json_type) |
166 elif 'choices' in json: | 167 elif 'choices' in json: |
167 assert len(json['choices']), 'Choices has no choices\n%s' % json | 168 assert len(json['choices']), 'Choices has no choices\n%s' % json |
168 self.choices = {} | 169 self.choices = {} |
169 self.type_ = PropertyType.CHOICES | 170 self.type_ = PropertyType.CHOICES |
170 for choice_json in json['choices']: | 171 for choice_json in json['choices']: |
171 choice = Property(self.name, choice_json) | 172 choice = Property(self.name, choice_json, serializable_objects) |
172 # A choice gets its unix_name set in | 173 # A choice gets its unix_name set in |
173 # cpp_type_generator.GetExpandedChoicesInParams | 174 # cpp_type_generator.GetExpandedChoicesInParams |
174 choice._unix_name = None | 175 choice._unix_name = None |
175 # The existence of any single choice is optional | 176 # The existence of any single choice is optional |
176 choice.optional = True | 177 choice.optional = True |
177 self.choices[choice.type_] = choice | 178 self.choices[choice.type_] = choice |
178 else: | 179 else: |
179 raise NotImplementedError(json) | 180 raise NotImplementedError(json) |
180 | 181 |
181 def GetUnixName(self): | 182 def GetUnixName(self): |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 CHOICES = _Info(False, "CHOICES") | 231 CHOICES = _Info(False, "CHOICES") |
231 OBJECT = _Info(False, "OBJECT") | 232 OBJECT = _Info(False, "OBJECT") |
232 ANY = _Info(False, "ANY") | 233 ANY = _Info(False, "ANY") |
233 | 234 |
234 def _UnixName(name): | 235 def _UnixName(name): |
235 """Returns the unix_style name for a given lowerCamelCase string. | 236 """Returns the unix_style name for a given lowerCamelCase string. |
236 """ | 237 """ |
237 return '_'.join([x.lower() | 238 return '_'.join([x.lower() |
238 for x in re.findall('[A-Z][a-z_]*', name[0].upper() + name[1:])]) | 239 for x in re.findall('[A-Z][a-z_]*', name[0].upper() + name[1:])]) |
239 | 240 |
OLD | NEW |