OLD | NEW |
---|---|
(Empty) | |
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 | |
5 import os.path | |
6 | |
7 class Model(object): | |
8 """Model of the API. | |
9 """ | |
10 def __init__(self): | |
11 self.namespaces = {} | |
12 self.types = {} | |
13 | |
14 def add_namespace(self, json, root_namespace, parent_path, filename_suffix): | |
not at google - send to devlin
2012/01/17 05:42:32
parent_path is a confusing name, it's the source f
calamity
2012/01/18 05:43:08
I'm a little iffy with global substitution because
| |
15 """Add a namespace's json to the model. | |
not at google - send to devlin
2012/01/17 05:42:32
.. "if it has a "compile" property set to true.
Re
calamity
2012/01/18 05:43:08
Done.
| |
16 """ | |
17 if not json.get('compile'): | |
18 return None | |
19 namespace = Namespace(json, root_namespace, | |
not at google - send to devlin
2012/01/17 05:42:32
extra space.
calamity
2012/01/18 05:43:08
Done.
| |
20 parent_path, self, filename_suffix) | |
21 self.namespaces[namespace.name] = namespace | |
22 for tipe in namespace.types.values(): | |
23 self.types[tipe.name] = namespace | |
24 return namespace | |
25 | |
26 class Namespace(object): | |
27 """An API namespace. | |
28 """ | |
29 def __init__(self, json, root_namespace, | |
30 parent_path, parent_model, filename_suffix): | |
31 self.name = json['namespace'] | |
32 self.root_namespace = root_namespace | |
33 self.parent_path = parent_path | |
34 self.parent_dir, self.parent_filename = os.path.split(parent_path) | |
not at google - send to devlin
2012/01/17 05:42:32
from what I can tell, this property is unnecessary
| |
35 self.parent_model = parent_model | |
not at google - send to devlin
2012/01/17 05:42:32
what's parent_model used for?
calamity
2012/01/18 05:43:08
Removed.
| |
36 self.filename = self.name + filename_suffix | |
37 self.type_dependencies = {} | |
38 self.types = {} | |
39 self.functions = {} | |
40 for type_json in json['types']: | |
41 tipe = Type(type_json) | |
42 self.types[tipe.name] = tipe | |
43 for function_json in json['functions']: | |
44 if not function_json.get('nocompile'): | |
45 function = Function(function_json) | |
46 self.functions[function.name] = function | |
47 | |
48 class Type(object): | |
49 """A Type defined in the json. | |
50 """ | |
51 def __init__(self, json): | |
52 self.name = json['id'] | |
53 self.description = json.get('description') | |
54 self.properties = {} | |
55 for prop_name, prop_json in json['properties'].items(): | |
56 self.properties[prop_name] = Property(prop_name, prop_json) | |
57 | |
58 class Callback(object): | |
59 """A callback parameter to a Function. | |
60 """ | |
61 def __init__(self, json): | |
62 self.param = None | |
63 # Callbacks can have at most a single parameter | |
64 assert len(json['parameters']) <= 1 | |
65 for param in json['parameters']: | |
66 self.param = Property(param['name'], param) | |
not at google - send to devlin
2012/01/17 05:42:32
yeah using a loop looks a bit strage given the lin
calamity
2012/01/18 05:43:08
Done.
| |
67 | |
68 class Function(object): | |
69 """A Function defined in the API. | |
70 """ | |
71 def __init__(self, json): | |
72 self.name = json['name'] | |
73 self.params = [] | |
74 self.description = json['description'] | |
75 self.callback = None | |
76 self.type_dependencies = {} | |
77 for param in json['parameters']: | |
78 if param.get('type') == 'function': | |
79 self.callback = Callback(param) | |
not at google - send to devlin
2012/01/17 05:42:32
assert not self.callback
calamity
2012/01/18 05:43:08
Done.
| |
80 continue | |
81 self.params.append(Property(param['name'], param)) | |
not at google - send to devlin
2012/01/17 05:42:32
just put in an else rather than a continue?
calamity
2012/01/18 05:43:08
Done.
| |
82 if not self.callback: | |
83 raise KeyError | |
not at google - send to devlin
2012/01/17 05:42:32
i'm not sure using a KeyError is appropriate? rea
calamity
2012/01/18 05:43:08
Agreed. Left this as an AssertionError for now.
| |
84 | |
85 # TODO(calamity): handle Enum/choices | |
86 class Property(object): | |
87 """A property of a type OR a parameter to a function. | |
88 | |
89 Members will change based on PropertyType. Check self.type to determine which | |
90 members actually exist. | |
91 """ | |
92 def __init__(self, name, json): | |
93 self.name = name | |
94 self.optional = json.get('optional', False) | |
95 self.description = json.get('description') | |
96 simple_types = ['boolean', 'integer', 'string', 'double'] | |
97 # TODO(calamity) maybe check for circular refs? could that be a problem? | |
98 if '$ref' in json: | |
99 self.json_type = json['$ref'] | |
not at google - send to devlin
2012/01/17 05:42:32
the distinction between "json_type", "item_type",
calamity
2012/01/18 05:43:08
Agreed. That's what the comment I made last revisi
| |
100 self.type = PropertyType.REF | |
101 elif 'type' in json: | |
102 json_type = json['type'] | |
103 if json_type in simple_types: | |
104 self.json_type = json_type | |
105 self.type = PropertyType.FUNDAMENTAL | |
106 elif json_type == 'array': | |
107 self.item_type = Property(name + "_inner", json['items']) | |
108 self.type = PropertyType.ARRAY | |
109 elif json_type == 'object': | |
110 self.properties = {} | |
111 self.type = PropertyType.OBJECT | |
112 for key, val in json['properties'].items(): | |
113 self.properties[key] = Property(key, val) | |
114 else: | |
115 raise NotImplementedError(json_type) | |
116 elif 'choices' in json: | |
117 self.type = PropertyType.CHOICES | |
118 self.choices = {} | |
119 | |
120 class PropertyType(object): | |
121 """Enum of different types of properties/parameters. | |
122 """ | |
123 STRING, FUNDAMENTAL, ARRAY, REF, CHOICES, OBJECT = range(6) | |
OLD | NEW |