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

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

Issue 9600050: Add IDL capability to json_schema_compiler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch Set 1 Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
(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
asargent_no_longer_on_chrome 2012/03/06 22:27:57 nit: might be worth having an overview comment her
5 import os.path
6 import sys
7
8 idl_generators_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
9 os.pardir, os.pardir, 'ppapi', 'generators')
10 if idl_generators_path not in sys.path:
11 sys.path.insert(0, idl_generators_path)
12 import idl_parser
13
14 class Callspec(object):
15 '''
16 Given a Callspec node representing an IDL namespace, converts into a Python
asargent_no_longer_on_chrome 2012/03/06 22:27:57 "an IDL namespace" -> "an IDL function declaration
17 dictionary that the JSON schema compiler expects to see.
18 '''
19 def __init__(self, callspec_node):
20 self.node = callspec_node
21
22 def process(self, refs):
23 parameters = []
24 for node in self.node.children:
25 parameters.append(Param(node).process(refs))
26 return self.node.GetName(), parameters
27
28 class Param(object):
29 '''
30 Given a Param node representing an IDL namespace, converts into a Python
asargent_no_longer_on_chrome 2012/03/06 22:27:57 "an IDL namespace" -> "a function parameter"
31 dictionary that the JSON schema compiler expects to see.
32 '''
33 def __init__(self, param_node):
34 self.node = param_node
35
36 def process(self, refs):
37 properties = { 'name': self.node.GetName() }
38 return dict(properties.items() +
39 Typeref(self.node.GetProperty(
40 'TYPEREF'), self.node).process(refs).items())
41
42 class Dictionary(object):
43 '''
44 Given a Dictionary node representing an IDL namespace, converts into a Python
asargent_no_longer_on_chrome 2012/03/06 22:27:57 "an IDL namespace" -> "an IDL dictionary"
45 dictionary that the JSON schema compiler expects to see.
46 '''
47 def __init__(self, dictionary_node):
48 self.node = dictionary_node
49
50 def process(self, refs):
51 properties = {}
52 for node in self.node.children:
53 if node.cls == 'Member':
54 k, v = Member(node).process(refs)
55 properties[k] = v
56 return { 'id': self.node.GetName(),
57 'properties': properties,
58 'type': 'object' }
59
60 class Member(object):
61 '''
62 Given a Member node representing an IDL namespace, converts into a Python
asargent_no_longer_on_chrome 2012/03/06 22:27:57 "an IDL namespace" -> "an IDL dictionary or interf
63 dictionary that the JSON schema compiler expects to see.
64 '''
65 def __init__(self, member_node):
66 self.node = member_node
67
68 def process(self, refs):
69 properties = {}
70 name = self.node.GetName()
71 if self.node.GetProperty('OPTIONAL'):
72 properties['optional'] = True
73 if self.node.GetProperty('nodoc'):
74 properties['nodoc'] = True
75 for node in self.node.children:
76 if node.cls == 'Callspec':
77 name, parameters = Callspec(node).process(refs)
asargent_no_longer_on_chrome 2012/03/06 22:27:57 It would be nice to enforce somewhere that this ca
78 properties['parameters'] = parameters
79 properties['name'] = name
80 typeref = self.node.GetProperty('TYPEREF')
81 if typeref == 'void':
asargent_no_longer_on_chrome 2012/03/06 22:27:57 Instead of looking for typeref==void to tell if th
miket_OOO 2012/03/07 00:23:01 Done.
82 properties['type'] = 'function'
83 else:
84 properties = dict(properties.items() +
85 Typeref(typeref, self.node).process(refs).items())
asargent_no_longer_on_chrome 2012/03/06 22:27:57 would it be clearer if instead of merging dict's h
miket_OOO 2012/03/07 00:23:01 Yes, great idea! Done.
86 return name, properties
87
88 class Typeref(object):
89 '''
90 Given a TYPEREF property representing an IDL namespace, converts into a
asargent_no_longer_on_chrome 2012/03/06 22:27:57 "an IDL namespace" -> "the type of a dictionary me
91 Python dictionary that the JSON schema compiler expects to see.
92 '''
93 def __init__(self, typeref, parent):
94 self.typeref = typeref
95 self.parent = parent
96
97 def process(self, refs):
98 properties = {}
99 if self.typeref == 'DOMString':
100 properties['type'] = 'string'
101 elif self.typeref == 'boolean':
102 properties['type'] = 'boolean'
103 elif self.typeref == 'long':
104 properties['type'] = 'integer'
105 elif self.typeref is None:
106 properties['type'] = 'function'
107 else:
108 try:
109 properties = refs[self.typeref]
110 except KeyError, e:
111 properties['$ref'] = self.typeref
asargent_no_longer_on_chrome 2012/03/06 22:27:57 Someday it would be cool to take note of not-yet-d
miket_OOO 2012/03/07 00:23:01 Agreed -- though at present, it's caught loudly an
asargent_no_longer_on_chrome 2012/03/07 00:42:09 Good point!
112 return properties
113
114 class Namespace(object):
115 '''
116 Given an IDLNode representing an IDL namespace, converts into a Python
117 dictionary that the JSON schema compiler expects to see.
118 '''
119
120 def __init__(self, namespace_node):
121 self.namespace = namespace_node
122 self.events = []
123 self.functions = []
124 self.types = []
125 self.refs = {}
126
127 def process(self):
128 for node in self.namespace.children:
129 cls = node.cls
130 if cls == "Dictionary":
131 self.types.append(Dictionary(node).process(self.refs))
132 elif cls == "Callback":
133 k, v = Member(node).process(self.refs)
134 self.refs[k] = v
135 elif cls == "Interface" and node.GetName() == "Functions":
136 self.functions = self.process_interface(node)
137 elif cls == "Interface" and node.GetName() == "Events":
138 self.events = self.process_interface(node)
139
140 return { 'events': self.events,
141 'functions': self.functions,
142 'types': self.types,
143 'namespace': self.namespace.GetName() }
asargent_no_longer_on_chrome 2012/03/06 22:27:57 looks like you're missing the 'nodoc' attribute if
miket_OOO 2012/03/07 00:23:01 Fixed. I know I'm being inconsistent here because
asargent_no_longer_on_chrome 2012/03/07 00:42:09 Good point. (It was actually seeing the code elsew
144
145 def process_interface(self, node):
146 members = []
147 for member in node.children:
148 if member.cls == 'Member':
149 name, properties = Member(member).process(self.refs)
150 members.append(properties)
151 return members
152
153 class IDLSchema(object):
154 '''
155 Given a list of IDLNodes and IDLAttributes, converts into a Python list
156 of api_defs that the JSON schema compiler expects to see.
157 '''
158
159 def __init__(self, idl):
160 self.idl = idl
161
162 def process(self):
163 namespaces = []
164 for node in self.idl:
165 if node.cls == 'Namespace':
166 namespace = Namespace(node)
167 namespaces.append(namespace.process())
168 return namespaces
169
170 def Load(filename):
171 '''
172 Given the filename of an IDL file, parses it and returns an equivalent
173 Python dictionary in a format that the JSON schema compiler expects to see.
174 '''
175
176 f = open(filename, 'r')
177 contents = f.read()
178 f.close()
179
180 idl = idl_parser.IDLParser().ParseData(contents, filename)
181 idl_schema = IDLSchema(idl)
182 return idl_schema.process()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698