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 re | 6 import re |
7 | 7 |
8 class SamplesDataSource(object): | 8 class SamplesDataSource(object): |
not at google - send to devlin
2012/07/20 01:28:21
See comment in template_data_source... this needs
cduvall
2012/07/20 19:40:50
Done.
| |
9 """Constructs a list of samples and their respective files and api calls. | 9 """Constructs a list of samples and their respective files and api calls. |
10 """ | 10 """ |
11 def __init__(self, fetcher, cache_builder, samples_path): | 11 def __init__(self, fetcher, cache_builder, samples_path): |
12 self._fetcher = fetcher | 12 self._fetcher = fetcher |
13 self._cache = cache_builder.build(self._MakeSamplesList) | 13 self._cache = cache_builder.build(self._MakeSamplesList) |
14 self._samples_path = samples_path | 14 self._samples_path = samples_path |
15 self._headers = {} | |
not at google - send to devlin
2012/07/20 01:28:21
let's just bind this to the request. Easier to und
cduvall
2012/07/20 19:40:50
Done.
| |
16 | |
17 def SetHeaders(self, headers): | |
18 self._headers = headers | |
15 | 19 |
16 def _GetApiItems(self, js_file): | 20 def _GetApiItems(self, js_file): |
17 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) | 21 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) |
18 | 22 |
19 def _MakeApiLink(self, prefix, item): | 23 def _MakeApiLink(self, prefix, item): |
20 api, name = item.replace('chrome.', '').split('.', 1) | 24 api, name = item.replace('chrome.', '').split('.', 1) |
21 return api + '.html#' + prefix + '-' + name | 25 return api + '.html#' + prefix + '-' + name |
22 | 26 |
27 def _FetchFile(self, filename): | |
28 return self._fetcher.Read([filename]).Get()[filename] | |
not at google - send to devlin
2012/07/20 01:28:21
Seen this pattern in a couple of places now.
Woul
cduvall
2012/07/20 19:40:50
Done.
| |
29 | |
23 def _GetDataFromManifest(self, path): | 30 def _GetDataFromManifest(self, path): |
24 manifest_path = path + '/manifest.json' | 31 manifest = self._FetchFile(path + '/manifest.json') |
25 manifest = self._fetcher.Read([manifest_path]).Get()[manifest_path] | |
26 manifest_json = json.loads(manifest) | 32 manifest_json = json.loads(manifest) |
27 return (manifest_json.get('name'), manifest_json.get('description')) | 33 name = manifest_json.get('name', '') |
34 description = manifest_json.get('description', '') | |
35 if name.startswith('__MSG_') or description.startswith('__MSG_'): | |
not at google - send to devlin
2012/07/20 01:28:21
Won't this cache only the language in the header?
cduvall
2012/07/20 19:40:50
Done.
| |
36 default_language = manifest_json['default_locale'] | |
37 if 'Accept-Language' in self._headers: | |
not at google - send to devlin
2012/07/20 01:28:21
for thsi kind of thing, would prefer to see someth
cduvall
2012/07/20 19:40:50
Done.
| |
38 languages = filter(lambda x: x != 'q', | |
39 re.findall('([a-zA-Z]+\-[a-zA-Z]+|[a-zA-Z]+)', | |
40 self._headers['Accept-Language'])) | |
41 languages = [l.replace('-', '_') for l in languages] | |
not at google - send to devlin
2012/07/20 01:28:21
l as a short variable is like the most confusing s
cduvall
2012/07/20 19:40:50
Very true, sorry about that.
| |
42 locales_path = path + '/_locales/' | |
43 locales = [l.strip('/') for l in self._FetchFile(locales_path)] | |
44 for language in languages: | |
45 if language in locales: | |
46 default_language = language | |
47 break | |
48 locale_json = json.loads(unicode( | |
49 self._FetchFile(locales_path + default_language + '/messages.json'), | |
50 'latin-1')) | |
51 if name.startswith('__MSG_'): | |
52 name = locale_json[name[len('__MSG_'):-2]]['message'] | |
53 if description.startswith('__MSG_'): | |
54 description = locale_json[description[len('__MSG_'):-2]]['message'] | |
55 | |
56 return (name, description) | |
28 | 57 |
29 def _MakeSamplesList(self, files): | 58 def _MakeSamplesList(self, files): |
30 samples_list = [] | 59 samples_list = [] |
31 for filename in sorted(files): | 60 for filename in sorted(files): |
32 if filename.rsplit('/')[-1] != 'manifest.json': | 61 if filename.rsplit('/')[-1] != 'manifest.json': |
33 continue | 62 continue |
34 # This is a little hacky, but it makes a sample page. | 63 # This is a little hacky, but it makes a sample page. |
35 sample_path = filename.rsplit('/', 1)[-2] | 64 sample_path = filename.rsplit('/', 1)[-2] |
36 sample_files = filter(lambda x: x.startswith(sample_path + '/'), files) | 65 sample_files = filter(lambda x: x.startswith(sample_path + '/'), files) |
37 js_files = filter(lambda x: x.endswith('.js'), sample_files) | 66 js_files = filter(lambda x: x.endswith('.js'), sample_files) |
(...skipping 25 matching lines...) Expand all Loading... | |
63 'files': [f.replace(sample_path + '/', '') for f in sample_files], | 92 'files': [f.replace(sample_path + '/', '') for f in sample_files], |
64 'api_calls': api_calls | 93 'api_calls': api_calls |
65 }) | 94 }) |
66 return samples_list | 95 return samples_list |
67 | 96 |
68 def __getitem__(self, key): | 97 def __getitem__(self, key): |
69 return self.get(key) | 98 return self.get(key) |
70 | 99 |
71 def get(self, key): | 100 def get(self, key): |
72 return self._cache.GetFromFileListing(self._samples_path + '/') | 101 return self._cache.GetFromFileListing(self._samples_path + '/') |
OLD | NEW |