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 logging | 5 import logging |
6 import os | 6 import os |
7 | 7 |
8 from file_system import FileNotFoundError | 8 from file_system import FileNotFoundError |
| 9 import file_system_cache as fs_cache |
9 import third_party.json_schema_compiler.model as model | 10 import third_party.json_schema_compiler.model as model |
10 from docs_server_utils import SanitizeAPIName | 11 from docs_server_utils import SanitizeAPIName |
11 | 12 |
12 # These files are special cases that shouldn't be in the API list. | 13 # These files are special cases that shouldn't be in the API list. |
13 IGNORED_FILES = [ | 14 IGNORED_FILES = [ |
14 'devtools' | 15 'devtools' |
15 ] | 16 ] |
16 | 17 |
17 class APIListDataSource(object): | 18 class APIListDataSource(object): |
| 19 class Factory(object): |
| 20 def __init__(self, cache_builder, file_system, api_path, public_path): |
| 21 self._cache = cache_builder.build(self._ListAPIs, fs_cache.LIST) |
| 22 self._file_system = file_system |
| 23 def Normalize(string): |
| 24 return string if string.endswith('/') else (string + '/') |
| 25 self._api_path = Normalize(api_path) |
| 26 self._public_path = Normalize(public_path) |
| 27 |
| 28 def _GetAPIsInSubdirectory(self, api_names, doc_type): |
| 29 public_templates = self._file_system.ReadSingle( |
| 30 self._public_path + doc_type + '/') |
| 31 template_names = [os.path.splitext(name)[0] |
| 32 for name in public_templates] |
| 33 experimental_apis = [] |
| 34 chrome_apis = [] |
| 35 for template_name in sorted(template_names): |
| 36 if template_name in IGNORED_FILES: |
| 37 continue |
| 38 if model.UnixName(template_name) in api_names: |
| 39 if template_name.startswith('experimental'): |
| 40 experimental_apis.append({ |
| 41 'name': template_name.replace('_', '.') |
| 42 }) |
| 43 else: |
| 44 chrome_apis.append({ 'name': template_name.replace('_', '.') }) |
| 45 chrome_apis[-1]['last'] = True |
| 46 experimental_apis[-1]['last'] = True |
| 47 return { |
| 48 'chrome': chrome_apis, |
| 49 'experimental': experimental_apis |
| 50 } |
| 51 |
| 52 def _ListAPIs(self, apis): |
| 53 api_names = set(SanitizeAPIName(name, self._api_path) for name in apis) |
| 54 return { |
| 55 'apps': self._GetAPIsInSubdirectory(api_names, 'apps'), |
| 56 'extensions': self._GetAPIsInSubdirectory(api_names, 'extensions') |
| 57 } |
| 58 |
| 59 def Create(self): |
| 60 return APIListDataSource(self._cache, self._api_path) |
| 61 |
18 """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs | 62 """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs |
19 that are used in the api_index.html and experimental.html pages. | 63 that are used in the api_index.html and experimental.html pages. |
20 """ | 64 """ |
21 def __init__(self, cache_builder, file_system, api_path, public_path): | 65 def __init__(self, cache, api_path): |
22 self._cache = cache_builder.build(self._ListAPIs) | 66 self._cache = cache |
23 self._file_system = file_system | 67 self._api_path = api_path |
24 self._api_path = api_path + '/' | |
25 self._public_path = public_path + '/' | |
26 | |
27 def _GetAPIsInSubdirectory(self, api_names, doc_type): | |
28 public_templates = self._file_system.ReadSingle( | |
29 self._public_path + doc_type + '/') | |
30 template_names = [os.path.splitext(name)[0] | |
31 for name in public_templates] | |
32 experimental_apis = [] | |
33 chrome_apis = [] | |
34 for template_name in sorted(template_names): | |
35 if template_name in IGNORED_FILES: | |
36 continue | |
37 if model.UnixName(template_name) in api_names: | |
38 if template_name.startswith('experimental'): | |
39 experimental_apis.append({ | |
40 'name': template_name.replace('_', '.') | |
41 }) | |
42 else: | |
43 chrome_apis.append({ 'name': template_name.replace('_', '.') }) | |
44 chrome_apis[-1]['last'] = True | |
45 experimental_apis[-1]['last'] = True | |
46 return { | |
47 'chrome': chrome_apis, | |
48 'experimental': experimental_apis | |
49 } | |
50 | |
51 def _ListAPIs(self, apis): | |
52 api_names = set(SanitizeAPIName(name, self._api_path) for name in apis) | |
53 return { | |
54 'apps': self._GetAPIsInSubdirectory(api_names, 'apps'), | |
55 'extensions': self._GetAPIsInSubdirectory(api_names, 'extensions') | |
56 } | |
57 | 68 |
58 def __getitem__(self, key): | 69 def __getitem__(self, key): |
59 return self.get(key) | 70 return self.get(key) |
60 | 71 |
61 def get(self, key): | 72 def get(self, key): |
62 try: | 73 try: |
63 return self._cache.GetFromFileListing(self._api_path)[key] | 74 return self._cache.GetFromFileListing(self._api_path)[key] |
64 except FileNotFoundError as e: | 75 except FileNotFoundError as e: |
65 raise ValueError(str(e) + ': Error listing files for "%s".' % key) | 76 raise ValueError('%s: Error listing files for "%s".' % (e, key)) |
OLD | NEW |