| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 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, |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
| 14 # | 14 # |
| 15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
| 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 17 | 17 |
| 18 import sys, os, subprocess, zipfile, json, urlparse, codecs | 18 import codecs |
| 19 import ConfigParser | |
| 20 import json | |
| 21 import os | |
| 19 from StringIO import StringIO | 22 from StringIO import StringIO |
| 20 from ConfigParser import SafeConfigParser | 23 import subprocess |
|
Sebastian Noack
2015/03/20 15:50:13
json, subprocess and zipfile seem to be unused. So
Wladimir Palant
2015/03/20 16:04:21
I actually verified that all of them are used. Did
Sebastian Noack
2015/03/20 16:11:36
Oh yes, I did.
| |
| 24 import sys | |
| 25 import urlparse | |
| 26 import zipfile | |
| 21 | 27 |
| 22 class Source: | 28 class Source: |
| 23 def resolve_link(self, url, locale): | 29 def resolve_link(self, url, locale): |
| 24 parsed = urlparse.urlparse(url) | 30 parsed = urlparse.urlparse(url) |
| 25 page = parsed.path | 31 page = parsed.path |
| 26 if parsed.scheme != "" or page.startswith("/") or page.startswith("."): | 32 if parsed.scheme != "" or page.startswith("/") or page.startswith("."): |
| 27 # Not a page link | 33 # Not a page link |
| 28 return None, None | 34 return None, None |
| 29 | 35 |
| 30 if parsed.path == "" and url != "": | 36 if page == "" and url != "": |
| 31 # Page-relative link | 37 # Page-relative link |
| 32 return None, None | 38 return None, None |
| 33 | 39 |
| 40 def has_locale(locale, page): | |
| 41 try: | |
| 42 page = config.get("locale_overrides", page) | |
| 43 except ConfigParser.Error: | |
| 44 pass | |
| 45 return self.has_locale(locale, page) | |
| 46 | |
| 34 config = self.read_config() | 47 config = self.read_config() |
| 35 default_locale = config.get("general", "defaultlocale") | 48 default_locale = config.get("general", "defaultlocale") |
| 36 default_page = config.get("general", "defaultpage") | 49 default_page = config.get("general", "defaultpage") |
| 50 alternative_page = "/".join([page, default_page]).lstrip("/") | |
| 37 | 51 |
| 38 checked_page = page | 52 if self.has_localizable_file(default_locale, page): |
| 39 if config.has_option("locale_overrides", page): | 53 if not self.has_localizable_file(locale, page): |
| 40 checked_page = config.get("locale_overrides", page) | |
| 41 | |
| 42 if self.has_localizable_file(default_locale, checked_page): | |
| 43 if not self.has_localizable_file(locale, checked_page): | |
| 44 locale = default_locale | 54 locale = default_locale |
| 45 elif self.has_page(checked_page): | 55 elif self.has_page(page): |
| 46 if not self.has_locale(locale, checked_page): | 56 if not has_locale(locale, page): |
| 57 locale = default_locale | |
| 58 elif self.has_page(alternative_page): | |
| 59 if not has_locale(locale, alternative_page): | |
| 47 locale = default_locale | 60 locale = default_locale |
| 48 else: | 61 else: |
| 49 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page | 62 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page |
| 50 | 63 |
| 51 if page == default_page: | 64 parts = page.split("/") |
| 52 page = "" | 65 if parts[-1] == default_page: |
| 66 page = "/".join(parts[:-1]) | |
| 53 | 67 |
| 54 path = "/%s/%s" % (locale, page) | 68 path = "/%s/%s" % (locale, page) |
| 55 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) | 69 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) |
| 56 | 70 |
| 57 def read_config(self): | 71 def read_config(self): |
| 58 configdata = self.read_file("settings.ini") | 72 configdata = self.read_file("settings.ini") |
| 59 config = SafeConfigParser() | 73 config = ConfigParser.SafeConfigParser() |
| 60 config.readfp(StringIO(configdata)) | 74 config.readfp(StringIO(configdata)) |
| 61 return config | 75 return config |
| 62 | 76 |
| 63 # | 77 # |
| 64 # Page helpers | 78 # Page helpers |
| 65 # | 79 # |
| 66 | 80 |
| 67 @staticmethod | 81 @staticmethod |
| 68 def page_filename(page, format): | 82 def page_filename(page, format): |
| 69 return "pages/%s.%s" % (page, format) | 83 return "pages/%s.%s" % (page, format) |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 return | 273 return |
| 260 | 274 |
| 261 for filename in files: | 275 for filename in files: |
| 262 path = os.path.join(dir, filename) | 276 path = os.path.join(dir, filename) |
| 263 if os.path.isfile(path): | 277 if os.path.isfile(path): |
| 264 result.append(relpath + filename) | 278 result.append(relpath + filename) |
| 265 elif os.path.isdir(path): | 279 elif os.path.isdir(path): |
| 266 do_list(path, relpath + filename + "/") | 280 do_list(path, relpath + filename + "/") |
| 267 do_list(self.get_path(subdir), "") | 281 do_list(self.get_path(subdir), "") |
| 268 return result | 282 return result |
| OLD | NEW |