Chromium Code Reviews| 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 | 7 |
| 8 from third_party.json_schema_compiler.json_parse import Parse | |
| 9 import third_party.json_schema_compiler.model as model | |
| 10 import docs_server_utils as utils | 8 import docs_server_utils as utils |
| 9 from file_system import FileNotFoundError | |
| 11 | 10 |
| 12 def _GetAPICategory(api, documented_apis): | 11 def _GetAPICategory(api, documented_apis): |
| 13 name = api['name'] | 12 name = api['name'] |
| 14 if (name.endswith('Private') or | 13 if (name.endswith('Private') or |
| 15 name not in documented_apis): | 14 name not in documented_apis): |
| 16 return 'private' | 15 return 'private' |
| 17 if name.startswith('experimental.'): | 16 if name.startswith('experimental.'): |
| 18 return 'experimental' | 17 return 'experimental' |
| 19 return 'chrome' | 18 return 'chrome' |
| 20 | 19 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 33 - It has a dependency in _{api,manifest,permission}_features with an | 32 - It has a dependency in _{api,manifest,permission}_features with an |
| 34 "extension_types" property where either/both "extension"/"platform_app" | 33 "extension_types" property where either/both "extension"/"platform_app" |
| 35 values are present. | 34 values are present. |
| 36 """ | 35 """ |
| 37 class Factory(object): | 36 class Factory(object): |
| 38 def __init__(self, | 37 def __init__(self, |
| 39 compiled_fs_factory, | 38 compiled_fs_factory, |
| 40 file_system, | 39 file_system, |
| 41 public_template_path, | 40 public_template_path, |
| 42 features_bundle, | 41 features_bundle, |
| 43 object_store_creator): | 42 object_store_creator, |
| 43 api_models, | |
| 44 availability_finder): | |
| 44 self._file_system = file_system | 45 self._file_system = file_system |
| 45 def NormalizePath(string): | 46 def NormalizePath(string): |
| 46 return string if string.endswith('/') else (string + '/') | 47 return string if string.endswith('/') else (string + '/') |
| 47 self._public_template_path = NormalizePath(public_template_path) | 48 self._public_template_path = NormalizePath(public_template_path) |
| 48 self._cache = compiled_fs_factory.Create(file_system, | 49 self._cache = compiled_fs_factory.Create(file_system, |
| 49 self._CollectDocumentedAPIs, | 50 self._CollectDocumentedAPIs, |
| 50 APIListDataSource) | 51 APIListDataSource) |
| 51 self._features_bundle = features_bundle | 52 self._features_bundle = features_bundle |
| 52 self._object_store_creator = object_store_creator | 53 self._object_store_creator = object_store_creator |
| 54 self._api_models = api_models | |
| 55 self._availability_finder = availability_finder | |
| 56 | |
| 57 def _GetDocumentedApis(self): | |
| 58 return self._cache.GetFromFileListing(self._public_template_path).Get() | |
| 59 | |
| 60 def _GetApiFeatures(self): | |
| 61 return self._features_bundle.GetAPIFeatures() | |
| 53 | 62 |
| 54 def _CollectDocumentedAPIs(self, base_dir, files): | 63 def _CollectDocumentedAPIs(self, base_dir, files): |
| 55 def GetDocumentedAPIsForPlatform(names, platform): | 64 def GetDocumentedAPIsForPlatform(names, platform): |
| 56 public_templates = [] | 65 public_templates = [] |
| 57 for root, _, files in self._file_system.Walk( | 66 for root, _, files in self._file_system.Walk( |
| 58 self._public_template_path + platform): | 67 self._public_template_path + platform): |
| 59 public_templates.extend( | 68 public_templates.extend( |
| 60 ('%s/%s' % (root, name)).lstrip('/') for name in files) | 69 ('%s/%s' % (root, name)).lstrip('/') for name in files) |
| 61 template_names = set(os.path.splitext(name)[0] | 70 template_names = set(os.path.splitext(name)[0] |
| 62 for name in public_templates) | 71 for name in public_templates) |
| 63 return [name.replace('_', '.') for name in template_names] | 72 return [name.replace('_', '.') for name in template_names] |
| 64 api_names = set(utils.SanitizeAPIName(name) for name in files) | 73 api_names = set(utils.SanitizeAPIName(name) for name in files) |
| 65 return { | 74 return { |
| 66 'apps': GetDocumentedAPIsForPlatform(api_names, 'apps'), | 75 'apps': GetDocumentedAPIsForPlatform(api_names, 'apps'), |
| 67 'extensions': GetDocumentedAPIsForPlatform(api_names, 'extensions') | 76 'extensions': GetDocumentedAPIsForPlatform(api_names, 'extensions') |
| 68 } | 77 } |
| 69 | 78 |
| 79 def GetAllNames(self): | |
|
not at google - send to devlin
2013/11/02 00:31:48
don't worry about these GetAllNames changes, I'm g
hukun
2013/11/04 11:07:26
OK
| |
| 80 api_features = self._GetApiFeatures().itervalues() | |
| 81 return [api['name'] for api in api_features] | |
| 82 | |
| 70 def _GenerateAPIDict(self): | 83 def _GenerateAPIDict(self): |
| 71 documented_apis = self._cache.GetFromFileListing( | 84 documented_apis = self._GetDocumentedApis() |
| 72 self._public_template_path).Get() | 85 api_features = self._GetApiFeatures() |
| 73 api_features = self._features_bundle.GetAPIFeatures() | |
| 74 | 86 |
| 75 def FilterAPIs(platform): | 87 def FilterAPIs(platform): |
| 76 return (api for api in api_features.itervalues() | 88 return (api for api in api_features.itervalues() |
|
not at google - send to devlin
2013/11/02 00:31:48
I think you could even just inline the whole of ap
hukun
2013/11/04 11:07:26
Done
| |
| 77 if platform in api['platforms']) | 89 if platform in api['platforms']) |
| 78 | 90 |
| 91 def GetChannelInfo(api_name): | |
| 92 return self._availability_finder.GetApiAvailability(api_name) | |
| 93 | |
| 79 def MakeDictForPlatform(platform): | 94 def MakeDictForPlatform(platform): |
| 80 platform_dict = { 'chrome': [], 'experimental': [], 'private': [] } | 95 platform_dict = { 'chrome': {'stable': [],'beta': [], |
| 96 'dev': [],'trunk': [] }, | |
| 97 'experimental': {'stable': [],'beta': [], | |
| 98 'dev': [],'trunk': [] }} | |
|
not at google - send to devlin
2013/11/02 00:31:48
Hm, so, how about we change the way this works.
-
hukun
2013/11/04 11:07:26
This is a little big change, I guess it will influ
not at google - send to devlin
2013/11/04 18:30:49
Sure.
| |
| 99 private_apis = [] | |
| 100 all_apis = [] | |
| 101 | |
| 81 for api in FilterAPIs(platform): | 102 for api in FilterAPIs(platform): |
| 82 category = _GetAPICategory(api, documented_apis[platform]) | 103 category = _GetAPICategory(api, documented_apis[platform]) |
| 83 platform_dict[category].append(api) | 104 if category == 'chrome' or category == 'experimental': |
|
not at google - send to devlin
2013/11/02 00:31:48
if category in ('chrome', 'experimental'):
though
hukun
2013/11/04 11:07:26
Do you mean put "private apis" here, then put "chr
not at google - send to devlin
2013/11/04 18:30:49
Yes
| |
| 84 for category, apis in platform_dict.iteritems(): | 105 api_name = api['name'] |
| 85 platform_dict[category] = sorted(apis, key=itemgetter('name')) | 106 all_apis.append(api) |
| 86 utils.MarkLast(platform_dict[category]) | 107 try: |
| 108 model = self._api_models.GetModel(api_name) | |
| 109 api['description'] = model.Get().description | |
|
not at google - send to devlin
2013/11/02 00:31:48
when is there not a model? there should always be
hukun
2013/11/04 11:07:26
For some private api, e.g. extension.sendNativeMes
not at google - send to devlin
2013/11/04 18:30:49
Ah right. Yes, I just fixed a bug for that in APIM
| |
| 110 except FileNotFoundError: | |
| 111 api['description'] = '' | |
| 112 | |
| 113 channel_info = GetChannelInfo(api_name) | |
| 114 channel = channel_info.channel | |
|
not at google - send to devlin
2013/11/02 00:31:48
inline this
hukun
2013/11/04 11:07:26
Done
| |
| 115 if channel == 'stable': | |
| 116 api['version'] = channel_info.version | |
| 117 platform_dict[category][channel].append(api) | |
| 118 elif category == 'private': | |
| 119 private_apis.append(api) | |
| 120 | |
|
not at google - send to devlin
2013/11/02 00:31:48
extra blank line
hukun
2013/11/04 11:07:26
Done
| |
| 121 | |
| 122 for category, apis_by_channel in platform_dict.iteritems(): | |
| 123 for channel, apis in apis_by_channel.iteritems(): | |
| 124 if len(apis) == 0: | |
| 125 platform_dict[category][channel] = None | |
| 126 continue | |
|
not at google - send to devlin
2013/11/02 00:31:48
why do you need this? an empty list would be simpl
hukun
2013/11/04 11:07:26
I used this to check if there is some data in temp
not at google - send to devlin
2013/11/04 18:30:49
{{?empty_list}} should be false (same as None/Fals
| |
| 127 platform_dict[category][channel] = sorted(apis, | |
| 128 key=itemgetter('name')) | |
| 129 utils.MarkLast(platform_dict[category][channel]) | |
| 130 | |
| 131 all_apis = sorted(all_apis, key=itemgetter('name')) | |
|
not at google - send to devlin
2013/11/02 00:31:48
extra space
hukun
2013/11/04 11:07:26
Done
| |
| 132 platform_dict['all_apis'] = all_apis | |
|
not at google - send to devlin
2013/11/02 00:31:48
you could do just all_apis.sort(key=itemgetter('na
hukun
2013/11/04 11:07:26
Done
| |
| 133 | |
| 134 private_apis = sorted(private_apis, key=itemgetter('name')) | |
| 135 platform_dict['private'] = private_apis | |
| 136 | |
| 87 return platform_dict | 137 return platform_dict |
| 88 | |
| 89 return { | 138 return { |
| 90 'apps': MakeDictForPlatform('apps'), | 139 'apps': MakeDictForPlatform('apps'), |
| 91 'extensions': MakeDictForPlatform('extensions') | 140 'extensions': MakeDictForPlatform('extensions'), |
| 92 } | 141 } |
| 93 | 142 |
| 94 def Create(self): | 143 def Create(self): |
| 95 return APIListDataSource(self, self._object_store_creator) | 144 return APIListDataSource(self, self._object_store_creator) |
| 96 | 145 |
| 97 def __init__(self, factory, object_store_creator): | 146 def __init__(self, factory, object_store_creator): |
| 98 self._factory = factory | 147 self._factory = factory |
| 99 self._object_store = object_store_creator.Create(APIListDataSource) | 148 self._object_store = object_store_creator.Create(APIListDataSource) |
| 100 | 149 |
| 101 def GetAllNames(self): | 150 def GetAllNames(self): |
|
not at google - send to devlin
2013/11/02 00:31:48
ditto. I'm going to delete this.
hukun
2013/11/04 11:07:26
OK
| |
| 102 apis = [] | 151 data = self._object_store.Get('all_names').Get() |
| 103 for platform in ['apps', 'extensions']: | 152 if data is None: |
| 104 for category in ['chrome', 'experimental', 'private']: | 153 data = self._factory.GetAllNames() |
| 105 apis.extend(self.get(platform).get(category)) | 154 self._object_store.Set('all_names', data) |
| 106 return [api['name'] for api in apis] | 155 return data |
| 107 | 156 |
| 108 def _GetCachedAPIData(self): | 157 def _GetCachedAPIData(self): |
| 109 data = self._object_store.Get('api_data').Get() | 158 data = self._object_store.Get('api_data').Get() |
| 110 if data is None: | 159 if data is None: |
| 111 data = self._factory._GenerateAPIDict() | 160 data = self._factory._GenerateAPIDict() |
| 112 self._object_store.Set('api_data', data) | 161 self._object_store.Set('api_data', data) |
| 113 return data | 162 return data |
| 114 | 163 |
| 115 def get(self, key): | 164 def get(self, key): |
| 116 return self._GetCachedAPIData().get(key) | 165 return self._GetCachedAPIData().get(key) |
| OLD | NEW |