Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/api_data_source.py |
| diff --git a/chrome/common/extensions/docs/server2/api_data_source.py b/chrome/common/extensions/docs/server2/api_data_source.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..964b047387e22a08efce34d40695667386cb3632 |
| --- /dev/null |
| +++ b/chrome/common/extensions/docs/server2/api_data_source.py |
| @@ -0,0 +1,34 @@ |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import json |
| +import os |
| + |
| +import third_party.json_schema_compiler.json_comment_eater as json_comment_eater |
| +import third_party.json_schema_compiler.model as model |
| + |
| +class APIDataSource(object): |
| + """This class fetches and loads JSON APIs with the fetcher passed in with |
| + |cache_builder|, so the APIs can be plugged into templates. |
| + """ |
| + def _LoadAPI(self, api): |
| + return json.loads(json_comment_eater.Nom(api))[0] |
|
not at google - send to devlin
2012/06/18 19:30:40
nit: either make static / pull outside the class (
cduvall
2012/06/18 19:48:46
Done.
|
| + |
| + def __init__(self, cache_builder, base_paths): |
| + self._cache = cache_builder.build(self._LoadAPI) |
|
not at google - send to devlin
2012/06/18 19:30:40
nice
|
| + self._base_paths = base_paths |
| + |
| + def __getitem__(self, key): |
| + return self.get(key) |
| + |
| + def get(self, key): |
| + path, ext = os.path.splitext(key) |
| + unix_name = model.UnixName(path) |
| + json_path = unix_name + '.json' |
| + for base_path in self._base_paths: |
| + try: |
| + return self._cache.get(base_path + '/' + json_path) |
| + except: |
| + pass |
| + return None |