OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2013 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. | |
4 | |
5 import logging | |
6 import os | |
7 import posixpath | |
8 | |
9 from file_system import FileNotFoundError | |
10 from future import Gettable, Future | |
11 from schema_util import ProcessSchema | |
12 from svn_constants import API_PATH | |
13 from third_party.json_schema_compiler.model import Namespace, UnixName | |
14 | |
15 | |
16 def _CreateAPIModel(path, data): | |
17 schema = ProcessSchema(path, data) | |
18 if os.path.splitext(path)[1] == '.json': | |
19 schema = schema[0] | |
20 return Namespace(schema, schema['namespace']) | |
21 | |
22 | |
23 class APIModels(object): | |
24 '''Tracks APIs and their Models. | |
25 ''' | |
26 | |
27 def __init__(self, features_bundle, compiled_fs_factory, file_system): | |
28 self._features_bundle = features_bundle | |
29 self._model_cache = compiled_fs_factory.Create( | |
30 file_system, _CreateAPIModel, APIModels) | |
31 | |
32 def GetNames(self): | |
33 return self._features_bundle.GetAPIFeatures().keys() | |
34 | |
35 def GetModel(self, api_name): | |
36 # Callers sometimes specify a full path (.json or .idl) themselves. If so, | |
Yoyo Zhou
2013/10/30 23:22:19
This isn't always a full path, sometimes just the
not at google - send to devlin
2013/10/30 23:50:05
Done.
| |
37 # believe them. | |
38 if os.path.splitext(api_name)[1] in ('.json', '.idl'): | |
39 if not api_name.startswith(API_PATH + '/'): | |
40 api_name = posixpath.join(API_PATH, api_name) | |
41 return self._model_cache.GetFromFile(api_name) | |
42 | |
43 assert not api_name.startswith(API_PATH) | |
44 | |
45 # API names are given as declarativeContent and app.window but file names | |
46 # will be declarative_content and app_window. | |
47 file_name = UnixName(api_name).replace('.', '_') | |
48 # Devtools APIs are in API_PATH/devtools/ not API_PATH/, and have their | |
49 # "devtools" names removed from the file names. | |
50 if 'devtools_' in file_name: | |
Yoyo Zhou
2013/10/30 23:22:19
perhaps os.path.basename(file_name).startswith('de
not at google - send to devlin
2013/10/30 23:50:05
Done.
| |
51 file_name = posixpath.join('devtools', file_name.replace('devtools_', '')) | |
52 | |
53 futures = [self._model_cache.GetFromFile('%s/%s.%s' % | |
54 (API_PATH, file_name, ext)) | |
55 for ext in ('json', 'idl')] | |
56 def resolve(): | |
57 for future in futures: | |
58 try: | |
59 return future.Get() | |
60 except FileNotFoundError: pass | |
61 # Propagate the first FileNotFoundError. | |
Yoyo Zhou
2013/10/30 23:22:19
...if both were filenotfound
not at google - send to devlin
2013/10/30 23:50:05
Done.
| |
62 futures[0].Get() | |
63 return Future(delegate=Gettable(resolve)) | |
OLD | NEW |