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 DEFAULT_ICON_PATH = '/images/sample-default-icon.png' | 9 DEFAULT_ICON_PATH = '/images/sample-default-icon.png' |
10 | 10 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 }) | 104 }) |
105 samples_list.append(l10n_data) | 105 samples_list.append(l10n_data) |
106 | 106 |
107 return samples_list | 107 return samples_list |
108 | 108 |
109 def __init__(self, cache, samples_path, request): | 109 def __init__(self, cache, samples_path, request): |
110 self._cache = cache | 110 self._cache = cache |
111 self._samples_path = samples_path | 111 self._samples_path = samples_path |
112 self._request = request | 112 self._request = request |
113 | 113 |
| 114 def GetSamplesForAPI(self, api_name): |
| 115 samples = self.values() |
| 116 api_search = '.' + api_name + '.' |
| 117 return [sample for sample in samples |
| 118 if any(api_search in api['name'] for api in sample['api_calls'])] |
| 119 |
114 def _GetAcceptedLanguages(self): | 120 def _GetAcceptedLanguages(self): |
115 accept_language = self._request.headers.get('Accept-Language', None) | 121 accept_language = self._request.headers.get('Accept-Language', None) |
116 if accept_language is None: | 122 if accept_language is None: |
117 return [] | 123 return [] |
118 return [lang_with_q.split(';')[0].strip() | 124 return [lang_with_q.split(';')[0].strip() |
119 for lang_with_q in accept_language.split(',')] | 125 for lang_with_q in accept_language.split(',')] |
120 | 126 |
121 def __getitem__(self, key): | 127 def __getitem__(self, key): |
122 return self.get(key) | 128 return self.get(key) |
123 | 129 |
| 130 def values(self): |
| 131 return self.get('') |
| 132 |
124 def get(self, key): | 133 def get(self, key): |
125 samples_list = self._cache.GetFromFileListing(self._samples_path + '/') | 134 samples_list = self._cache.GetFromFileListing(self._samples_path + '/') |
126 return_list = [] | 135 return_list = [] |
127 for dict_ in samples_list: | 136 for dict_ in samples_list: |
128 name = dict_['name'] | 137 name = dict_['name'] |
129 description = dict_['description'] | 138 description = dict_['description'] |
130 if name.startswith('__MSG_') or description.startswith('__MSG_'): | 139 if name.startswith('__MSG_') or description.startswith('__MSG_'): |
131 try: | 140 try: |
132 # Copy the sample dict so we don't change the dict in the cache. | 141 # Copy the sample dict so we don't change the dict in the cache. |
133 sample_data = dict_.copy() | 142 sample_data = dict_.copy() |
134 name_key = name[len('__MSG_'):-len('__')] | 143 name_key = name[len('__MSG_'):-len('__')] |
135 description_key = description[len('__MSG_'):-len('__')] | 144 description_key = description[len('__MSG_'):-len('__')] |
136 locale = sample_data['default_locale'] | 145 locale = sample_data['default_locale'] |
137 for lang in self._GetAcceptedLanguages(): | 146 for lang in self._GetAcceptedLanguages(): |
138 if lang in sample_data['locales']: | 147 if lang in sample_data['locales']: |
139 locale = lang | 148 locale = lang |
140 break | 149 break |
141 locale_data = sample_data['locales'][locale] | 150 locale_data = sample_data['locales'][locale] |
142 sample_data['name'] = locale_data[name_key]['message'] | 151 sample_data['name'] = locale_data[name_key]['message'] |
143 sample_data['description'] = locale_data[description_key]['message'] | 152 sample_data['description'] = locale_data[description_key]['message'] |
144 except Exception as e: | 153 except Exception as e: |
145 logging.error(e) | 154 logging.error(e) |
146 # Revert the sample to the original dict. | 155 # Revert the sample to the original dict. |
147 sample_data = dict_ | 156 sample_data = dict_ |
148 return_list.append(sample_data) | 157 return_list.append(sample_data) |
149 else: | 158 else: |
150 return_list.append(dict_) | 159 return_list.append(dict_) |
151 return return_list | 160 return return_list |
OLD | NEW |