LEFT | RIGHT |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2013 Eyeo GmbH | 4 # Copyright (C) 2006-2013 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
(...skipping 13 matching lines...) Expand all Loading... |
24 parsed = urlparse.urlparse(url) | 24 parsed = urlparse.urlparse(url) |
25 page = parsed.path | 25 page = parsed.path |
26 if parsed.scheme != "" or page.startswith("/") or page.startswith("."): | 26 if parsed.scheme != "" or page.startswith("/") or page.startswith("."): |
27 # Not a page link | 27 # Not a page link |
28 return None, None | 28 return None, None |
29 | 29 |
30 config = self.read_config() | 30 config = self.read_config() |
31 default_locale = config.get("general", "defaultlocale") | 31 default_locale = config.get("general", "defaultlocale") |
32 default_page = config.get("general", "defaultpage") | 32 default_page = config.get("general", "defaultpage") |
33 | 33 |
34 if self.has_localizable_file(default_locale, page): | 34 checked_page = page |
35 if not self.has_localizable_file(locale, page): | 35 if config.has_option("locale_overrides", page): |
| 36 checked_page = config.get("locale_overrides", page) |
| 37 |
| 38 if self.has_localizable_file(default_locale, checked_page): |
| 39 if not self.has_localizable_file(locale, checked_page): |
36 locale = default_locale | 40 locale = default_locale |
37 elif self.has_locale(default_locale, page): | 41 elif self.has_locale(default_locale, checked_page): |
38 if not self.has_locale(locale, page): | 42 if not self.has_locale(locale, checked_page): |
39 locale = default_locale | 43 locale = default_locale |
40 else: | 44 else: |
41 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page | 45 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page |
42 | 46 |
43 if page == default_page: | 47 if page == default_page: |
44 page = "" | 48 page = "" |
45 | 49 |
46 path = "/%s/%s" % (locale, page) | 50 path = "/%s/%s" % (locale, page) |
47 return locale, urlparse.urlunparse(list(parsed[0:2]) + [path] + list(parsed[
3:])) | 51 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) |
48 | 52 |
49 def read_config(self): | 53 def read_config(self): |
50 configdata = self.read_file("settings.ini") | 54 configdata = self.read_file("settings.ini") |
51 config = SafeConfigParser() | 55 config = SafeConfigParser() |
52 config.readfp(StringIO(configdata)) | 56 config.readfp(StringIO(configdata)) |
53 return config | 57 return config |
54 | 58 |
55 # | 59 # |
56 # Page helpers | 60 # Page helpers |
57 # | 61 # |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 def has_static(self, filename): | 111 def has_static(self, filename): |
108 return self.has_file(self.static_filename(filename)) | 112 return self.has_file(self.static_filename(filename)) |
109 | 113 |
110 def read_static(self, filename): | 114 def read_static(self, filename): |
111 return self.read_file(self.static_filename(filename), binary=True) | 115 return self.read_file(self.static_filename(filename), binary=True) |
112 | 116 |
113 # | 117 # |
114 # Locale helpers | 118 # Locale helpers |
115 # | 119 # |
116 | 120 |
117 @staticmethod | 121 @classmethod |
118 def locale_filename(locale, page): | 122 def locale_filename(cls, locale, page): |
119 return "locales/%s/%s.json" % (locale, page) | 123 return cls.localizable_file_filename(locale, page + ".json") |
120 | 124 |
121 def list_locales(self): | 125 def list_locales(self): |
122 result = set() | 126 result = set() |
123 for filename in self.list_files("locales"): | 127 for filename in self.list_files("locales"): |
124 if "/" in filename: | 128 if "/" in filename: |
125 locale, path = filename.split("/", 1) | 129 locale, path = filename.split("/", 1) |
126 result.add(locale) | 130 result.add(locale) |
127 return result | 131 return result |
128 | 132 |
129 def has_locale(self, locale, page): | 133 def has_locale(self, locale, page): |
| 134 config = self.read_config() |
| 135 if config.has_option("locale_overrides", page): |
| 136 page = config.get("locale_overrides", page) |
130 return self.has_file(self.locale_filename(locale, page)) | 137 return self.has_file(self.locale_filename(locale, page)) |
131 | 138 |
132 def read_locale(self, locale, page): | 139 def read_locale(self, locale, page): |
133 default_locale = self.read_config().get("general", "defaultlocale") | 140 default_locale = self.read_config().get("general", "defaultlocale") |
134 if locale == default_locale: | 141 if locale == default_locale: |
135 result = {} | 142 result = {} |
136 else: | 143 else: |
137 result = self.read_locale(default_locale, page) | 144 result = self.read_locale(default_locale, page) |
138 | 145 |
139 if self.has_locale(locale, page): | 146 if self.has_locale(locale, page): |
140 filedata = self.read_file(self.locale_filename(locale, page)) | 147 filedata = self.read_file(self.locale_filename(locale, page)) |
141 localedata = json.loads(filedata) | 148 localedata = json.loads(filedata) |
142 for key, value in localedata.iteritems(): | 149 for key, value in localedata.iteritems(): |
143 result[key] = value["message"] | 150 result[key] = value["message"] |
144 | 151 |
145 return result | 152 return result |
146 | 153 |
147 # | 154 # |
148 # Template helpers | 155 # Template helpers |
149 # | 156 # |
150 | 157 |
151 @staticmethod | 158 @staticmethod |
152 def template_filename(template): | 159 def template_filename(template): |
153 return "templates/%s.tmpl" % template | 160 return "templates/%s.tmpl" % template |
154 | 161 |
155 def read_template(self, template): | 162 def read_template(self, template): |
156 return self.read_file(self.template_filename(template)) | 163 return self.read_file(self.template_filename(template)) |
157 | 164 |
| 165 # |
| 166 # Include helpers |
| 167 # |
| 168 |
| 169 @staticmethod |
| 170 def include_filename(include, format): |
| 171 return "includes/%s.%s" % (include, format) |
| 172 |
| 173 def has_include(self, include, format): |
| 174 return self.has_file(self.include_filename(include, format)) |
| 175 |
| 176 def read_include(self, include, format): |
| 177 return self.read_file(self.include_filename(include, format)) |
| 178 |
158 class MercurialSource(Source): | 179 class MercurialSource(Source): |
159 def __init__(self, repo): | 180 def __init__(self, repo): |
160 command = ["hg", "-R", repo, "archive", "-r", "default", | 181 command = ["hg", "-R", repo, "archive", "-r", "default", |
161 "-t", "uzip", "-p", ".", "-"] | 182 "-t", "uzip", "-p", ".", "-"] |
162 data, _ = subprocess.Popen(command, stdout=subprocess.PIPE).communicate() | 183 data = subprocess.check_output(command) |
163 self._archive = zipfile.ZipFile(StringIO(data), mode="r") | 184 self._archive = zipfile.ZipFile(StringIO(data), mode="r") |
164 | 185 |
165 command = ["hg", "-R", repo, "id", "-n", "-r", "default"] | 186 command = ["hg", "-R", repo, "id", "-n", "-r", "default"] |
166 self.version, _ = subprocess.Popen(command, stdout=subprocess.PIPE).communic
ate() | 187 self.version = subprocess.check_output(command).strip() |
167 self.version = self.version.strip() | |
168 | 188 |
169 def __enter__(self): | 189 def __enter__(self): |
170 return self | 190 return self |
171 | 191 |
172 def __exit__(self, type, value, traceback): | 192 def __exit__(self, type, value, traceback): |
173 self.close() | 193 self.close() |
174 return False | 194 return False |
175 | 195 |
176 def close(self): | 196 def close(self): |
177 self._archive.close() | 197 self._archive.close() |
178 | 198 |
179 def has_file(self, filename): | 199 def has_file(self, filename): |
180 try: | 200 try: |
181 self._archive.getinfo("./%s" % filename) | 201 self._archive.getinfo("./%s" % filename) |
182 except KeyError: | 202 except KeyError: |
183 return False | 203 return False |
184 return True | 204 return True |
185 | 205 |
186 def read_file(self, filename, binary=False): | 206 def read_file(self, filename, binary=False): |
187 result = self._archive.read("./%s" % filename) | 207 result = self._archive.read("./%s" % filename) |
188 if not binary: | 208 if not binary: |
189 result = result.decode("utf-8") | 209 result = result.decode("utf-8") |
190 return result | 210 return result |
191 | 211 |
192 def list_files(self, subdir): | 212 def list_files(self, subdir): |
193 prefix = "./" + subdir + "/" | 213 prefix = "./%s/" % subdir |
194 for filename in self._archive.namelist(): | 214 for filename in self._archive.namelist(): |
195 if filename.startswith(prefix): | 215 if filename.startswith(prefix): |
196 yield filename[len(prefix):] | 216 yield filename[len(prefix):] |
197 | 217 |
198 class FileSource(Source): | 218 class FileSource(Source): |
199 def __init__(self, dir): | 219 def __init__(self, dir): |
200 self._dir = dir | 220 self._dir = dir |
201 | 221 |
202 def __enter__(self): | 222 def __enter__(self): |
203 return self | 223 return self |
(...skipping 20 matching lines...) Expand all Loading... |
224 def do_list(dir, relpath): | 244 def do_list(dir, relpath): |
225 files = os.listdir(dir) | 245 files = os.listdir(dir) |
226 for filename in files: | 246 for filename in files: |
227 path = os.path.join(dir, filename) | 247 path = os.path.join(dir, filename) |
228 if os.path.isfile(path): | 248 if os.path.isfile(path): |
229 result.append(relpath + filename) | 249 result.append(relpath + filename) |
230 elif os.path.isdir(path): | 250 elif os.path.isdir(path): |
231 do_list(path, relpath + filename + "/") | 251 do_list(path, relpath + filename + "/") |
232 do_list(self.get_path(subdir), "") | 252 do_list(self.get_path(subdir), "") |
233 return result | 253 return result |
LEFT | RIGHT |