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.
11 from model
import Model
, UnixName
13 class SchemaLoader(object):
14 '''Resolves a type name into the namespace the type belongs to.
17 - |display_path| path to the directory with the API header files, intended for
19 - |real_path| path to the directory with the API header files, used for file
22 def __init__(self
, display_path
, real_path
):
23 self
._display
_path
= display_path
24 self
._real
_path
= real_path
26 def ResolveType(self
, full_name
, default_namespace
):
27 name_parts
= full_name
.rsplit('.', 1)
28 if len(name_parts
) == 1:
29 if full_name
not in default_namespace
.types
:
31 return default_namespace
32 namespace_name
, type_name
= name_parts
34 # Try to find the file defining the namespace. Eg. for
35 # nameSpace.sub_name_space.Type' the following heuristics looks for:
36 # 1. name_space_sub_name_space.json,
37 # 2. name_space_sub_name_space.idl.
38 for ext
in ['json', 'idl']:
39 basename
= UnixName(namespace_name
)
40 filename
= '%s.%s' % (basename
, ext
)
41 filepath
= os
.path
.join(self
._real
_path
, filename
);
42 if os
.path
.exists(filepath
):
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
:
54 def LoadSchema(self
, schema
):
55 '''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
57 the real_path argument passed to the constructor.'''
58 schema_filename
, schema_extension
= os
.path
.splitext(schema
)
60 schema_path
= os
.path
.join(self
._real
_path
, schema
);
61 if schema_extension
== '.json':
62 api_defs
= json_schema
.Load(schema_path
)
63 elif schema_extension
== '.idl':
64 api_defs
= idl_schema
.Load(schema_path
)
66 sys
.exit('Did not recognize file extension %s for schema %s' %
67 (schema_extension
, schema
))