OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2011 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 ModelT(object): | |
not at google - send to devlin
2012/01/12 06:01:05
consider putting this in a model/ directory, with
calamity
2012/01/16 04:01:06
Any particular reason for doing this?
not at google - send to devlin
2012/01/17 01:59:24
Yes, so that you can have
- a different file for
| |
8 """Model of the API.""" | |
not at google - send to devlin
2012/01/12 06:01:05
also, why is every model object suffixed with T?
calamity
2012/01/12 22:59:20
It was done so I had some sort of naming conventio
| |
9 def __init__(self): | |
10 self.namespaces = {} | |
11 self.types = {} | |
12 | |
13 def add_namespace(self, json, root_namespace, parent_path, filename_suffix): | |
14 """Add a namespace's json to the model.""" | |
15 if not json.get('generate'): | |
16 return None | |
17 namespace = NamespaceT(json, root_namespace, | |
18 parent_path, self, filename_suffix) | |
19 self.namespaces[namespace.name] = namespace | |
20 for tipe in namespace.types.values(): | |
21 self.types[tipe.name] = namespace | |
22 return namespace | |
23 | |
24 class NamespaceT(object): | |
25 """An API namespace.""" | |
26 def __init__(self, json, root_namespace, | |
27 parent_path, parent_model, filename_suffix): | |
28 self.name = json['namespace'] | |
29 self.root_namespace = root_namespace | |
30 self.parent_path = parent_path | |
31 self.parent_dir, self.parent_filename = os.path.split(parent_path) | |
32 self.parent_model = parent_model | |
33 self.filename = self.name + filename_suffix | |
34 self.type_dependencies = {} | |
35 self.types = {} | |
36 self.functions = {} | |
37 for type_json in json['types']: | |
38 tipe = TypeT(type_json) | |
39 self.types[tipe.name] = tipe | |
40 for function_json in json['functions']: | |
41 if not function_json.get('nogenerate'): | |
42 function = FunctionT(function_json) | |
43 self.functions[function.name] = function | |
44 | |
45 class TypeT(object): | |
46 """A Type defined in the json.""" | |
47 def __init__(self, json): | |
48 self.name = json['id'] | |
49 self.description = json.get('description') | |
50 self.properties = {} | |
51 for prop_name, prop_json in json['properties'].items(): | |
52 self.properties[prop_name] = PropertyT(prop_name, prop_json) | |
53 | |
54 class CallbackT(object): | |
55 """A callback parameter to a Function.""" | |
56 def __init__(self, json): | |
57 self.param = None | |
58 assert len(json['parameters']) <= 1 | |
59 for param in json['parameters']: | |
60 self.param = PropertyT(param['name'], param) | |
61 | |
62 class FunctionT(object): | |
63 """A Function defined in the API.""" | |
64 def __init__(self, json): | |
65 self.name = json['name'] | |
66 self.params = [] | |
67 self.description = json['description'] | |
68 self.callback = None | |
69 self.type_dependencies = {} | |
70 for param in json['parameters']: | |
71 if param.get('type') == 'function': | |
72 # TODO(calamity): Deal with this | |
73 self.callback = CallbackT(param) | |
74 continue | |
75 self.params.append(PropertyT(param['name'], param)) | |
76 if not self.callback: | |
77 raise KeyError | |
78 | |
79 # TODO(calamity): handle Enum/choices | |
80 class PropertyT(object): | |
81 """A property of a type OR a parameter to a function. | |
82 | |
83 A polymorphic type, check self.type to determine which | |
84 members actually exist. | |
85 """ | |
86 def __init__(self, name, json): | |
87 self.name = name | |
88 self.optional = json.get('optional', False) | |
89 self.description = json.get('description') | |
90 simple_types = ['boolean', 'integer', 'string', 'double'] | |
91 # TODO(calamity) maybe check for circular refs? could that be a problem? | |
92 if '$ref' in json: | |
93 self.json_type = json['$ref'] | |
94 self.type = PropertyType.REF | |
95 elif 'type' in json: | |
96 json_type = json['type'] | |
97 if json_type in simple_types: | |
98 self.json_type = json_type | |
99 self.type = PropertyType.FUNDAMENTAL | |
100 elif json_type == 'array': | |
101 self.item_type = PropertyT(name + "_inner", json['items']) | |
102 self.type = PropertyType.ARRAY | |
103 elif json_type == 'object': | |
104 self.properties = {} | |
105 self.type = PropertyType.OBJECT | |
106 for key, val in json['properties'].items(): | |
107 self.properties[key] = PropertyT(key, val) | |
108 else: | |
109 raise NotImplementedError(json_type) | |
110 elif 'choices' in json: | |
111 self.type = PropertyType.CHOICES | |
112 self.choices = {} | |
113 | |
114 class PropertyType(object): | |
115 """Enum of different types of properties/parameters.""" | |
116 STRING, FUNDAMENTAL, ARRAY, REF, CHOICES, OBJECT = range(6) | |
OLD | NEW |