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 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 ''' | 44 ''' |
45 def __init__(self, | 45 def __init__(self, |
46 root, | 46 root, |
47 path, | 47 path, |
48 include_rules, | 48 include_rules, |
49 cpp_namespace_pattern): | 49 cpp_namespace_pattern): |
50 self._root = root | 50 self._root = root |
51 self._include_rules = [(path, cpp_namespace_pattern)] | 51 self._include_rules = [(path, cpp_namespace_pattern)] |
52 self._include_rules.extend(include_rules) | 52 self._include_rules.extend(include_rules) |
53 | 53 |
| 54 def ResolveNamespace(self, full_namespace): |
| 55 filenames = GenerateFilenames(full_namespace) |
| 56 for path, cpp_namespace in self._include_rules: |
| 57 for filename in reversed(filenames): |
| 58 filepath = os.path.join(path, filename); |
| 59 if os.path.exists(os.path.join(self._root, filepath)): |
| 60 return Model().AddNamespace( |
| 61 self.LoadSchema(filepath)[0], |
| 62 filepath, |
| 63 environment=CppNamespaceEnvironment(cpp_namespace)) |
| 64 return None |
| 65 |
54 def ResolveType(self, full_name, default_namespace): | 66 def ResolveType(self, full_name, default_namespace): |
55 name_parts = full_name.rsplit('.', 1) | 67 name_parts = full_name.rsplit('.', 1) |
56 if len(name_parts) == 1: | 68 if len(name_parts) == 1: |
57 if full_name not in default_namespace.types: | 69 if full_name not in default_namespace.types: |
58 return None | 70 return None |
59 return default_namespace | 71 return default_namespace |
60 full_namespace, type_name = full_name.rsplit('.', 1) | 72 full_namespace, type_name = full_name.rsplit('.', 1) |
61 filenames = GenerateFilenames(full_namespace) | 73 namespace = self.ResolveNamespace(full_namespace) |
62 for path, namespace in self._include_rules: | 74 if namespace and type_name in namespace.types: |
63 for filename in reversed(filenames): | 75 return namespace |
64 filepath = os.path.join(path, filename); | |
65 if os.path.exists(os.path.join(self._root, filepath)): | |
66 namespace = Model().AddNamespace( | |
67 self.LoadSchema(filepath)[0], | |
68 filepath, | |
69 environment=CppNamespaceEnvironment(namespace)) | |
70 if type_name in namespace.types: | |
71 return namespace | |
72 return None | 76 return None |
73 | 77 |
74 def LoadSchema(self, schema): | 78 def LoadSchema(self, schema): |
75 '''Load a schema definition. The schema parameter must be a file name | 79 '''Load a schema definition. The schema parameter must be a file name |
76 with the full path relative to the root.''' | 80 with the full path relative to the root.''' |
77 schema_filename, schema_extension = os.path.splitext(schema) | 81 schema_filename, schema_extension = os.path.splitext(schema) |
78 | 82 |
79 schema_path = os.path.join(self._root, schema) | 83 schema_path = os.path.join(self._root, schema) |
80 if schema_extension == '.json': | 84 if schema_extension == '.json': |
81 api_defs = json_schema.Load(schema_path) | 85 api_defs = json_schema.Load(schema_path) |
82 elif schema_extension == '.idl': | 86 elif schema_extension == '.idl': |
83 api_defs = idl_schema.Load(schema_path) | 87 api_defs = idl_schema.Load(schema_path) |
84 else: | 88 else: |
85 sys.exit('Did not recognize file extension %s for schema %s' % | 89 sys.exit('Did not recognize file extension %s for schema %s' % |
86 (schema_extension, schema)) | 90 (schema_extension, schema)) |
87 | 91 |
88 return api_defs | 92 return api_defs |
OLD | NEW |