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

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

Issue 2596583002: [Schema Compiler] Separate out SchemaLoader and namespace resolving (Closed)
Patch Set: . Created 3 years, 12 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/cpp_type_generator.py ('k') | tools/json_schema_compiler/preview.py » ('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 2016 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
7 import sys
8 6
9 import idl_schema
10 import json_schema
11 from cpp_namespace_environment import CppNamespaceEnvironment 7 from cpp_namespace_environment import CppNamespaceEnvironment
12 from model import Model, UnixName 8 from model import Model, UnixName
9 from schema_loader import SchemaLoader
13 10
14 def GenerateFilenames(full_namespace): 11
12 def _GenerateFilenames(full_namespace):
15 # Try to find the file defining the namespace. Eg. for 13 # Try to find the file defining the namespace. Eg. for
16 # nameSpace.sub_name_space.Type' the following heuristics looks for: 14 # nameSpace.sub_name_space.Type' the following heuristics looks for:
17 # 1. name_space_sub_name_space.json, 15 # 1. name_space_sub_name_space.json,
18 # 2. name_space_sub_name_space.idl, 16 # 2. name_space_sub_name_space.idl,
19 # 3. sub_name_space.json, 17 # 3. sub_name_space.json,
20 # 4. sub_name_space.idl, 18 # 4. sub_name_space.idl,
21 # 5. etc. 19 # 5. etc.
22 sub_namespaces = full_namespace.split('.') 20 sub_namespaces = full_namespace.split('.')
23 filenames = [ ] 21 filenames = [ ]
24 basename = None 22 basename = None
25 for namespace in reversed(sub_namespaces): 23 for namespace in reversed(sub_namespaces):
26 if basename is not None: 24 if basename is not None:
27 basename = UnixName(namespace + '.' + basename) 25 basename = UnixName(namespace + '.' + basename)
28 else: 26 else:
29 basename = UnixName(namespace) 27 basename = UnixName(namespace)
30 for ext in ['json', 'idl']: 28 for ext in ['json', 'idl']:
31 filenames.append('%s.%s' % (basename, ext)) 29 filenames.append('%s.%s' % (basename, ext))
32 return filenames 30 return filenames
33 31
34 class SchemaLoader(object): 32
33 class NamespaceResolver(object):
35 '''Resolves a type name into the namespace the type belongs to. 34 '''Resolves a type name into the namespace the type belongs to.
36
37 Properties:
38 - |root| path to the root directory. 35 - |root| path to the root directory.
39 - |path| path to the directory with the API header files, relative to the 36 - |path| path to the directory with the API header files, relative to the
40 root. 37 root.
41 - |include_rules| List containing tuples with (path, cpp_namespace_pattern) 38 - |include_rules| List containing tuples with (path, cpp_namespace_pattern)
42 used when searching for types. 39 used when searching for types.
43 - |cpp_namespace_pattern| Default namespace pattern 40 - |cpp_namespace_pattern| Default namespace pattern
44 ''' 41 '''
45 def __init__(self, 42 def __init__(self, root, path, include_rules, cpp_namespace_pattern):
46 root,
47 path,
48 include_rules,
49 cpp_namespace_pattern):
50 self._root = root 43 self._root = root
51 self._include_rules = [(path, cpp_namespace_pattern)] 44 self._include_rules = [(path, cpp_namespace_pattern)] + include_rules
52 self._include_rules.extend(include_rules)
53 45
54 def ResolveNamespace(self, full_namespace): 46 def ResolveNamespace(self, full_namespace):
55 filenames = GenerateFilenames(full_namespace) 47 '''Returns the model.Namespace object associated with the |full_namespace|,
48 or None if one can't be found.
49 '''
50 filenames = _GenerateFilenames(full_namespace)
56 for path, cpp_namespace in self._include_rules: 51 for path, cpp_namespace in self._include_rules:
57 cpp_namespace_environment = None 52 cpp_namespace_environment = None
58 if cpp_namespace: 53 if cpp_namespace:
59 cpp_namespace_environment = CppNamespaceEnvironment(cpp_namespace) 54 cpp_namespace_environment = CppNamespaceEnvironment(cpp_namespace)
60 for filename in reversed(filenames): 55 for filename in reversed(filenames):
61 filepath = os.path.join(path, filename); 56 filepath = os.path.join(path, filename);
62 if os.path.exists(os.path.join(self._root, filepath)): 57 if os.path.exists(os.path.join(self._root, filepath)):
58 schema = SchemaLoader(self._root).LoadSchema(filepath)[0]
63 return Model().AddNamespace( 59 return Model().AddNamespace(
64 self.LoadSchema(filepath)[0], 60 schema,
65 filepath, 61 filepath,
66 environment=cpp_namespace_environment) 62 environment=cpp_namespace_environment)
67 return None 63 return None
68 64
69 def ResolveType(self, full_name, default_namespace): 65 def ResolveType(self, full_name, default_namespace):
66 '''Returns the model.Namespace object where the type with the given
67 |full_name| is defined, or None if one can't be found.
68 '''
70 name_parts = full_name.rsplit('.', 1) 69 name_parts = full_name.rsplit('.', 1)
71 if len(name_parts) == 1: 70 if len(name_parts) == 1:
72 if full_name not in default_namespace.types: 71 if full_name not in default_namespace.types:
73 return None 72 return None
74 return default_namespace 73 return default_namespace
75 full_namespace, type_name = full_name.rsplit('.', 1) 74 full_namespace, type_name = full_name.rsplit('.', 1)
76 namespace = self.ResolveNamespace(full_namespace) 75 namespace = self.ResolveNamespace(full_namespace)
77 if namespace and type_name in namespace.types: 76 if namespace and type_name in namespace.types:
78 return namespace 77 return namespace
79 return None 78 return None
80
81 def LoadSchema(self, schema):
82 '''Load a schema definition. The schema parameter must be a file name
83 with the full path relative to the root.'''
84 _, schema_extension = os.path.splitext(schema)
85
86 schema_path = os.path.join(self._root, schema)
87 if schema_extension == '.json':
88 api_defs = json_schema.Load(schema_path)
89 elif schema_extension == '.idl':
90 api_defs = idl_schema.Load(schema_path)
91 else:
92 sys.exit('Did not recognize file extension %s for schema %s' %
93 (schema_extension, schema))
94
95 return api_defs
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/cpp_type_generator.py ('k') | tools/json_schema_compiler/preview.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698