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 os | 5 import os |
6 | 6 |
7 import third_party.json_schema_compiler.model as model | 7 import third_party.json_schema_compiler.model as model |
8 from docs_server_utils import SanitizeAPIName | 8 from docs_server_utils import SanitizeAPIName |
9 | 9 |
10 class APIListDataSource(object): | 10 class APIListDataSource(object): |
11 """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs | 11 """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs |
12 that are used in the api_index.html and experimental.html pages. | 12 that are used in the api_index.html and experimental.html pages. |
13 """ | 13 """ |
14 def __init__(self, cache_builder, file_system, api_path, public_path): | 14 def __init__(self, cache_builder, file_system, api_path, public_path): |
15 self._cache = cache_builder.build(self._ListAPIs) | 15 self._cache = cache_builder.build(self._ListAPIs) |
16 self._file_system = file_system | 16 self._file_system = file_system |
17 self._api_path = api_path + '/' | 17 self._api_path = api_path + '/' |
18 self._public_path = public_path + '/' | 18 self._public_path = public_path + '/' |
19 | 19 |
20 def _ListAPIs(self, apis): | 20 def _GetAPIsInSubdirectory(self, api_names, doc_type): |
21 api_names = set(SanitizeAPIName(name, self._api_path) for name in apis) | 21 public_templates = self._file_system.ReadSingle( |
22 public_templates = self._file_system.ReadSingle(self._public_path) | 22 self._public_path + doc_type + '/') |
23 template_names = [os.path.splitext(name)[0] for name in public_templates] | 23 template_names = [os.path.splitext(name)[0] |
| 24 for name in public_templates] |
24 experimental_apis = [] | 25 experimental_apis = [] |
25 chrome_apis = [] | 26 chrome_apis = [] |
26 for i, template_name in enumerate(sorted(template_names)): | 27 for i, template_name in enumerate(sorted(template_names)): |
27 if model.UnixName(template_name) in api_names: | 28 if model.UnixName(template_name) in api_names: |
28 if template_name.startswith('experimental'): | 29 if template_name.startswith('experimental'): |
29 experimental_apis.append({ 'name': template_name.replace('_', '.') }) | 30 experimental_apis.append({ |
| 31 'name': template_name.replace('_', '.') |
| 32 }) |
30 else: | 33 else: |
31 chrome_apis.append({ 'name': template_name.replace('_', '.') }) | 34 chrome_apis.append({ 'name': template_name.replace('_', '.') }) |
32 chrome_apis[-1]['last'] = True | 35 chrome_apis[-1]['last'] = True |
33 experimental_apis[-1]['last'] = True | 36 experimental_apis[-1]['last'] = True |
34 return { | 37 return { |
35 'chrome': chrome_apis, | 38 'chrome': chrome_apis, |
36 'experimental': experimental_apis | 39 'experimental': experimental_apis |
37 } | 40 } |
38 | 41 |
| 42 def _ListAPIs(self, apis): |
| 43 api_names = set(SanitizeAPIName(name, self._api_path) for name in apis) |
| 44 return { |
| 45 'apps': self._GetAPIsInSubdirectory(api_names, 'apps'), |
| 46 'extensions': self._GetAPIsInSubdirectory(api_names, 'extensions') |
| 47 } |
| 48 |
39 def __getitem__(self, key): | 49 def __getitem__(self, key): |
40 return self.get(key) | 50 return self.get(key) |
41 | 51 |
42 def get(self, key): | 52 def get(self, key): |
43 try: | 53 try: |
44 return self._cache.GetFromFileListing(self._api_path)[key] | 54 return self._cache.GetFromFileListing(self._api_path)[key] |
45 except Exception as e: | 55 except Exception as e: |
46 return None | 56 return None |
OLD | NEW |