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 import json | 5 import json |
6 import logging | 6 import logging |
7 import re | 7 import re |
8 | 8 |
9 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater | |
10 | |
9 DEFAULT_ICON_PATH = '/images/sample-default-icon.png' | 11 DEFAULT_ICON_PATH = '/images/sample-default-icon.png' |
12 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.
| |
13 RAW_GITHUB_BASE = ('https://github.com/GoogleChrome/chrome-app-samples/raw/' | |
14 'master') | |
10 | 15 |
11 class SamplesDataSource(object): | 16 class SamplesDataSource(object): |
12 """Constructs a list of samples and their respective files and api calls. | 17 """Constructs a list of samples and their respective files and api calls. |
13 """ | 18 """ |
14 | 19 |
15 class Factory(object): | 20 class Factory(object): |
16 """A factory to create SamplesDataSource instances bound to individual | 21 """A factory to create SamplesDataSource instances bound to individual |
17 Requests. | 22 Requests. |
18 """ | 23 """ |
19 def __init__(self, branch, file_system, cache_builder, samples_path): | 24 def __init__(self, |
25 branch, | |
26 file_system, | |
27 zip_file_system, | |
28 cache_builder, | |
29 zip_cache_builder, | |
30 samples_path): | |
31 self._file_system = file_system | |
32 self._zip_file_system = zip_file_system | |
20 self._static_path = ((('/' + branch) if branch != 'local' else '') + | 33 self._static_path = ((('/' + branch) if branch != 'local' else '') + |
21 '/static') | 34 '/static') |
22 self._file_system = file_system | 35 self._extensions_cache = cache_builder.build(self._MakeSamplesList) |
23 self._cache = cache_builder.build(self._MakeSamplesList) | 36 self._apps_cache = zip_cache_builder.build( |
37 lambda x: self._MakeSamplesList(x, True)) | |
24 self._samples_path = samples_path | 38 self._samples_path = samples_path |
25 | 39 |
26 def Create(self, request): | 40 def Create(self, request): |
27 """Returns a new SamplesDataSource bound to |request|. | 41 """Returns a new SamplesDataSource bound to |request|. |
28 """ | 42 """ |
29 return SamplesDataSource(self._cache, | 43 return SamplesDataSource(self._extensions_cache, |
44 self._apps_cache, | |
30 self._samples_path, | 45 self._samples_path, |
31 request) | 46 request) |
32 | 47 |
33 def _GetApiItems(self, js_file): | 48 def _GetApiItems(self, js_file): |
34 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) | 49 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) |
35 | 50 |
36 def _MakeApiLink(self, prefix, item): | 51 def _MakeApiLink(self, prefix, item): |
37 api, name = item.replace('chrome.', '').split('.', 1) | 52 api, name = item.replace('chrome.', '').split('.', 1) |
38 return api + '.html#' + prefix + '-' + name | 53 return api + '.html#' + prefix + '-' + name |
39 | 54 |
40 def _GetDataFromManifest(self, path): | 55 def _GetDataFromManifest(self, path, file_system): |
41 manifest = self._file_system.ReadSingle(path + '/manifest.json') | 56 manifest = file_system.ReadSingle(path + '/manifest.json') |
42 manifest_json = json.loads(manifest) | 57 manifest_json = json.loads(json_comment_eater.Nom(manifest)) |
43 l10n_data = { | 58 l10n_data = { |
44 'name': manifest_json.get('name', ''), | 59 'name': manifest_json.get('name', ''), |
45 'description': manifest_json.get('description', ''), | 60 'description': manifest_json.get('description', ''), |
46 'icon': manifest_json.get('icons', {}).get('128', None), | 61 'icon': manifest_json.get('icons', {}).get('128', None), |
47 'default_locale': manifest_json.get('default_locale', None), | 62 'default_locale': manifest_json.get('default_locale', None), |
48 'locales': {} | 63 'locales': {} |
49 } | 64 } |
50 if not l10n_data['default_locale']: | 65 if not l10n_data['default_locale']: |
51 return l10n_data | 66 return l10n_data |
52 locales_path = path + '/_locales/' | 67 locales_path = path + '/_locales/' |
53 locales_dir = self._file_system.ReadSingle(locales_path) | 68 locales_dir = file_system.ReadSingle(locales_path) |
54 if locales_dir: | 69 if locales_dir: |
55 locales_files = self._file_system.Read( | 70 locales_files = file_system.Read( |
56 [locales_path + f + 'messages.json' for f in locales_dir]).Get() | 71 [locales_path + f + 'messages.json' for f in locales_dir]).Get() |
57 locales_json = [(path, json.loads(contents)) | 72 locales_json = [(path, json.loads(contents)) |
58 for path, contents in locales_files.iteritems()] | 73 for path, contents in locales_files.iteritems()] |
59 for path, json_ in locales_json: | 74 for path, json_ in locales_json: |
60 l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_ | 75 l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_ |
61 return l10n_data | 76 return l10n_data |
62 | 77 |
63 def _MakeSamplesList(self, files): | 78 def _MakeSamplesList(self, files, is_apps=False): |
79 file_system = self._zip_file_system if is_apps else self._file_system | |
64 samples_list = [] | 80 samples_list = [] |
65 for filename in sorted(files): | 81 for filename in sorted(files): |
66 if filename.rsplit('/')[-1] != 'manifest.json': | 82 if filename.rsplit('/')[-1] != 'manifest.json': |
67 continue | 83 continue |
68 # This is a little hacky, but it makes a sample page. | 84 # This is a little hacky, but it makes a sample page. |
69 sample_path = filename.rsplit('/', 1)[-2] | 85 sample_path = filename.rsplit('/', 1)[-2] |
70 sample_files = [path for path in files | 86 sample_files = [path for path in files |
71 if path.startswith(sample_path + '/')] | 87 if path.startswith(sample_path + '/')] |
72 js_files = [path for path in sample_files if path.endswith('.js')] | 88 js_files = [path for path in sample_files if path.endswith('.js')] |
73 js_contents = self._file_system.Read(js_files).Get() | 89 js_contents = file_system.Read(js_files).Get() |
74 api_items = set() | 90 api_items = set() |
75 for js in js_contents.values(): | 91 for js in js_contents.values(): |
76 api_items.update(self._GetApiItems(js)) | 92 api_items.update(self._GetApiItems(js)) |
77 | 93 |
78 api_calls = [] | 94 api_calls = [] |
79 for item in api_items: | 95 for item in api_items: |
80 if len(item.split('.')) < 3: | 96 if len(item.split('.')) < 3: |
81 continue | 97 continue |
82 if item.endswith('.addListener'): | 98 if item.endswith('.addListener'): |
83 item = item.replace('.addListener', '') | 99 item = item.replace('.addListener', '') |
84 api_calls.append({ | 100 api_calls.append({ |
85 'name': item, | 101 'name': item, |
86 'link': self._MakeApiLink('event', item) | 102 'link': self._MakeApiLink('event', item) |
87 }) | 103 }) |
88 else: | 104 else: |
89 api_calls.append({ | 105 api_calls.append({ |
90 'name': item, | 106 'name': item, |
91 'link': self._MakeApiLink('method', item) | 107 'link': self._MakeApiLink('method', item) |
92 }) | 108 }) |
93 l10n_data = self._GetDataFromManifest(sample_path) | 109 l10n_data = self._GetDataFromManifest(sample_path, file_system) |
94 sample_base_path = sample_path.split('/', 1)[1] | 110 sample_base_path = sample_path.split('/', 1)[1] |
111 if is_apps: | |
112 l10n_data['github_path'] = GITHUB_BASE + '/' + sample_base_path | |
113 sample_base_path = RAW_GITHUB_BASE + '/' + sample_base_path | |
95 if l10n_data['icon'] is None: | 114 if l10n_data['icon'] is None: |
96 icon_path = self._static_path + DEFAULT_ICON_PATH | 115 icon_path = self._static_path + DEFAULT_ICON_PATH |
97 else: | 116 else: |
98 icon_path = sample_base_path + '/' + l10n_data['icon'] | 117 icon_path = sample_base_path + '/' + l10n_data['icon'] |
99 l10n_data.update({ | 118 l10n_data.update({ |
100 'icon': icon_path, | 119 'icon': icon_path, |
101 'path': sample_base_path, | 120 'path': sample_base_path, |
102 'files': [f.replace(sample_path + '/', '') for f in sample_files], | 121 'files': [f.replace(sample_path + '/', '') for f in sample_files], |
103 'api_calls': api_calls | 122 'api_calls': api_calls |
104 }) | 123 }) |
105 samples_list.append(l10n_data) | 124 samples_list.append(l10n_data) |
106 | 125 |
107 return samples_list | 126 return samples_list |
108 | 127 |
109 def __init__(self, cache, samples_path, request): | 128 def __init__(self, extensions_cache, apps_cache, samples_path, request): |
110 self._cache = cache | 129 self._extensions_cache = extensions_cache |
130 self._apps_cache = apps_cache | |
111 self._samples_path = samples_path | 131 self._samples_path = samples_path |
112 self._request = request | 132 self._request = request |
113 | 133 |
114 def _GetAcceptedLanguages(self): | 134 def _GetAcceptedLanguages(self): |
115 accept_language = self._request.headers.get('Accept-Language', None) | 135 accept_language = self._request.headers.get('Accept-Language', None) |
116 if accept_language is None: | 136 if accept_language is None: |
117 return [] | 137 return [] |
118 return [lang_with_q.split(';')[0].strip() | 138 return [lang_with_q.split(';')[0].strip() |
119 for lang_with_q in accept_language.split(',')] | 139 for lang_with_q in accept_language.split(',')] |
120 | 140 |
121 def __getitem__(self, key): | 141 def __getitem__(self, key): |
122 return self.get(key) | 142 return self.get(key) |
123 | 143 |
124 def get(self, key): | 144 def get(self, key): |
125 samples_list = self._cache.GetFromFileListing(self._samples_path + '/') | 145 if key == 'apps': |
146 samples_list = self._apps_cache.GetFromFileListing('/') | |
147 else: | |
148 samples_list = self._extensions_cache.GetFromFileListing( | |
149 self._samples_path + '/') | |
126 return_list = [] | 150 return_list = [] |
127 for dict_ in samples_list: | 151 for dict_ in samples_list: |
128 name = dict_['name'] | 152 name = dict_['name'] |
129 description = dict_['description'] | 153 description = dict_['description'] |
130 if name.startswith('__MSG_') or description.startswith('__MSG_'): | 154 if name.startswith('__MSG_') or description.startswith('__MSG_'): |
131 try: | 155 try: |
132 # Copy the sample dict so we don't change the dict in the cache. | 156 # Copy the sample dict so we don't change the dict in the cache. |
133 sample_data = dict_.copy() | 157 sample_data = dict_.copy() |
134 name_key = name[len('__MSG_'):-len('__')] | 158 name_key = name[len('__MSG_'):-len('__')] |
135 description_key = description[len('__MSG_'):-len('__')] | 159 description_key = description[len('__MSG_'):-len('__')] |
136 locale = sample_data['default_locale'] | 160 locale = sample_data['default_locale'] |
137 for lang in self._GetAcceptedLanguages(): | 161 for lang in self._GetAcceptedLanguages(): |
138 if lang in sample_data['locales']: | 162 if lang in sample_data['locales']: |
139 locale = lang | 163 locale = lang |
140 break | 164 break |
141 locale_data = sample_data['locales'][locale] | 165 locale_data = sample_data['locales'][locale] |
142 sample_data['name'] = locale_data[name_key]['message'] | 166 sample_data['name'] = locale_data[name_key]['message'] |
143 sample_data['description'] = locale_data[description_key]['message'] | 167 sample_data['description'] = locale_data[description_key]['message'] |
144 except Exception as e: | 168 except Exception as e: |
145 logging.error(e) | 169 logging.error(e) |
146 # Revert the sample to the original dict. | 170 # Revert the sample to the original dict. |
147 sample_data = dict_ | 171 sample_data = dict_ |
148 return_list.append(sample_data) | 172 return_list.append(sample_data) |
149 else: | 173 else: |
150 return_list.append(dict_) | 174 return_list.append(dict_) |
151 return return_list | 175 return return_list |
OLD | NEW |