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 sys | 6 import sys |
7 | 7 |
8 import idl_schema | 8 import idl_schema |
9 import json_schema | 9 import json_schema |
10 from model import Model | 10 from model import Model |
11 | 11 |
12 class SchemaLoader(object): | 12 class SchemaLoader(object): |
13 '''Resolves a type name into the namespace the type belongs to. | 13 '''Resolves a type name into the namespace the type belongs to. |
| 14 |
| 15 Properties: |
| 16 - |display_path| path to the directory with the API header files, intended for |
| 17 use with the Model. |
| 18 - |real_path| path to the directory with the API header files, used for file |
| 19 access. |
14 ''' | 20 ''' |
15 def __init__(self, api_path): | 21 def __init__(self, display_path, real_path): |
16 self._api_path = api_path | 22 self._display_path = display_path |
| 23 self._real_path = real_path |
17 | 24 |
18 def ResolveType(self, full_name, default_namespace): | 25 def ResolveType(self, full_name, default_namespace): |
19 name_parts = full_name.rsplit('.', 1) | 26 name_parts = full_name.rsplit('.', 1) |
20 if len(name_parts) == 1: | 27 if len(name_parts) == 1: |
21 if full_name not in default_namespace.types: | 28 if full_name not in default_namespace.types: |
22 return None | 29 return None |
23 return default_namespace | 30 return default_namespace |
24 namespace_name, type_name = name_parts | 31 namespace_name, type_name = name_parts |
25 real_name = None | 32 real_name = None |
26 for ext in ['json', 'idl']: | 33 for ext in ['json', 'idl']: |
27 filename = '%s.%s' % (namespace_name, ext) | 34 filename = '%s.%s' % (namespace_name, ext) |
28 if os.path.exists(filename): | 35 filepath = os.path.join(self._real_path, filename); |
| 36 if os.path.exists(filepath): |
29 real_name = filename | 37 real_name = filename |
30 break | 38 break |
31 if real_name is None: | 39 if real_name is None: |
32 return None | 40 return None |
33 namespace = Model().AddNamespace(self.LoadSchema(real_name)[0], | 41 namespace = Model().AddNamespace( |
34 os.path.join(self._api_path, real_name)) | 42 self.LoadSchema(real_name)[0], |
| 43 os.path.join(self._display_path, real_name)) |
35 if type_name not in namespace.types: | 44 if type_name not in namespace.types: |
36 return None | 45 return None |
37 return namespace | 46 return namespace |
38 | 47 |
39 def LoadSchema(self, schema): | 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.''' |
40 schema_filename, schema_extension = os.path.splitext(schema) | 52 schema_filename, schema_extension = os.path.splitext(schema) |
41 | 53 |
| 54 schema_path = os.path.join(self._real_path, schema); |
42 if schema_extension == '.json': | 55 if schema_extension == '.json': |
43 api_defs = json_schema.Load(schema) | 56 api_defs = json_schema.Load(schema_path) |
44 elif schema_extension == '.idl': | 57 elif schema_extension == '.idl': |
45 api_defs = idl_schema.Load(schema) | 58 api_defs = idl_schema.Load(schema_path) |
46 else: | 59 else: |
47 sys.exit('Did not recognize file extension %s for schema %s' % | 60 sys.exit('Did not recognize file extension %s for schema %s' % |
48 (schema_extension, schema)) | 61 (schema_extension, schema)) |
49 if len(api_defs) != 1: | 62 if len(api_defs) != 1: |
50 sys.exit('File %s has multiple schemas. Files are only allowed to contain' | 63 sys.exit('File %s has multiple schemas. Files are only allowed to contain' |
51 'a single schema.' % schema) | 64 'a single schema.' % schema) |
52 | 65 |
53 return api_defs | 66 return api_defs |
OLD | NEW |