Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: tools/json_schema_compiler/model.py

Issue 9114036: Code generation for extensions api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: some rework Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 Model(object):
8 """Model of the API."""
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('compile'):
16 return None
17 namespace = Namespace(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 Namespace(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 = Type(type_json)
39 self.types[tipe.name] = tipe
40 for function_json in json['functions']:
41 if not function_json.get('nocompile'):
42 function = Function(function_json)
43 self.functions[function.name] = function
44
45 class Type(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] = Property(prop_name, prop_json)
53
54 class Callback(object):
55 """A callback parameter to a Function."""
56 def __init__(self, json):
57 self.param = None
58 assert len(json['parameters']) <= 1
not at google - send to devlin 2012/01/13 06:45:55 add a message like, "Callbacks can have at most a
calamity 2012/01/16 04:01:06 Done.
59 for param in json['parameters']:
60 self.param = Property(param['name'], param)
61
62 class Function(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 = Callback(param)
74 continue
75 self.params.append(Property(param['name'], param))
76 if not self.callback:
77 raise KeyError
78
79 # TODO(calamity): handle Enum/choices
80 class Property(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.
not at google - send to devlin 2012/01/13 02:14:09 not sure what this sentence means...
not at google - send to devlin 2012/01/17 05:42:32 Ah I see. How about like "This is a union type, c
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 = Property(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] = Property(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)
calamity 2012/01/16 04:01:06 PropertyType.STRING is unused. On dev branch, FUND
not at google - send to devlin 2012/01/17 01:59:24 I'm missing the context of this comment.
calamity 2012/01/18 05:43:08 Just a note in case you were wondering why Propert
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698