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) |
not at google - send to devlin
2012/08/07 01:35:38
l10n_data is getting to be a confusing variable na
cduvall
2012/08/08 19:27:25
Done.
| |
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 GetSamplesForAPI(self, api_name): | 134 def GetSamplesForAPI(self, api_name): |
115 samples = self.values() | 135 samples = self.values() |
116 api_search = '.' + api_name + '.' | 136 api_search = '.' + api_name + '.' |
117 return [sample for sample in samples | 137 return [sample for sample in samples |
118 if any(api_search in api['name'] for api in sample['api_calls'])] | 138 if any(api_search in api['name'] for api in sample['api_calls'])] |
119 | 139 |
120 def _GetAcceptedLanguages(self): | 140 def _GetAcceptedLanguages(self): |
121 accept_language = self._request.headers.get('Accept-Language', None) | 141 accept_language = self._request.headers.get('Accept-Language', None) |
122 if accept_language is None: | 142 if accept_language is None: |
123 return [] | 143 return [] |
124 return [lang_with_q.split(';')[0].strip() | 144 return [lang_with_q.split(';')[0].strip() |
125 for lang_with_q in accept_language.split(',')] | 145 for lang_with_q in accept_language.split(',')] |
126 | 146 |
127 def __getitem__(self, key): | 147 def __getitem__(self, key): |
128 return self.get(key) | 148 return self.get(key) |
129 | 149 |
130 def values(self): | 150 def values(self): |
131 return self.get('') | 151 return self.get('') |
132 | 152 |
133 def get(self, key): | 153 def get(self, key): |
134 samples_list = self._cache.GetFromFileListing(self._samples_path + '/') | 154 if key == 'apps': |
155 samples_list = self._apps_cache.GetFromFileListing('/') | |
156 else: | |
157 samples_list = self._extensions_cache.GetFromFileListing( | |
158 self._samples_path + '/') | |
not at google - send to devlin
2012/08/07 01:35:38
I think we should do the same kind of thing you di
cduvall
2012/08/08 19:27:25
Done.
| |
135 return_list = [] | 159 return_list = [] |
136 for dict_ in samples_list: | 160 for dict_ in samples_list: |
137 name = dict_['name'] | 161 name = dict_['name'] |
138 description = dict_['description'] | 162 description = dict_['description'] |
139 if name.startswith('__MSG_') or description.startswith('__MSG_'): | 163 if name.startswith('__MSG_') or description.startswith('__MSG_'): |
140 try: | 164 try: |
141 # Copy the sample dict so we don't change the dict in the cache. | 165 # Copy the sample dict so we don't change the dict in the cache. |
142 sample_data = dict_.copy() | 166 sample_data = dict_.copy() |
143 name_key = name[len('__MSG_'):-len('__')] | 167 name_key = name[len('__MSG_'):-len('__')] |
144 description_key = description[len('__MSG_'):-len('__')] | 168 description_key = description[len('__MSG_'):-len('__')] |
145 locale = sample_data['default_locale'] | 169 locale = sample_data['default_locale'] |
146 for lang in self._GetAcceptedLanguages(): | 170 for lang in self._GetAcceptedLanguages(): |
147 if lang in sample_data['locales']: | 171 if lang in sample_data['locales']: |
148 locale = lang | 172 locale = lang |
149 break | 173 break |
150 locale_data = sample_data['locales'][locale] | 174 locale_data = sample_data['locales'][locale] |
151 sample_data['name'] = locale_data[name_key]['message'] | 175 sample_data['name'] = locale_data[name_key]['message'] |
152 sample_data['description'] = locale_data[description_key]['message'] | 176 sample_data['description'] = locale_data[description_key]['message'] |
153 except Exception as e: | 177 except Exception as e: |
154 logging.error(e) | 178 logging.error(e) |
155 # Revert the sample to the original dict. | 179 # Revert the sample to the original dict. |
156 sample_data = dict_ | 180 sample_data = dict_ |
157 return_list.append(sample_data) | 181 return_list.append(sample_data) |
158 else: | 182 else: |
159 return_list.append(dict_) | 183 return_list.append(dict_) |
160 return return_list | 184 return return_list |
OLD | NEW |