Index: chrome/common/extensions/docs/server2/samples_data_source.py |
diff --git a/chrome/common/extensions/docs/server2/samples_data_source.py b/chrome/common/extensions/docs/server2/samples_data_source.py |
index a1281941e224fbe51ee03af29a5db22e9346e3a9..c712651e8a96453ec3ff98685e1fd2b40554d20b 100644 |
--- a/chrome/common/extensions/docs/server2/samples_data_source.py |
+++ b/chrome/common/extensions/docs/server2/samples_data_source.py |
@@ -6,6 +6,10 @@ import json |
import logging |
import re |
+import third_party.json_schema_compiler.json_comment_eater as json_comment_eater |
+import third_party.json_schema_compiler.model as model |
+import url_constants |
+ |
DEFAULT_ICON_PATH = '/images/sample-default-icon.png' |
class SamplesDataSource(object): |
@@ -16,17 +20,27 @@ class SamplesDataSource(object): |
"""A factory to create SamplesDataSource instances bound to individual |
Requests. |
""" |
- def __init__(self, branch, file_system, cache_builder, samples_path): |
+ def __init__(self, |
+ branch, |
+ file_system, |
+ github_file_system, |
+ cache_builder, |
+ github_cache_builder, |
+ samples_path): |
+ self._file_system = file_system |
+ self._github_file_system = github_file_system |
self._static_path = ((('/' + branch) if branch != 'local' else '') + |
'/static') |
- self._file_system = file_system |
- self._cache = cache_builder.build(self._MakeSamplesList) |
+ self._extensions_cache = cache_builder.build(self._MakeSamplesList) |
+ self._apps_cache = github_cache_builder.build( |
+ lambda x: self._MakeSamplesList(x, is_apps=True)) |
self._samples_path = samples_path |
def Create(self, request): |
"""Returns a new SamplesDataSource bound to |request|. |
""" |
- return SamplesDataSource(self._cache, |
+ return SamplesDataSource(self._extensions_cache, |
+ self._apps_cache, |
self._samples_path, |
request) |
@@ -37,9 +51,9 @@ class SamplesDataSource(object): |
api, name = item.replace('chrome.', '').split('.', 1) |
return api + '.html#' + prefix + '-' + name |
- def _GetDataFromManifest(self, path): |
- manifest = self._file_system.ReadSingle(path + '/manifest.json') |
- manifest_json = json.loads(manifest) |
+ def _GetDataFromManifest(self, path, file_system): |
+ manifest = file_system.ReadSingle(path + '/manifest.json') |
+ manifest_json = json.loads(json_comment_eater.Nom(manifest)) |
l10n_data = { |
'name': manifest_json.get('name', ''), |
'description': manifest_json.get('description', ''), |
@@ -50,9 +64,9 @@ class SamplesDataSource(object): |
if not l10n_data['default_locale']: |
return l10n_data |
locales_path = path + '/_locales/' |
- locales_dir = self._file_system.ReadSingle(locales_path) |
+ locales_dir = file_system.ReadSingle(locales_path) |
if locales_dir: |
- locales_files = self._file_system.Read( |
+ locales_files = file_system.Read( |
[locales_path + f + 'messages.json' for f in locales_dir]).Get() |
locales_json = [(path, json.loads(contents)) |
for path, contents in locales_files.iteritems()] |
@@ -60,7 +74,8 @@ class SamplesDataSource(object): |
l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_ |
return l10n_data |
- def _MakeSamplesList(self, files): |
+ def _MakeSamplesList(self, files, is_apps=False): |
+ file_system = self._github_file_system if is_apps else self._file_system |
samples_list = [] |
for filename in sorted(files): |
if filename.rsplit('/')[-1] != 'manifest.json': |
@@ -70,7 +85,7 @@ class SamplesDataSource(object): |
sample_files = [path for path in files |
if path.startswith(sample_path + '/')] |
js_files = [path for path in sample_files if path.endswith('.js')] |
- js_contents = self._file_system.Read(js_files).Get() |
+ js_contents = file_system.Read(js_files).Get() |
api_items = set() |
for js in js_contents.values(): |
api_items.update(self._GetApiItems(js)) |
@@ -90,33 +105,39 @@ class SamplesDataSource(object): |
'name': item, |
'link': self._MakeApiLink('method', item) |
}) |
- l10n_data = self._GetDataFromManifest(sample_path) |
+ manifest_data = self._GetDataFromManifest(sample_path, file_system) |
+ |
sample_base_path = sample_path.split('/', 1)[1] |
- if l10n_data['icon'] is None: |
+ if is_apps: |
+ url = url_constants.GITHUB_BASE + '/' + sample_base_path |
+ icon_base = url_constants.RAW_GITHUB_BASE + '/' + sample_base_path |
+ download_url = url |
+ else: |
+ url = sample_base_path |
+ icon_base = sample_base_path |
+ download_url = sample_base_path + '.zip' |
+ |
+ if manifest_data['icon'] is None: |
icon_path = self._static_path + DEFAULT_ICON_PATH |
else: |
- icon_path = '/' + sample_base_path + '/' + l10n_data['icon'] |
- l10n_data.update({ |
+ icon_path = icon_base + '/' + manifest_data['icon'] |
+ manifest_data.update({ |
'icon': icon_path, |
- 'path': sample_base_path, |
+ 'download_url': download_url, |
+ 'url': url, |
'files': [f.replace(sample_path + '/', '') for f in sample_files], |
'api_calls': api_calls |
}) |
- samples_list.append(l10n_data) |
+ samples_list.append(manifest_data) |
return samples_list |
- def __init__(self, cache, samples_path, request): |
- self._cache = cache |
+ def __init__(self, extensions_cache, apps_cache, samples_path, request): |
+ self._extensions_cache = extensions_cache |
+ self._apps_cache = apps_cache |
self._samples_path = samples_path |
self._request = request |
- def GetSamplesForAPI(self, api_name): |
- samples = self.values() |
- api_search = '.' + api_name + '.' |
- return [sample for sample in samples |
- if any(api_search in api['name'] for api in sample['api_calls'])] |
- |
def _GetAcceptedLanguages(self): |
accept_language = self._request.headers.get('Accept-Language', None) |
if accept_language is None: |
@@ -124,14 +145,33 @@ class SamplesDataSource(object): |
return [lang_with_q.split(';')[0].strip() |
for lang_with_q in accept_language.split(',')] |
- def __getitem__(self, key): |
- return self.get(key) |
- |
- def values(self): |
- return self.get('') |
- |
- def get(self, key): |
- samples_list = self._cache.GetFromFileListing(self._samples_path + '/') |
+ def FilterSamples(self, key, api_name): |
+ """Fetches and filters the list of samples specified by |key|, returning |
+ only the samples that use the API |api_name|. |key| is either 'apps' or |
+ 'extensions'. |
+ """ |
+ api_search = '_' + api_name + '_' |
+ samples_list = [] |
+ try: |
+ for sample in self.get(key): |
+ api_calls_unix = [model.UnixName(call['name']) |
+ for call in sample['api_calls']] |
+ for call in api_calls_unix: |
+ if api_search in call: |
+ samples_list.append(sample) |
+ break |
+ except NotImplementedError: |
+ # If we're testing, the GithubFileSystem can't fetch samples. |
+ # Bug: http://crbug.com/141910 |
+ return [] |
+ return samples_list |
+ |
+ def _CreateSamplesDict(self, key): |
+ if key == 'apps': |
+ samples_list = self._apps_cache.GetFromFileListing('/') |
+ else: |
+ samples_list = self._extensions_cache.GetFromFileListing( |
+ self._samples_path + '/') |
return_list = [] |
for dict_ in samples_list: |
name = dict_['name'] |
@@ -158,3 +198,12 @@ class SamplesDataSource(object): |
else: |
return_list.append(dict_) |
return return_list |
+ |
+ def __getitem__(self, key): |
+ return self.get(key) |
+ |
+ def get(self, key): |
+ return { |
+ 'apps': lambda: self._CreateSamplesDict('apps'), |
+ 'extensions': lambda: self._CreateSamplesDict('extensions') |
+ }.get(key, lambda: {})() |