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 json | 5 import json |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 | 8 |
9 from handlebar_dict_generator import HandlebarDictGenerator | 9 from handlebar_dict_generator import HandlebarDictGenerator |
10 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater | 10 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater |
11 import third_party.json_schema_compiler.model as model | 11 import third_party.json_schema_compiler.model as model |
12 import third_party.json_schema_compiler.idl_schema as idl_schema | 12 import third_party.json_schema_compiler.idl_schema as idl_schema |
13 import third_party.json_schema_compiler.idl_parser as idl_parser | 13 import third_party.json_schema_compiler.idl_parser as idl_parser |
14 | 14 |
15 class APIDataSource(object): | 15 class APIDataSource(object): |
16 """This class fetches and loads JSON APIs from the FileSystem passed in with | 16 """This class fetches and loads JSON APIs from the FileSystem passed in with |
17 |cache_builder|, so the APIs can be plugged into templates. | 17 |cache_builder|, so the APIs can be plugged into templates. |
18 """ | 18 """ |
19 def __init__(self, cache_builder, base_path): | 19 |
20 class Factory(object): | |
21 def __init__(self, cache_builder, base_path): | |
22 self._cache_builder = cache_builder | |
23 self._base_path = base_path | |
24 | |
25 def Create(self, samples): | |
26 return APIDataSource(self._cache_builder, self._base_path, samples) | |
27 | |
28 def __init__(self, cache_builder, base_path, samples): | |
29 self._samples = samples | |
20 self._json_cache = cache_builder.build(self._LoadJsonAPI) | 30 self._json_cache = cache_builder.build(self._LoadJsonAPI) |
21 self._idl_cache = cache_builder.build(self._LoadIdlAPI) | 31 self._idl_cache = cache_builder.build(self._LoadIdlAPI) |
not at google - send to devlin
2012/07/30 12:23:03
This means that on every construction (via the Fac
chebert
2012/07/31 22:41:33
The issue I am having with this is that the APIDat
not at google - send to devlin
2012/08/01 10:09:19
Ah sorry, looks like I confused myself, and you in
| |
22 self._base_path = base_path | 32 self._base_path = base_path |
23 | 33 |
24 def _LoadJsonAPI(self, api): | 34 def _LoadJsonAPI(self, api): |
25 generator = HandlebarDictGenerator( | 35 generator = HandlebarDictGenerator( |
26 json.loads(json_comment_eater.Nom(api))[0]) | 36 json.loads(json_comment_eater.Nom(api))[0], |
37 self._samples) | |
27 return generator.Generate() | 38 return generator.Generate() |
28 | 39 |
29 def _LoadIdlAPI(self, api): | 40 def _LoadIdlAPI(self, api): |
30 idl = idl_parser.IDLParser().ParseData(api) | 41 idl = idl_parser.IDLParser().ParseData(api) |
31 generator = HandlebarDictGenerator(idl_schema.IDLSchema(idl).process()[0]) | 42 generator = HandlebarDictGenerator(idl_schema.IDLSchema(idl).process()[0], |
43 self._samples) | |
32 return generator.Generate() | 44 return generator.Generate() |
33 | 45 |
34 def __getitem__(self, key): | 46 def __getitem__(self, key): |
35 return self.get(key) | 47 return self.get(key) |
36 | 48 |
37 def get(self, key): | 49 def get(self, key): |
38 path, ext = os.path.splitext(key) | 50 path, ext = os.path.splitext(key) |
39 unix_name = model.UnixName(path) | 51 unix_name = model.UnixName(path) |
40 json_path = unix_name + '.json' | 52 json_path = unix_name + '.json' |
41 idl_path = unix_name + '.idl' | 53 idl_path = unix_name + '.idl' |
42 try: | 54 try: |
43 return self._json_cache.GetFromFile(self._base_path + '/' + json_path) | 55 return self._json_cache.GetFromFile(self._base_path + '/' + json_path) |
44 except Exception: | 56 except Exception: |
45 try: | 57 try: |
46 return self._idl_cache.GetFromFile(self._base_path + '/' + idl_path) | 58 return self._idl_cache.GetFromFile(self._base_path + '/' + idl_path) |
47 except Exception as e: | 59 except Exception as e: |
48 logging.warn(e) | 60 logging.warn(e) |
49 return None | 61 return None |
OLD | NEW |