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 import url_constants |
| 11 |
9 DEFAULT_ICON_PATH = '/images/sample-default-icon.png' | 12 DEFAULT_ICON_PATH = '/images/sample-default-icon.png' |
10 | 13 |
11 class SamplesDataSource(object): | 14 class SamplesDataSource(object): |
12 """Constructs a list of samples and their respective files and api calls. | 15 """Constructs a list of samples and their respective files and api calls. |
13 """ | 16 """ |
14 | 17 |
15 class Factory(object): | 18 class Factory(object): |
16 """A factory to create SamplesDataSource instances bound to individual | 19 """A factory to create SamplesDataSource instances bound to individual |
17 Requests. | 20 Requests. |
18 """ | 21 """ |
19 def __init__(self, branch, file_system, cache_builder, samples_path): | 22 def __init__(self, |
| 23 branch, |
| 24 file_system, |
| 25 github_file_system, |
| 26 cache_builder, |
| 27 github_cache_builder, |
| 28 samples_path): |
| 29 self._file_system = file_system |
| 30 self._github_file_system = github_file_system |
20 self._static_path = ((('/' + branch) if branch != 'local' else '') + | 31 self._static_path = ((('/' + branch) if branch != 'local' else '') + |
21 '/static') | 32 '/static') |
22 self._file_system = file_system | 33 self._extensions_cache = cache_builder.build(self._MakeSamplesList) |
23 self._cache = cache_builder.build(self._MakeSamplesList) | 34 self._apps_cache = github_cache_builder.build( |
| 35 lambda x: self._MakeSamplesList(x, is_apps=True)) |
24 self._samples_path = samples_path | 36 self._samples_path = samples_path |
25 | 37 |
26 def Create(self, request): | 38 def Create(self, request): |
27 """Returns a new SamplesDataSource bound to |request|. | 39 """Returns a new SamplesDataSource bound to |request|. |
28 """ | 40 """ |
29 return SamplesDataSource(self._cache, | 41 return SamplesDataSource(self._extensions_cache, |
| 42 self._apps_cache, |
30 self._samples_path, | 43 self._samples_path, |
31 request) | 44 request) |
32 | 45 |
33 def _GetApiItems(self, js_file): | 46 def _GetApiItems(self, js_file): |
34 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) | 47 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) |
35 | 48 |
36 def _MakeApiLink(self, prefix, item): | 49 def _MakeApiLink(self, prefix, item): |
37 api, name = item.replace('chrome.', '').split('.', 1) | 50 api, name = item.replace('chrome.', '').split('.', 1) |
38 return api + '.html#' + prefix + '-' + name | 51 return api + '.html#' + prefix + '-' + name |
39 | 52 |
40 def _GetDataFromManifest(self, path): | 53 def _GetDataFromManifest(self, path, file_system): |
41 manifest = self._file_system.ReadSingle(path + '/manifest.json') | 54 manifest = file_system.ReadSingle(path + '/manifest.json') |
42 manifest_json = json.loads(manifest) | 55 manifest_json = json.loads(json_comment_eater.Nom(manifest)) |
43 l10n_data = { | 56 l10n_data = { |
44 'name': manifest_json.get('name', ''), | 57 'name': manifest_json.get('name', ''), |
45 'description': manifest_json.get('description', ''), | 58 'description': manifest_json.get('description', ''), |
46 'icon': manifest_json.get('icons', {}).get('128', None), | 59 'icon': manifest_json.get('icons', {}).get('128', None), |
47 'default_locale': manifest_json.get('default_locale', None), | 60 'default_locale': manifest_json.get('default_locale', None), |
48 'locales': {} | 61 'locales': {} |
49 } | 62 } |
50 if not l10n_data['default_locale']: | 63 if not l10n_data['default_locale']: |
51 return l10n_data | 64 return l10n_data |
52 locales_path = path + '/_locales/' | 65 locales_path = path + '/_locales/' |
53 locales_dir = self._file_system.ReadSingle(locales_path) | 66 locales_dir = file_system.ReadSingle(locales_path) |
54 if locales_dir: | 67 if locales_dir: |
55 locales_files = self._file_system.Read( | 68 locales_files = file_system.Read( |
56 [locales_path + f + 'messages.json' for f in locales_dir]).Get() | 69 [locales_path + f + 'messages.json' for f in locales_dir]).Get() |
57 locales_json = [(path, json.loads(contents)) | 70 locales_json = [(path, json.loads(contents)) |
58 for path, contents in locales_files.iteritems()] | 71 for path, contents in locales_files.iteritems()] |
59 for path, json_ in locales_json: | 72 for path, json_ in locales_json: |
60 l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_ | 73 l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_ |
61 return l10n_data | 74 return l10n_data |
62 | 75 |
63 def _MakeSamplesList(self, files): | 76 def _MakeSamplesList(self, files, is_apps=False): |
| 77 file_system = self._github_file_system if is_apps else self._file_system |
64 samples_list = [] | 78 samples_list = [] |
65 for filename in sorted(files): | 79 for filename in sorted(files): |
66 if filename.rsplit('/')[-1] != 'manifest.json': | 80 if filename.rsplit('/')[-1] != 'manifest.json': |
67 continue | 81 continue |
68 # This is a little hacky, but it makes a sample page. | 82 # This is a little hacky, but it makes a sample page. |
69 sample_path = filename.rsplit('/', 1)[-2] | 83 sample_path = filename.rsplit('/', 1)[-2] |
70 sample_files = [path for path in files | 84 sample_files = [path for path in files |
71 if path.startswith(sample_path + '/')] | 85 if path.startswith(sample_path + '/')] |
72 js_files = [path for path in sample_files if path.endswith('.js')] | 86 js_files = [path for path in sample_files if path.endswith('.js')] |
73 js_contents = self._file_system.Read(js_files).Get() | 87 js_contents = file_system.Read(js_files).Get() |
74 api_items = set() | 88 api_items = set() |
75 for js in js_contents.values(): | 89 for js in js_contents.values(): |
76 api_items.update(self._GetApiItems(js)) | 90 api_items.update(self._GetApiItems(js)) |
77 | 91 |
78 api_calls = [] | 92 api_calls = [] |
79 for item in api_items: | 93 for item in api_items: |
80 if len(item.split('.')) < 3: | 94 if len(item.split('.')) < 3: |
81 continue | 95 continue |
82 if item.endswith('.addListener'): | 96 if item.endswith('.addListener'): |
83 item = item.replace('.addListener', '') | 97 item = item.replace('.addListener', '') |
84 api_calls.append({ | 98 api_calls.append({ |
85 'name': item, | 99 'name': item, |
86 'link': self._MakeApiLink('event', item) | 100 'link': self._MakeApiLink('event', item) |
87 }) | 101 }) |
88 else: | 102 else: |
89 api_calls.append({ | 103 api_calls.append({ |
90 'name': item, | 104 'name': item, |
91 'link': self._MakeApiLink('method', item) | 105 'link': self._MakeApiLink('method', item) |
92 }) | 106 }) |
93 l10n_data = self._GetDataFromManifest(sample_path) | 107 l10n_data = self._GetDataFromManifest(sample_path, file_system) |
94 sample_base_path = sample_path.split('/', 1)[1] | 108 sample_base_path = sample_path.split('/', 1)[1] |
| 109 if is_apps: |
| 110 l10n_data['github_path'] = (url_constants.GITHUB_BASE + '/' + |
| 111 sample_base_path) |
| 112 sample_base_path = (url_constants.RAW_GITHUB_BASE + '/' + |
| 113 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 |