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 from operator import itemgetter | 5 from operator import itemgetter |
6 import os | 6 import os |
7 import posixpath | 7 import posixpath |
8 | 8 |
9 import docs_server_utils as utils | 9 import docs_server_utils as utils |
| 10 from branch_utility import ChannelInfo |
10 from extensions_paths import PUBLIC_TEMPLATES | 11 from extensions_paths import PUBLIC_TEMPLATES |
| 12 from file_system import FileNotFoundError |
11 | 13 |
12 def _GetAPICategory(api, documented_apis): | 14 def _GetAPICategory(api_name, documented_apis): |
13 name = api['name'] | 15 if (api_name.endswith('Private') or |
14 if (name.endswith('Private') or | 16 api_name not in documented_apis): |
15 name not in documented_apis): | |
16 return 'private' | 17 return 'private' |
17 if name.startswith('experimental.'): | 18 if api_name.startswith('experimental.'): |
18 return 'experimental' | 19 return 'experimental' |
19 return 'chrome' | 20 return 'chrome' |
20 | 21 |
21 | 22 |
22 class APIListDataSource(object): | 23 class APIListDataSource(object): |
23 """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs | 24 """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs |
24 for extensions and apps that are used in the api_index.html, | 25 for extensions and apps that are used in the api_index.html, |
25 experimental.html, and private_apis.html pages. | 26 experimental.html, and private_apis.html pages. |
26 | 27 |
27 An API is considered listable if it is listed in _api_features.json, | 28 An API is considered listable if it is listed in _api_features.json, |
28 it has a corresponding HTML file in the public template path, and one of | 29 it has a corresponding HTML file in the public template path, and one of |
29 the following conditions is met: | 30 the following conditions is met: |
30 - It has no "dependencies" or "extension_types" properties in _api_features | 31 - It has no "dependencies" or "extension_types" properties in _api_features |
31 - It has an "extension_types" property in _api_features with either/both | 32 - It has an "extension_types" property in _api_features with either/both |
32 "extension"/"platform_app" values present. | 33 "extension"/"platform_app" values present. |
33 - It has a dependency in _{api,manifest,permission}_features with an | 34 - It has a dependency in _{api,manifest,permission}_features with an |
34 "extension_types" property where either/both "extension"/"platform_app" | 35 "extension_types" property where either/both "extension"/"platform_app" |
35 values are present. | 36 values are present. |
36 """ | 37 """ |
37 class Factory(object): | 38 class Factory(object): |
38 def __init__(self, | 39 def __init__(self, |
39 compiled_fs_factory, | 40 compiled_fs_factory, |
40 file_system, | 41 file_system, |
41 features_bundle, | 42 features_bundle, |
42 object_store_creator): | 43 object_store_creator, |
| 44 api_models, |
| 45 availability_finder): |
43 self._file_system = file_system | 46 self._file_system = file_system |
44 self._cache = compiled_fs_factory.Create(file_system, | 47 self._cache = compiled_fs_factory.Create(file_system, |
45 self._CollectDocumentedAPIs, | 48 self._CollectDocumentedAPIs, |
46 APIListDataSource) | 49 APIListDataSource) |
47 self._features_bundle = features_bundle | 50 self._features_bundle = features_bundle |
48 self._object_store_creator = object_store_creator | 51 self._object_store_creator = object_store_creator |
| 52 self._api_models = api_models |
| 53 self._availability_finder = availability_finder |
| 54 |
| 55 def _GetDocumentedApis(self): |
| 56 return self._cache.GetFromFileListing(self._public_template_path).Get() |
49 | 57 |
50 def _CollectDocumentedAPIs(self, base_dir, files): | 58 def _CollectDocumentedAPIs(self, base_dir, files): |
51 def GetDocumentedAPIsForPlatform(names, platform): | 59 def GetDocumentedAPIsForPlatform(names, platform): |
52 public_templates = [] | 60 public_templates = [] |
53 for root, _, files in self._file_system.Walk(posixpath.join( | 61 for root, _, files in self._file_system.Walk(posixpath.join( |
54 PUBLIC_TEMPLATES, platform)): | 62 PUBLIC_TEMPLATES, platform)): |
55 public_templates.extend( | 63 public_templates.extend( |
56 ('%s/%s' % (root, name)).lstrip('/') for name in files) | 64 ('%s/%s' % (root, name)).lstrip('/') for name in files) |
57 template_names = set(os.path.splitext(name)[0] | 65 template_names = set(os.path.splitext(name)[0] |
58 for name in public_templates) | 66 for name in public_templates) |
59 return [name.replace('_', '.') for name in template_names] | 67 return [name.replace('_', '.') for name in template_names] |
60 api_names = set(utils.SanitizeAPIName(name) for name in files) | 68 api_names = set(utils.SanitizeAPIName(name) for name in files) |
61 return { | 69 return { |
62 'apps': GetDocumentedAPIsForPlatform(api_names, 'apps'), | 70 'apps': GetDocumentedAPIsForPlatform(api_names, 'apps'), |
63 'extensions': GetDocumentedAPIsForPlatform(api_names, 'extensions') | 71 'extensions': GetDocumentedAPIsForPlatform(api_names, 'extensions') |
64 } | 72 } |
65 | 73 |
66 def _GenerateAPIDict(self): | 74 def _GenerateAPIDict(self): |
| 75 |
67 documented_apis = self._cache.GetFromFileListing(PUBLIC_TEMPLATES).Get() | 76 documented_apis = self._cache.GetFromFileListing(PUBLIC_TEMPLATES).Get() |
68 api_features = self._features_bundle.GetAPIFeatures().Get() | |
69 | 77 |
70 def FilterAPIs(platform): | 78 def GetChannelInfo(api_name): |
71 return (api for api in api_features.itervalues() | 79 return self._availability_finder.GetApiAvailability(api_name) |
72 if platform in api['platforms']) | 80 |
| 81 def GetApiDescription(model): |
| 82 try: |
| 83 return model.Get().description if model.Get() else '' |
| 84 except FileNotFoundError: |
| 85 return '' |
| 86 |
| 87 def GetApiPlatform(api_name): |
| 88 feature = self._features_bundle.GetAPIFeatures().Get()[api_name] |
| 89 return feature['platforms'] |
73 | 90 |
74 def MakeDictForPlatform(platform): | 91 def MakeDictForPlatform(platform): |
75 platform_dict = { 'chrome': [], 'experimental': [], 'private': [] } | 92 platform_dict = { |
76 for api in FilterAPIs(platform): | 93 'chrome': {'stable': [], 'beta': [], 'dev': [], 'trunk': []} |
77 if api['name'] in documented_apis[platform]: | 94 } |
78 category = _GetAPICategory(api, documented_apis[platform]) | 95 private_apis = [] |
79 platform_dict[category].append(api) | 96 experimental_apis = [] |
80 for category, apis in platform_dict.iteritems(): | 97 all_apis = [] |
81 platform_dict[category] = sorted(apis, key=itemgetter('name')) | 98 for api_name, api_model in self._api_models.IterModels(): |
82 utils.MarkLast(platform_dict[category]) | 99 api = { |
| 100 'name': api_name, |
| 101 'description': GetApiDescription(api_model), |
| 102 'platforms': GetApiPlatform(api_name), |
| 103 } |
| 104 category = _GetAPICategory(api_name, documented_apis[platform]) |
| 105 if category == 'chrome': |
| 106 channel_info = GetChannelInfo(api_name) |
| 107 channel = channel_info.channel |
| 108 if channel == 'stable': |
| 109 api['version'] = channel_info.version |
| 110 platform_dict[category][channel].append(api) |
| 111 all_apis.append(api) |
| 112 elif category == 'experimental': |
| 113 experimental_apis.append(api) |
| 114 all_apis.append(api) |
| 115 elif category == 'private': |
| 116 private_apis.append(api) |
| 117 |
| 118 for channel, apis_by_channel in platform_dict['chrome'].iteritems(): |
| 119 apis_by_channel = sorted(apis_by_channel, key=itemgetter('name')) |
| 120 utils.MarkLast(apis_by_channel) |
| 121 platform_dict['chrome'][channel] = apis_by_channel |
| 122 |
| 123 for key, apis in (('all', all_apis), |
| 124 ('private', private_apis), |
| 125 ('experimental', experimental_apis)): |
| 126 apis = sorted(apis, key=itemgetter('name')) |
| 127 utils.MarkLast(apis) |
| 128 platform_dict[key] = apis |
83 return platform_dict | 129 return platform_dict |
84 | |
85 return { | 130 return { |
86 'apps': MakeDictForPlatform('apps'), | 131 'apps': MakeDictForPlatform('apps'), |
87 'extensions': MakeDictForPlatform('extensions') | 132 'extensions': MakeDictForPlatform('extensions'), |
88 } | 133 } |
89 | 134 |
90 def Create(self): | 135 def Create(self): |
91 return APIListDataSource(self, self._object_store_creator) | 136 return APIListDataSource(self, self._object_store_creator) |
92 | 137 |
93 def __init__(self, factory, object_store_creator): | 138 def __init__(self, factory, object_store_creator): |
94 self._factory = factory | 139 self._factory = factory |
95 self._object_store = object_store_creator.Create(APIListDataSource) | 140 self._object_store = object_store_creator.Create(APIListDataSource) |
96 | 141 |
97 def _GetCachedAPIData(self): | 142 def _GetCachedAPIData(self): |
98 data = self._object_store.Get('api_data').Get() | 143 data = self._object_store.Get('api_data').Get() |
99 if data is None: | 144 if data is None: |
100 data = self._factory._GenerateAPIDict() | 145 data = self._factory._GenerateAPIDict() |
101 self._object_store.Set('api_data', data) | 146 self._object_store.Set('api_data', data) |
102 return data | 147 return data |
103 | 148 |
104 def get(self, key): | 149 def get(self, key): |
105 return self._GetCachedAPIData().get(key) | 150 return self._GetCachedAPIData().get(key) |
OLD | NEW |