| 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 import features_utility as features |
| 9 from third_party.json_schema_compiler.json_parse import Parse |
| 7 import third_party.json_schema_compiler.model as model | 10 import third_party.json_schema_compiler.model as model |
| 8 import docs_server_utils as utils | 11 import docs_server_utils as utils |
| 9 | 12 |
| 13 def _GetAPICategory(api, platform, public_apis): |
| 14 name = api['name'] |
| 15 if name.startswith('experimental.'): |
| 16 return 'experimental' |
| 17 if (name.endswith('Private') or |
| 18 name not in public_apis[platform]): |
| 19 return 'private' |
| 20 return 'chrome' |
| 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 file_system, |
| 23 APIListDataSource) | 40 compiled_fs_factory, |
| 41 public_template_path, |
| 42 features_bundle): |
| 24 self._file_system = file_system | 43 self._file_system = file_system |
| 25 def Normalize(string): | 44 def NormalizePath(string): |
| 26 return string if string.endswith('/') else (string + '/') | 45 return string if string.endswith('/') else (string + '/') |
| 27 self._api_path = Normalize(api_path) | 46 self._public_template_path = NormalizePath(public_template_path) |
| 28 self._public_path = Normalize(public_path) | 47 self._cache = compiled_fs_factory.Create(self._CollectPublicAPIs, |
| 48 APIListDataSource) |
| 49 self._features_bundle = features_bundle |
| 29 | 50 |
| 30 def _GetAPIsInSubdirectory(self, api_names, doc_type): | 51 def _CollectPublicAPIs(self, base_dir, files): |
| 31 public_templates = [] | 52 def GetAPIsForPlatform(names, platform): |
| 32 for root, _, files in self._file_system.Walk( | 53 public_templates = [] |
| 33 self._public_path + doc_type): | 54 for root, _, files in self._file_system.Walk( |
| 34 public_templates.extend( | 55 self._public_template_path + platform): |
| 35 ('%s/%s' % (root, name)).lstrip('/') for name in files) | 56 public_templates.extend( |
| 36 template_names = set(os.path.splitext(name)[0] | 57 ('%s/%s' % (root, name)).lstrip('/') for name in files) |
| 37 for name in public_templates) | 58 template_names = set(os.path.splitext(name)[0] |
| 38 experimental_apis = [] | 59 for name in public_templates) |
| 39 chrome_apis = [] | 60 return sorted(map(lambda name: name.replace('_', '.'), template_names)) |
| 40 private_apis = [] | 61 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 { | 62 return { |
| 58 'chrome': chrome_apis, | 63 'apps': GetAPIsForPlatform(api_names, 'apps'), |
| 59 'experimental': experimental_apis, | 64 'extensions': GetAPIsForPlatform(api_names, 'extensions') |
| 60 'private': private_apis | |
| 61 } | 65 } |
| 62 | 66 |
| 63 def _ListAPIs(self, base_dir, apis): | 67 def _GenerateAPIDict(self): |
| 64 api_names = set(utils.SanitizeAPIName(name) for name in apis) | 68 def MakeEmptyAPIDict(): |
| 65 return { | 69 return { 'chrome': [], 'experimental': [], 'private': [] } |
| 66 'apps': self._GetAPIsInSubdirectory(api_names, 'apps'), | 70 api_dict = { |
| 67 'extensions': self._GetAPIsInSubdirectory(api_names, 'extensions') | 71 'apps': MakeEmptyAPIDict(), |
| 72 'extensions': MakeEmptyAPIDict() |
| 68 } | 73 } |
| 74 public_apis = self._cache.GetFromFileListing(self._public_template_path) |
| 75 api_features = self._features_bundle.get('api') |
| 76 for platform in api_dict.iterkeys(): |
| 77 for name, api in api_features.get(platform).iteritems(): |
| 78 category = _GetAPICategory(api, platform, public_apis) |
| 79 api_dict[platform][category].append(api) |
| 80 for platform in api_dict.iterkeys(): |
| 81 for category in api_dict[platform]: |
| 82 api_dict[platform][category] = sorted(api_dict[platform][category], |
| 83 key=itemgetter('name')) |
| 84 api_dict[platform][category][-1]['last'] = True |
| 85 return api_dict |
| 69 | 86 |
| 70 def Create(self): | 87 def Create(self): |
| 71 return APIListDataSource(self._compiled_fs, self._api_path) | 88 return APIListDataSource(self._GenerateAPIDict()) |
| 72 | 89 |
| 73 def __init__(self, compiled_fs, api_path): | 90 def __init__(self, apis): |
| 74 self._compiled_fs = compiled_fs | 91 self._apis = apis |
| 75 self._api_path = api_path | |
| 76 | 92 |
| 77 def GetAllNames(self): | 93 def GetAllNames(self): |
| 78 names = [] | 94 apis = [] |
| 79 for platform in ['apps', 'extensions']: | 95 for platform in ['apps', 'extensions']: |
| 80 for category in ['chrome', 'experimental', 'private']: | 96 for category in ['chrome', 'experimental', 'private']: |
| 81 names.extend(self.get(platform).get(category)) | 97 apis.extend(self._apis[platform][category]) |
| 82 return [api_name['name'] for api_name in names] | 98 return sorted(api['name'] for api in apis) |
| 83 | 99 |
| 84 def get(self, key): | 100 def get(self, key): |
| 85 return self._compiled_fs.GetFromFileListing(self._api_path)[key] | 101 return self._apis.get(key) |
| OLD | NEW |