| OLD | NEW |
| (Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import json |
| 6 import os |
| 7 |
| 8 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater |
| 9 import third_party.json_schema_compiler.model as model |
| 10 |
| 11 class APIDataSource(object): |
| 12 """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 """ |
| 15 def __init__(self, cache_builder, base_paths): |
| 16 self._cache = cache_builder.build(self._LoadAPI) |
| 17 self._base_paths = base_paths |
| 18 |
| 19 def _LoadAPI(self, api): |
| 20 return json.loads(json_comment_eater.Nom(api))[0] |
| 21 |
| 22 def __getitem__(self, key): |
| 23 return self.get(key) |
| 24 |
| 25 def get(self, key): |
| 26 path, ext = os.path.splitext(key) |
| 27 unix_name = model.UnixName(path) |
| 28 json_path = unix_name + '.json' |
| 29 for base_path in self._base_paths: |
| 30 try: |
| 31 return self._cache.get(base_path + '/' + json_path) |
| 32 except: |
| 33 pass |
| 34 return None |
| OLD | NEW |