Chromium Code Reviews| 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 69167eb041b08311dfb4e9af6b55ab88926368ec..ab14f143d5258b412dc1107494841ae05bc5ea70 100644 |
| --- a/chrome/common/extensions/docs/server2/samples_data_source.py |
| +++ b/chrome/common/extensions/docs/server2/samples_data_source.py |
| @@ -6,7 +6,12 @@ import json |
| import logging |
| import re |
| +import third_party.json_schema_compiler.json_comment_eater as json_comment_eater |
| + |
| DEFAULT_ICON_PATH = '/images/sample-default-icon.png' |
| +GITHUB_BASE = 'https://github.com/GoogleChrome/chrome-app-samples/tree/master' |
|
not at google - send to devlin
2012/07/30 11:24:05
looks like this is kinda duplicated in echo_handle
cduvall
2012/08/02 01:14:53
Done.
|
| +RAW_GITHUB_BASE = ('https://github.com/GoogleChrome/chrome-app-samples/raw/' |
| + 'master') |
| class SamplesDataSource(object): |
| """Constructs a list of samples and their respective files and api calls. |
| @@ -16,17 +21,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, |
| + zip_file_system, |
| + cache_builder, |
| + zip_cache_builder, |
| + samples_path): |
| + self._file_system = file_system |
| + self._zip_file_system = zip_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 = zip_cache_builder.build( |
| + lambda x: self._MakeSamplesList(x, 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 +52,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 +65,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 +75,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._zip_file_system if is_apps else self._file_system |
| samples_list = [] |
| for filename in sorted(files): |
| if filename.rsplit('/')[-1] != 'manifest.json': |
| @@ -70,7 +86,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,8 +106,11 @@ class SamplesDataSource(object): |
| 'name': item, |
| 'link': self._MakeApiLink('method', item) |
| }) |
| - l10n_data = self._GetDataFromManifest(sample_path) |
| + l10n_data = self._GetDataFromManifest(sample_path, file_system) |
| sample_base_path = sample_path.split('/', 1)[1] |
| + if is_apps: |
| + l10n_data['github_path'] = GITHUB_BASE + '/' + sample_base_path |
| + sample_base_path = RAW_GITHUB_BASE + '/' + sample_base_path |
| if l10n_data['icon'] is None: |
| icon_path = self._static_path + DEFAULT_ICON_PATH |
| else: |
| @@ -106,8 +125,9 @@ class SamplesDataSource(object): |
| 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 |
| @@ -122,7 +142,11 @@ class SamplesDataSource(object): |
| return self.get(key) |
| def get(self, key): |
| - samples_list = self._cache.GetFromFileListing(self._samples_path + '/') |
| + 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'] |