| 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 dict_generator import DictGenerator |
| 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 = DictGenerator(json.loads(json_comment_eater.Nom(api))[0]) |
| 22 return generator.Generate() |
| 21 | 23 |
| 22 def __getitem__(self, key): | 24 def __getitem__(self, key): |
| 23 return self.get(key) | 25 return self.get(key) |
| 24 | 26 |
| 25 def get(self, key): | 27 def get(self, key): |
| 26 path, ext = os.path.splitext(key) | 28 path, ext = os.path.splitext(key) |
| 27 unix_name = model.UnixName(path) | 29 unix_name = model.UnixName(path) |
| 28 json_path = unix_name + '.json' | 30 json_path = unix_name + '.json' |
| 29 for base_path in self._base_paths: | 31 for base_path in self._base_paths: |
| 30 try: | 32 try: |
| 31 return self._cache.get(base_path + '/' + json_path) | 33 return self._cache.get(base_path + '/' + json_path) |
| 32 except: | 34 except: |
| 33 pass | 35 pass |
| 34 return None | 36 return None |
| OLD | NEW |