| 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 import os | 6 import os |
| 6 | 7 |
| 8 from third_party.json_schema_compiler.json_parse import Parse |
| 7 import third_party.json_schema_compiler.model as model | 9 import third_party.json_schema_compiler.model as model |
| 8 import docs_server_utils as utils | 10 import docs_server_utils as utils |
| 9 | 11 |
| 12 def _GetAPICategory(api, documented_apis): |
| 13 name = api['name'] |
| 14 if (name.endswith('Private') or |
| 15 name not in documented_apis): |
| 16 return 'private' |
| 17 if name.startswith('experimental.'): |
| 18 return 'experimental' |
| 19 return 'chrome' |
| 20 |
| 21 |
| 10 class APIListDataSource(object): | 22 class APIListDataSource(object): |
| 11 """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs | 23 """ This class creates a list of chrome.* APIs and chrome.experimental.* APIs |
| 12 for extensions and apps that are used in the api_index.html and | 24 for extensions and apps that are used in the api_index.html and |
| 13 experimental.html pages. | 25 experimental.html pages. |
| 14 |api_path| is the path to the API schemas. | 26 |
| 15 |public_path| is the path to the public HTML templates. | 27 An API is considered listable if it is listed in _api_features.json, |
| 16 An API is considered listable if it's in both |api_path| and |public_path| - | 28 it has a corresponding HTML file in the public template path, and one of |
| 17 the API schemas may contain undocumentable APIs, and the public HTML templates | 29 the following conditions is met: |
| 18 will contain non-API articles. | 30 - 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 "extension"/"platform_app" values present. |
| 33 - It has a dependency in _{api,manifest,permission}_features with an |
| 34 "extension_types" property where either/both "extension"/"platform_app" |
| 35 values are present. |
| 19 """ | 36 """ |
| 20 class Factory(object): | 37 class Factory(object): |
| 21 def __init__(self, compiled_fs_factory, file_system, api_path, public_path): | 38 def __init__(self, |
| 22 self._compiled_fs = compiled_fs_factory.Create(self._ListAPIs, | 39 compiled_fs_factory, |
| 23 APIListDataSource) | 40 file_system, |
| 41 public_template_path, |
| 42 features_bundle, |
| 43 object_store_creator): |
| 24 self._file_system = file_system | 44 self._file_system = file_system |
| 25 def Normalize(string): | 45 def NormalizePath(string): |
| 26 return string if string.endswith('/') else (string + '/') | 46 return string if string.endswith('/') else (string + '/') |
| 27 self._api_path = Normalize(api_path) | 47 self._public_template_path = NormalizePath(public_template_path) |
| 28 self._public_path = Normalize(public_path) | 48 self._cache = compiled_fs_factory.Create(self._CollectDocumentedAPIs, |
| 49 APIListDataSource) |
| 50 self._features_bundle = features_bundle |
| 51 self._object_store_creator = object_store_creator |
| 29 | 52 |
| 30 def _GetAPIsInSubdirectory(self, api_names, doc_type): | 53 def _CollectDocumentedAPIs(self, base_dir, files): |
| 31 public_templates = [] | 54 def GetDocumentedAPIsForPlatform(names, platform): |
| 32 for root, _, files in self._file_system.Walk( | 55 public_templates = [] |
| 33 self._public_path + doc_type): | 56 for root, _, files in self._file_system.Walk( |
| 34 public_templates.extend( | 57 self._public_template_path + platform): |
| 35 ('%s/%s' % (root, name)).lstrip('/') for name in files) | 58 public_templates.extend( |
| 36 template_names = set(os.path.splitext(name)[0] | 59 ('%s/%s' % (root, name)).lstrip('/') for name in files) |
| 37 for name in public_templates) | 60 template_names = set(os.path.splitext(name)[0] |
| 38 experimental_apis = [] | 61 for name in public_templates) |
| 39 chrome_apis = [] | 62 return [name.replace('_', '.') for name in template_names] |
| 40 private_apis = [] | 63 api_names = set(utils.SanitizeAPIName(name) for name in files) |
| 41 for template_name in sorted(template_names): | |
| 42 if model.UnixName(template_name) not in api_names: | |
| 43 continue | |
| 44 entry = {'name': template_name.replace('_', '.')} | |
| 45 if template_name.startswith('experimental'): | |
| 46 experimental_apis.append(entry) | |
| 47 elif template_name.endswith('Private'): | |
| 48 private_apis.append(entry) | |
| 49 else: | |
| 50 chrome_apis.append(entry) | |
| 51 if len(chrome_apis): | |
| 52 chrome_apis[-1]['last'] = True | |
| 53 if len(experimental_apis): | |
| 54 experimental_apis[-1]['last'] = True | |
| 55 if len(private_apis): | |
| 56 private_apis[-1]['last'] = True | |
| 57 return { | 64 return { |
| 58 'chrome': chrome_apis, | 65 'apps': GetDocumentedAPIsForPlatform(api_names, 'apps'), |
| 59 'experimental': experimental_apis, | 66 'extensions': GetDocumentedAPIsForPlatform(api_names, 'extensions') |
| 60 'private': private_apis | |
| 61 } | 67 } |
| 62 | 68 |
| 63 def _ListAPIs(self, base_dir, apis): | 69 def _GenerateAPIDict(self): |
| 64 api_names = set(utils.SanitizeAPIName(name) for name in apis) | 70 documented_apis = self._cache.GetFromFileListing( |
| 71 self._public_template_path) |
| 72 api_features = self._features_bundle.GetAPIFeatures() |
| 73 |
| 74 def FilterAPIs(platform): |
| 75 return (api for api in api_features.itervalues() |
| 76 if platform in api['platforms']) |
| 77 |
| 78 def MakeDictForPlatform(platform): |
| 79 platform_dict = { 'chrome': [], 'experimental': [], 'private': [] } |
| 80 for api in FilterAPIs(platform): |
| 81 category = _GetAPICategory(api, documented_apis[platform]) |
| 82 platform_dict[category].append(api) |
| 83 for category, apis in platform_dict.iteritems(): |
| 84 platform_dict[category] = sorted(apis, key=itemgetter('name')) |
| 85 utils.MarkLast(platform_dict[category]) |
| 86 return platform_dict |
| 87 |
| 65 return { | 88 return { |
| 66 'apps': self._GetAPIsInSubdirectory(api_names, 'apps'), | 89 'apps': MakeDictForPlatform('apps'), |
| 67 'extensions': self._GetAPIsInSubdirectory(api_names, 'extensions') | 90 'extensions': MakeDictForPlatform('extensions') |
| 68 } | 91 } |
| 69 | 92 |
| 70 def Create(self): | 93 def Create(self): |
| 71 return APIListDataSource(self._compiled_fs, self._api_path) | 94 return APIListDataSource(self, self._object_store_creator) |
| 72 | 95 |
| 73 def __init__(self, compiled_fs, api_path): | 96 def __init__(self, factory, object_store_creator): |
| 74 self._compiled_fs = compiled_fs | 97 self._factory = factory |
| 75 self._api_path = api_path | 98 self._object_store = object_store_creator.Create(APIListDataSource) |
| 76 | 99 |
| 77 def GetAllNames(self): | 100 def GetAllNames(self): |
| 78 names = [] | 101 apis = [] |
| 79 for platform in ['apps', 'extensions']: | 102 for platform in ['apps', 'extensions']: |
| 80 for category in ['chrome', 'experimental', 'private']: | 103 for category in ['chrome', 'experimental', 'private']: |
| 81 names.extend(self.get(platform).get(category)) | 104 apis.extend(self.get(platform).get(category)) |
| 82 return [api_name['name'] for api_name in names] | 105 return [api['name'] for api in apis] |
| 106 |
| 107 def _GetCachedAPIData(self): |
| 108 data = self._object_store.Get('api_data').Get() |
| 109 if data is None: |
| 110 data = self._factory._GenerateAPIDict() |
| 111 self._object_store.Set('api_data', data) |
| 112 return data |
| 83 | 113 |
| 84 def get(self, key): | 114 def get(self, key): |
| 85 return self._compiled_fs.GetFromFileListing(self._api_path)[key] | 115 return self._GetCachedAPIData().get(key) |
| OLD | NEW |