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
3 # found in the LICENSE file.
10 from model import Model
12 class SchemaLoader(object):
13 '''Resolves a type name into the namespace the type belongs to.
16 - |display_path| path to the directory with the API header files, intended for
18 - |real_path| path to the directory with the API header files, used for file
21 def __init__(self, display_path, real_path):
22 self._display_path = display_path
23 self._real_path = real_path
25 def ResolveType(self, full_name, default_namespace):
26 name_parts = full_name.rsplit('.', 1)
27 if len(name_parts) == 1:
28 if full_name not in default_namespace.types:
30 return default_namespace
31 namespace_name, type_name = name_parts
33 for ext in ['json', 'idl']:
34 filename = '%s.%s' % (namespace_name, ext)
35 filepath = os.path.join(self._real_path, filename);
36 if os.path.exists(filepath):
41 namespace = Model().AddNamespace(
42 self.LoadSchema(real_name)[0],
43 os.path.join(self._display_path, real_name))
44 if type_name not in namespace.types:
48 def LoadSchema(self, schema):
49 '''Load a schema definition. The schema parameter must be a file name
50 without any path component - the file is loaded from the path defined by
51 the real_path argument passed to the constructor.'''
52 schema_filename, schema_extension = os.path.splitext(schema)
54 schema_path = os.path.join(self._real_path, schema);
55 if schema_extension == '.json':
56 api_defs = json_schema.Load(schema_path)
57 elif schema_extension == '.idl':
58 api_defs = idl_schema.Load(schema_path)
60 sys.exit('Did not recognize file extension %s for schema %s' %
61 (schema_extension, schema))
62 if len(api_defs) != 1:
63 sys.exit('File %s has multiple schemas. Files are only allowed to contain'
64 'a single schema.' % schema)