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 sys | 7 import sys |
7 | 8 |
8 import idl_schema | 9 import idl_schema |
9 import json_schema | 10 import json_schema |
10 from model import Model | 11 from model import Model |
11 | 12 |
12 class SchemaLoader(object): | 13 class SchemaLoader(object): |
13 '''Resolves a type name into the namespace the type belongs to. | 14 '''Resolves a type name into the namespace the type belongs to. |
14 | 15 |
15 Properties: | 16 Properties: |
16 - |display_path| path to the directory with the API header files, intended for | 17 - |display_path| path to the directory with the API header files, intended for |
17 use with the Model. | 18 use with the Model. |
18 - |real_path| path to the directory with the API header files, used for file | 19 - |real_path| path to the directory with the API header files, used for file |
19 access. | 20 access. |
20 ''' | 21 ''' |
21 def __init__(self, display_path, real_path): | 22 def __init__(self, display_path, real_path): |
22 self._display_path = display_path | 23 self._display_path = display_path |
23 self._real_path = real_path | 24 self._real_path = real_path |
24 | 25 |
25 def ResolveType(self, full_name, default_namespace): | 26 def ResolveType(self, full_name, default_namespace): |
26 name_parts = full_name.rsplit('.', 1) | 27 name_parts = full_name.rsplit('.', 1) |
27 if len(name_parts) == 1: | 28 if len(name_parts) == 1: |
28 if full_name not in default_namespace.types: | 29 if full_name not in default_namespace.types: |
29 return None | 30 return None |
30 return default_namespace | 31 return default_namespace |
31 namespace_name, type_name = name_parts | 32 namespace_name, type_name = name_parts |
32 real_name = None | 33 real_name = None |
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. | |
33 for ext in ['json', 'idl']: | 38 for ext in ['json', 'idl']: |
34 filename = '%s.%s' % (namespace_name, ext) | 39 basename = re.sub('(?!^)([A-Z]+)', r'_\1', |
40 namespace_name.replace('.', '_')).lower() | |
not at google - send to devlin
2014/03/28 18:42:25
there is already a helper for this sort of thing i
mtomasz
2014/03/31 01:39:20
Done.
| |
41 filename = '%s.%s' % (basename, ext) | |
35 filepath = os.path.join(self._real_path, filename); | 42 filepath = os.path.join(self._real_path, filename); |
36 if os.path.exists(filepath): | 43 if os.path.exists(filepath): |
37 real_name = filename | 44 real_name = filename |
38 break | 45 break |
39 if real_name is None: | 46 if real_name is None: |
40 return None | 47 return None |
41 namespace = Model().AddNamespace( | 48 namespace = Model().AddNamespace( |
42 self.LoadSchema(real_name)[0], | 49 self.LoadSchema(real_name)[0], |
43 os.path.join(self._display_path, real_name)) | 50 os.path.join(self._display_path, real_name)) |
44 if type_name not in namespace.types: | 51 if type_name not in namespace.types: |
45 return None | 52 return None |
46 return namespace | 53 return namespace |
47 | 54 |
48 def LoadSchema(self, schema): | 55 def LoadSchema(self, schema): |
49 '''Load a schema definition. The schema parameter must be a file name | 56 '''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 | 57 without any path component - the file is loaded from the path defined by |
51 the real_path argument passed to the constructor.''' | 58 the real_path argument passed to the constructor.''' |
52 schema_filename, schema_extension = os.path.splitext(schema) | 59 schema_filename, schema_extension = os.path.splitext(schema) |
53 | 60 |
54 schema_path = os.path.join(self._real_path, schema); | 61 schema_path = os.path.join(self._real_path, schema); |
55 if schema_extension == '.json': | 62 if schema_extension == '.json': |
56 api_defs = json_schema.Load(schema_path) | 63 api_defs = json_schema.Load(schema_path) |
57 elif schema_extension == '.idl': | 64 elif schema_extension == '.idl': |
58 api_defs = idl_schema.Load(schema_path) | 65 api_defs = idl_schema.Load(schema_path) |
59 else: | 66 else: |
60 sys.exit('Did not recognize file extension %s for schema %s' % | 67 sys.exit('Did not recognize file extension %s for schema %s' % |
61 (schema_extension, schema)) | 68 (schema_extension, schema)) |
62 | 69 |
63 return api_defs | 70 return api_defs |
OLD | NEW |