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

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

Issue 487533005: Add support for references in different paths in apis (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Renaming core_api::types->core_api::extension_types. Created 6 years, 3 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
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 5 import os
6 import re 6 import re
7 import sys 7 import sys
8 8
9 import idl_schema 9 import idl_schema
10 import json_schema 10 import json_schema
11 from model import Model, UnixName 11 from model import Model, UnixName
12 12
13 def GenerateFilenames(full_namespace):
14 # Try to find the file defining the namespace. Eg. for
15 # nameSpace.sub_name_space.Type' the following heuristics looks for:
16 # 1. name_space_sub_name_space.json,
17 # 2. name_space_sub_name_space.idl,
18 # 3. sub_name_space.json,
19 # 4. sub_name_space.idl,
20 # 5. etc.
21 sub_namespaces = full_namespace.split('.')
22 filenames = [ ]
23 basename = None
24 for namespace in reversed(sub_namespaces):
25 if basename is not None:
26 basename = UnixName(namespace + '.' + basename)
27 else:
28 basename = UnixName(namespace)
29 for ext in ['json', 'idl']:
30 filenames.append('%s.%s' % (basename, ext))
31 return filenames
32
13 class SchemaLoader(object): 33 class SchemaLoader(object):
14 '''Resolves a type name into the namespace the type belongs to. 34 '''Resolves a type name into the namespace the type belongs to.
15 35
16 Properties: 36 Properties:
17 - |display_path| path to the directory with the API header files, intended for 37 - |display_path| path to the directory with the API header files, intended for
18 use with the Model. 38 use with the Model.
19 - |real_path| path to the directory with the API header files, used for file 39 - |real_path| path to the directory with the API header files, used for file
20 access. 40 access.
21 ''' 41 '''
22 def __init__(self, display_path, real_path): 42 def __init__(self, display_path, real_path, includes, cpp_namespace_pattern):
23 self._display_path = display_path 43 self._display_path = display_path
24 self._real_path = real_path 44 self._real_path = real_path
45 self._includes = [
46 { 'path': real_path, 'namespace': cpp_namespace_pattern } ]
47 self._includes.extend(includes)
25 48
26 def ResolveType(self, full_name, default_namespace): 49 def ResolveType(self, full_name, default_namespace):
27 name_parts = full_name.rsplit('.', 1) 50 name_parts = full_name.rsplit('.', 1)
28 if len(name_parts) == 1: 51 if len(name_parts) == 1:
29 if full_name not in default_namespace.types: 52 if full_name not in default_namespace.types:
30 return None 53 return None
31 return default_namespace 54 return default_namespace
32 namespace_name, type_name = name_parts 55 full_namespace, type_name = full_name.rsplit('.', 1)
33 real_name = None 56 filenames = GenerateFilenames(full_namespace)
34 # Try to find the file defining the namespace. Eg. for 57 for include in self._includes:
35 # nameSpace.sub_name_space.Type' the following heuristics looks for: 58 path = include['path']
36 # 1. name_space_sub_name_space.json, 59 for filename in reversed(filenames):
37 # 2. name_space_sub_name_space.idl. 60 filepath = os.path.join(path, filename);
38 for ext in ['json', 'idl']: 61 if os.path.exists(filepath):
39 basename = UnixName(namespace_name) 62 namespace = Model().AddNamespace(
40 filename = '%s.%s' % (basename, ext) 63 self.LoadSchema(filepath)[0],
41 filepath = os.path.join(self._real_path, filename); 64 os.path.join(self._display_path, filepath),
42 if os.path.exists(filepath): 65 include['namespace'])
43 real_name = filename 66 if type_name in namespace.types:
44 break 67 return namespace
45 if real_name is None: 68 return None
46 return None
47 namespace = Model().AddNamespace(
48 self.LoadSchema(real_name)[0],
49 os.path.join(self._display_path, real_name))
50 if type_name not in namespace.types:
51 return None
52 return namespace
53 69
54 def LoadSchema(self, schema): 70 def LoadSchema(self, schema):
55 '''Load a schema definition. The schema parameter must be a file name 71 '''Load a schema definition. The schema parameter must be a file name
56 without any path component - the file is loaded from the path defined by 72 without any path component - the file is loaded from the path defined by
57 the real_path argument passed to the constructor.''' 73 the real_path argument passed to the constructor.'''
58 schema_filename, schema_extension = os.path.splitext(schema) 74 schema_filename, schema_extension = os.path.splitext(schema)
59 75
60 schema_path = os.path.join(self._real_path, schema); 76 schema_path = os.path.join(self._real_path, schema);
61 if schema_extension == '.json': 77 if schema_extension == '.json':
62 api_defs = json_schema.Load(schema_path) 78 api_defs = json_schema.Load(schema_path)
63 elif schema_extension == '.idl': 79 elif schema_extension == '.idl':
64 api_defs = idl_schema.Load(schema_path) 80 api_defs = idl_schema.Load(schema_path)
65 else: 81 else:
66 sys.exit('Did not recognize file extension %s for schema %s' % 82 sys.exit('Did not recognize file extension %s for schema %s' %
67 (schema_extension, schema)) 83 (schema_extension, schema))
68 84
69 return api_defs 85 return api_defs
OLDNEW
« tools/json_schema_compiler/model.py ('K') | « tools/json_schema_compiler/model_test.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698