Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1107)

Unified Diff: chrome/common/extensions/docs/server2/samples_data_source.py

Issue 10825067: Extensions Docs Server: Apps samples page (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Samples on API pages Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..2ce88ba10bd1bd971d0f76013e3bff7e2035c247 100644
--- a/chrome/common/extensions/docs/server2/samples_data_source.py
+++ b/chrome/common/extensions/docs/server2/samples_data_source.py
@@ -6,6 +6,9 @@ import json
import logging
import re
+import third_party.json_schema_compiler.json_comment_eater as json_comment_eater
+import url_constants
+
DEFAULT_ICON_PATH = '/images/sample-default-icon.png'
class SamplesDataSource(object):
@@ -16,17 +19,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 +50,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 +63,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 +73,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 +84,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 +104,33 @@ 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:
+ manifest_data['github_path'] = (url_constants.GITHUB_BASE + '/' +
+ sample_base_path)
+ sample_base_path = (url_constants.RAW_GITHUB_BASE + '/' +
not at google - send to devlin 2012/08/09 07:52:36 nit: line-break like sample_base_path = ( url
cduvall 2012/08/09 19:20:08 Done.
+ sample_base_path)
+ 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 = sample_base_path + '/' + manifest_data['icon']
+ manifest_data.update({
'icon': icon_path,
'path': sample_base_path,
'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 +138,12 @@ 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 _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 +170,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: {})()

Powered by Google App Engine
This is Rietveld 408576698