Index: tools/json_schema_compiler/namespace_resolver.py |
diff --git a/tools/json_schema_compiler/schema_loader.py b/tools/json_schema_compiler/namespace_resolver.py |
similarity index 66% |
copy from tools/json_schema_compiler/schema_loader.py |
copy to tools/json_schema_compiler/namespace_resolver.py |
index aad661e357c50d4ce2b3825c0d02f938562ab9b7..6387c807a0506877d4713238654561e63fa07a0c 100644 |
--- a/tools/json_schema_compiler/schema_loader.py |
+++ b/tools/json_schema_compiler/namespace_resolver.py |
@@ -1,17 +1,15 @@ |
-# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+# Copyright 2016 The Chromium Authors. All rights reserved. |
# Use of this source code is governed by a BSD-style license that can be |
# found in the LICENSE file. |
import os |
-import re |
-import sys |
-import idl_schema |
-import json_schema |
from cpp_namespace_environment import CppNamespaceEnvironment |
from model import Model, UnixName |
+from schema_loader import SchemaLoader |
-def GenerateFilenames(full_namespace): |
+ |
+def _GenerateFilenames(full_namespace): |
# Try to find the file defining the namespace. Eg. for |
# nameSpace.sub_name_space.Type' the following heuristics looks for: |
# 1. name_space_sub_name_space.json, |
@@ -31,10 +29,9 @@ def GenerateFilenames(full_namespace): |
filenames.append('%s.%s' % (basename, ext)) |
return filenames |
-class SchemaLoader(object): |
- '''Resolves a type name into the namespace the type belongs to. |
- Properties: |
+class NamespaceResolver(object): |
+ '''Resolves a type name into the namespace the type belongs to. |
- |root| path to the root directory. |
- |path| path to the directory with the API header files, relative to the |
root. |
@@ -42,17 +39,15 @@ class SchemaLoader(object): |
used when searching for types. |
- |cpp_namespace_pattern| Default namespace pattern |
''' |
- def __init__(self, |
- root, |
- path, |
- include_rules, |
- cpp_namespace_pattern): |
+ def __init__(self, root, path, include_rules, cpp_namespace_pattern): |
self._root = root |
- self._include_rules = [(path, cpp_namespace_pattern)] |
- self._include_rules.extend(include_rules) |
+ self._include_rules = [(path, cpp_namespace_pattern)] + include_rules |
def ResolveNamespace(self, full_namespace): |
- filenames = GenerateFilenames(full_namespace) |
+ '''Returns the model.Namespace object associated with the |full_namespace|, |
+ or None if one can't be found. |
+ ''' |
+ filenames = _GenerateFilenames(full_namespace) |
for path, cpp_namespace in self._include_rules: |
cpp_namespace_environment = None |
if cpp_namespace: |
@@ -60,13 +55,17 @@ class SchemaLoader(object): |
for filename in reversed(filenames): |
filepath = os.path.join(path, filename); |
if os.path.exists(os.path.join(self._root, filepath)): |
+ schema = SchemaLoader(self._root).LoadSchema(filepath)[0] |
return Model().AddNamespace( |
- self.LoadSchema(filepath)[0], |
+ schema, |
filepath, |
environment=cpp_namespace_environment) |
return None |
def ResolveType(self, full_name, default_namespace): |
+ '''Returns the model.Namespace object where the type with the given |
+ |full_name| is defined, or None if one can't be found. |
+ ''' |
name_parts = full_name.rsplit('.', 1) |
if len(name_parts) == 1: |
if full_name not in default_namespace.types: |
@@ -77,19 +76,3 @@ class SchemaLoader(object): |
if namespace and type_name in namespace.types: |
return namespace |
return None |
- |
- def LoadSchema(self, schema): |
- '''Load a schema definition. The schema parameter must be a file name |
- with the full path relative to the root.''' |
- _, schema_extension = os.path.splitext(schema) |
- |
- schema_path = os.path.join(self._root, schema) |
- if schema_extension == '.json': |
- api_defs = json_schema.Load(schema_path) |
- elif schema_extension == '.idl': |
- api_defs = idl_schema.Load(schema_path) |
- else: |
- sys.exit('Did not recognize file extension %s for schema %s' % |
- (schema_extension, schema)) |
- |
- return api_defs |