| 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 os | 6 import os |
| 7 | 7 |
| 8 from handlebar_dict_generator import HandlebarDictGenerator |
| 8 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater | 9 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater |
| 9 import third_party.json_schema_compiler.model as model | 10 import third_party.json_schema_compiler.model as model |
| 10 | 11 |
| 11 class APIDataSource(object): | 12 class APIDataSource(object): |
| 12 """This class fetches and loads JSON APIs with the fetcher passed in with | 13 """This class fetches and loads JSON APIs with the fetcher passed in with |
| 13 |cache_builder|, so the APIs can be plugged into templates. | 14 |cache_builder|, so the APIs can be plugged into templates. |
| 14 """ | 15 """ |
| 15 def __init__(self, cache_builder, base_paths): | 16 def __init__(self, cache_builder, base_paths): |
| 16 self._cache = cache_builder.build(self._LoadAPI) | 17 self._cache = cache_builder.build(self._LoadAPI) |
| 17 self._base_paths = base_paths | 18 self._base_paths = base_paths |
| 18 | 19 |
| 19 def _LoadAPI(self, api): | 20 def _LoadAPI(self, api): |
| 20 return json.loads(json_comment_eater.Nom(api))[0] | 21 generator = HandlebarDictGenerator( |
| 22 json.loads(json_comment_eater.Nom(api))[0]) |
| 23 return generator.Generate() |
| 21 | 24 |
| 22 def __getitem__(self, key): | 25 def __getitem__(self, key): |
| 23 return self.get(key) | 26 return self.get(key) |
| 24 | 27 |
| 25 def get(self, key): | 28 def get(self, key): |
| 26 path, ext = os.path.splitext(key) | 29 path, ext = os.path.splitext(key) |
| 27 unix_name = model.UnixName(path) | 30 unix_name = model.UnixName(path) |
| 28 json_path = unix_name + '.json' | 31 json_path = unix_name + '.json' |
| 29 for base_path in self._base_paths: | 32 for base_path in self._base_paths: |
| 30 try: | 33 try: |
| 31 return self._cache.get(base_path + '/' + json_path) | 34 return self._cache.get(base_path + '/' + json_path) |
| 32 except: | 35 except: |
| 33 pass | 36 pass |
| 34 return None | 37 return None |
| OLD | NEW |