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

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

Issue 9309044: Supporting more APIs with json_schema_compiler (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: rework, add a couple of tests Created 8 years, 10 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
« no previous file with comments | « tools/json_schema_compiler/h_generator.py ('k') | tools/json_schema_compiler/test/array.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 8
8 class Model(object): 9 class Model(object):
9 """Model of all namespaces that comprise an API. 10 """Model of all namespaces that comprise an API.
10 11
11 Properties: 12 Properties:
12 - |namespaces| a map of a namespace name to its model.Namespace 13 - |namespaces| a map of a namespace name to its model.Namespace
13 """ 14 """
14 def __init__(self): 15 def __init__(self):
15 self.namespaces = {} 16 self.namespaces = {}
16 17
17 def AddNamespace(self, json, source_file): 18 def AddNamespace(self, json, source_file):
18 """Add a namespace's json to the model if it doesn't have "nocompile" 19 """Add a namespace's json to the model if it doesn't have "nocompile"
19 property set to true. Returns the new namespace or None if a namespace 20 property set to true. Returns the new namespace or None if a namespace
20 wasn't added. 21 wasn't added.
21 """ 22 """
22 if json.get('nocompile', False): 23 if json.get('nocompile', False):
23 return None 24 return None
24 namespace = Namespace(json, source_file) 25 namespace = Namespace(json, source_file)
25 self.namespaces[namespace.name] = namespace 26 self.namespaces[namespace.name] = namespace
26 return namespace 27 return namespace
27 28
28 class Namespace(object): 29 class Namespace(object):
29 """An API namespace. 30 """An API namespace.
30 31
31 Properties: 32 Properties:
32 - |name| the name of the namespace 33 - |name| the name of the namespace
33 - |source_file_dir| the directory component of the file that contained the 34 - |unix_name| the unix_name of the namespace
34 namespace definition 35 - |source_file| the file that contained the namespace definition
35 - |source_file_filename| the filename component of the file that contained 36 - |source_file_dir| the directory component of |source_file|
36 the namespace definition 37 - |source_file_filename| the filename component of |source_file|
37 - |types| a map of type names to their model.Type 38 - |types| a map of type names to their model.Type
38 - |functions| a map of function names to their model.Function 39 - |functions| a map of function names to their model.Function
39 """ 40 """
40 def __init__(self, json, source_file): 41 def __init__(self, json, source_file):
41 self.name = json['namespace'] 42 self.name = json['namespace']
43 self.unix_name = _UnixName(self.name)
42 self.source_file = source_file 44 self.source_file = source_file
43 self.source_file_dir, self.source_file_filename = os.path.split(source_file) 45 self.source_file_dir, self.source_file_filename = os.path.split(source_file)
44 self.types = {} 46 self.types = {}
45 self.functions = {} 47 self.functions = {}
46 for type_json in json['types']: 48 for type_json in json['types']:
47 type_ = Type(type_json) 49 type_ = Type(type_json)
48 self.types[type_.name] = type_ 50 self.types[type_.name] = type_
49 for function_json in json['functions']: 51 for function_json in json['functions']:
50 if not function_json.get('nocompile', False): 52 if not function_json.get('nocompile', False):
51 function = Function(function_json) 53 function = Function(function_json)
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 """ 188 """
187 if unix_name == self._unix_name: 189 if unix_name == self._unix_name:
188 return 190 return
189 if self._unix_name_used: 191 if self._unix_name_used:
190 raise AttributeError( 192 raise AttributeError(
191 'Cannot set the unix_name on %s; ' 193 'Cannot set the unix_name on %s; '
192 'it is already used elsewhere as %s' % 194 'it is already used elsewhere as %s' %
193 (self.name, self._unix_name)) 195 (self.name, self._unix_name))
194 self._unix_name = unix_name 196 self._unix_name = unix_name
195 197
198 def Copy(self):
199 """Makes a copy of this model.Property object and allow the unix_name to be
200 set again.
201 """
202 property_copy = copy.copy(self)
203 property_copy._unix_name_used = False
204 return property_copy
205
196 unix_name = property(GetUnixName, SetUnixName) 206 unix_name = property(GetUnixName, SetUnixName)
197 207
198 class PropertyType(object): 208 class PropertyType(object):
199 """Enum of different types of properties/parameters. 209 """Enum of different types of properties/parameters.
200 """ 210 """
201 class _Info(object): 211 class _Info(object):
202 def __init__(self, is_fundamental, name): 212 def __init__(self, is_fundamental, name):
203 self.is_fundamental = is_fundamental 213 self.is_fundamental = is_fundamental
204 self.name = name 214 self.name = name
205 215
206 def __repr__(self): 216 def __repr__(self):
207 return self.name 217 return self.name
208 218
209 INTEGER = _Info(True, "INTEGER") 219 INTEGER = _Info(True, "INTEGER")
210 DOUBLE = _Info(True, "DOUBLE") 220 DOUBLE = _Info(True, "DOUBLE")
211 BOOLEAN = _Info(True, "BOOLEAN") 221 BOOLEAN = _Info(True, "BOOLEAN")
212 STRING = _Info(True, "STRING") 222 STRING = _Info(True, "STRING")
213 ARRAY = _Info(False, "ARRAY") 223 ARRAY = _Info(False, "ARRAY")
214 REF = _Info(False, "REF") 224 REF = _Info(False, "REF")
215 CHOICES = _Info(False, "CHOICES") 225 CHOICES = _Info(False, "CHOICES")
216 OBJECT = _Info(False, "OBJECT") 226 OBJECT = _Info(False, "OBJECT")
217 ANY = _Info(False, "ANY") 227 ANY = _Info(False, "ANY")
218 228
219 def _UnixName(name): 229 def _UnixName(name):
220 """Returns the unix_style name for a given string in lowerCamelCase format. 230 """Returns the unix_style name for a given lowerCamelCase string.
221 """ 231 """
222 return '_'.join([x.lower() 232 return '_'.join([x.lower()
223 for x in re.findall('[A-Z][a-z_]*', name[0].upper() + name[1:])]) 233 for x in re.findall('[A-Z][a-z_]*', name[0].upper() + name[1:])])
224 234
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/h_generator.py ('k') | tools/json_schema_compiler/test/array.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698