OLD | NEW |
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 cpp_namespace_environment import CppNamespaceEnvironment |
11 from model import Model, UnixName | 12 from model import Model, UnixName |
12 | 13 |
| 14 def GenerateFilenames(full_namespace): |
| 15 # Try to find the file defining the namespace. Eg. for |
| 16 # nameSpace.sub_name_space.Type' the following heuristics looks for: |
| 17 # 1. name_space_sub_name_space.json, |
| 18 # 2. name_space_sub_name_space.idl, |
| 19 # 3. sub_name_space.json, |
| 20 # 4. sub_name_space.idl, |
| 21 # 5. etc. |
| 22 sub_namespaces = full_namespace.split('.') |
| 23 filenames = [ ] |
| 24 basename = None |
| 25 for namespace in reversed(sub_namespaces): |
| 26 if basename is not None: |
| 27 basename = UnixName(namespace + '.' + basename) |
| 28 else: |
| 29 basename = UnixName(namespace) |
| 30 for ext in ['json', 'idl']: |
| 31 filenames.append('%s.%s' % (basename, ext)) |
| 32 return filenames |
| 33 |
13 class SchemaLoader(object): | 34 class SchemaLoader(object): |
14 '''Resolves a type name into the namespace the type belongs to. | 35 '''Resolves a type name into the namespace the type belongs to. |
15 | 36 |
16 Properties: | 37 Properties: |
17 - |display_path| path to the directory with the API header files, intended for | 38 - |root| path to the root directory. |
18 use with the Model. | 39 - |path| path to the directory with the API header files, relative to the |
19 - |real_path| path to the directory with the API header files, used for file | 40 root. |
20 access. | 41 - |include_rules| List containing tuples with (path, cpp_namespace_pattern) |
| 42 used when searching for types. |
| 43 - |cpp_namespace_pattern| Default namespace pattern |
21 ''' | 44 ''' |
22 def __init__(self, display_path, real_path): | 45 def __init__(self, |
23 self._display_path = display_path | 46 root, |
24 self._real_path = real_path | 47 path, |
| 48 include_rules, |
| 49 cpp_namespace_pattern): |
| 50 self._root = root |
| 51 self._include_rules = [(path, cpp_namespace_pattern)] |
| 52 self._include_rules.extend(include_rules) |
25 | 53 |
26 def ResolveType(self, full_name, default_namespace): | 54 def ResolveType(self, full_name, default_namespace): |
27 name_parts = full_name.rsplit('.', 1) | 55 name_parts = full_name.rsplit('.', 1) |
28 if len(name_parts) == 1: | 56 if len(name_parts) == 1: |
29 if full_name not in default_namespace.types: | 57 if full_name not in default_namespace.types: |
30 return None | 58 return None |
31 return default_namespace | 59 return default_namespace |
32 namespace_name, type_name = name_parts | 60 full_namespace, type_name = full_name.rsplit('.', 1) |
33 real_name = None | 61 filenames = GenerateFilenames(full_namespace) |
34 # Try to find the file defining the namespace. Eg. for | 62 for path, namespace in self._include_rules: |
35 # nameSpace.sub_name_space.Type' the following heuristics looks for: | 63 for filename in reversed(filenames): |
36 # 1. name_space_sub_name_space.json, | 64 filepath = os.path.join(path, filename); |
37 # 2. name_space_sub_name_space.idl. | 65 if os.path.exists(os.path.join(self._root, filepath)): |
38 for ext in ['json', 'idl']: | 66 namespace = Model().AddNamespace( |
39 basename = UnixName(namespace_name) | 67 self.LoadSchema(filepath)[0], |
40 filename = '%s.%s' % (basename, ext) | 68 filepath, |
41 filepath = os.path.join(self._real_path, filename); | 69 environment=CppNamespaceEnvironment(namespace)) |
42 if os.path.exists(filepath): | 70 if type_name in namespace.types: |
43 real_name = filename | 71 return namespace |
44 break | 72 return None |
45 if real_name is 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 | 73 |
54 def LoadSchema(self, schema): | 74 def LoadSchema(self, schema): |
55 '''Load a schema definition. The schema parameter must be a file name | 75 '''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 | 76 with the full path relative to the root.''' |
57 the real_path argument passed to the constructor.''' | |
58 schema_filename, schema_extension = os.path.splitext(schema) | 77 schema_filename, schema_extension = os.path.splitext(schema) |
59 | 78 |
60 schema_path = os.path.join(self._real_path, schema); | 79 schema_path = os.path.join(self._root, schema) |
61 if schema_extension == '.json': | 80 if schema_extension == '.json': |
62 api_defs = json_schema.Load(schema_path) | 81 api_defs = json_schema.Load(schema_path) |
63 elif schema_extension == '.idl': | 82 elif schema_extension == '.idl': |
64 api_defs = idl_schema.Load(schema_path) | 83 api_defs = idl_schema.Load(schema_path) |
65 else: | 84 else: |
66 sys.exit('Did not recognize file extension %s for schema %s' % | 85 sys.exit('Did not recognize file extension %s for schema %s' % |
67 (schema_extension, schema)) | 86 (schema_extension, schema)) |
68 | 87 |
69 return api_defs | 88 return api_defs |
OLD | NEW |