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