| 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-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, |
| 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 sys, os, subprocess, zipfile, json, urlparse, codecs |
| 19 from StringIO import StringIO | 19 from StringIO import StringIO |
| 20 from ConfigParser import SafeConfigParser | 20 from ConfigParser import SafeConfigParser |
| 21 | 21 |
| 22 class Source: | 22 class Source: |
| 23 def resolve_link(self, url, locale): | 23 def resolve_link(self, url, locale): |
| 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 if parsed.path == "" and url != "": |
| 31 # Page-relative link |
| 32 return None, None |
| 33 |
| 30 config = self.read_config() | 34 config = self.read_config() |
| 31 default_locale = config.get("general", "defaultlocale") | 35 default_locale = config.get("general", "defaultlocale") |
| 32 default_page = config.get("general", "defaultpage") | 36 default_page = config.get("general", "defaultpage") |
| 33 | 37 |
| 34 checked_page = page | 38 checked_page = page |
| 35 if config.has_option("locale_overrides", page): | 39 if config.has_option("locale_overrides", page): |
| 36 checked_page = config.get("locale_overrides", page) | 40 checked_page = config.get("locale_overrides", page) |
| 37 | 41 |
| 38 if self.has_localizable_file(default_locale, checked_page): | 42 if self.has_localizable_file(default_locale, checked_page): |
| 39 if not self.has_localizable_file(locale, checked_page): | 43 if not self.has_localizable_file(locale, checked_page): |
| (...skipping 20 matching lines...) Expand all Loading... |
| 60 # Page helpers | 64 # Page helpers |
| 61 # | 65 # |
| 62 | 66 |
| 63 @staticmethod | 67 @staticmethod |
| 64 def page_filename(page, format): | 68 def page_filename(page, format): |
| 65 return "pages/%s.%s" % (page, format) | 69 return "pages/%s.%s" % (page, format) |
| 66 | 70 |
| 67 def list_pages(self): | 71 def list_pages(self): |
| 68 for filename in self.list_files("pages"): | 72 for filename in self.list_files("pages"): |
| 69 root, ext = os.path.splitext(filename) | 73 root, ext = os.path.splitext(filename) |
| 70 format = ext[1:] | 74 format = ext[1:].lower() |
| 71 yield root, format | 75 yield root, format |
| 72 | 76 |
| 73 def has_page(self, page, format): | 77 def has_page(self, page, format): |
| 74 return self.has_file(self.page_filename(page, format)) | 78 return self.has_file(self.page_filename(page, format)) |
| 75 | 79 |
| 76 def read_page(self, page, format): | 80 def read_page(self, page, format): |
| 77 return self.read_file(self.page_filename(page, format)) | 81 return self.read_file(self.page_filename(page, format)) |
| 78 | 82 |
| 79 # | 83 # |
| 80 # Localizable files helpers | 84 # Localizable files helpers |
| 81 # | 85 # |
| 82 | 86 |
| 83 @staticmethod | 87 @staticmethod |
| 84 def localizable_file_filename(locale, filename): | 88 def localizable_file_filename(locale, filename): |
| 85 return "locales/%s/%s" % (locale, filename) | 89 return "locales/%s/%s" % (locale, filename) |
| 86 | 90 |
| 87 def list_localizable_files(self): | 91 def list_localizable_files(self): |
| 88 default_locale = self.read_config().get("general", "defaultlocale") | 92 default_locale = self.read_config().get("general", "defaultlocale") |
| 89 return filter( | 93 return filter( |
| 90 lambda f: os.path.splitext(f)[1] != ".json", | 94 lambda f: os.path.splitext(f)[1].lower() != ".json", |
| 91 self.list_files("locales/%s" % default_locale) | 95 self.list_files("locales/%s" % default_locale) |
| 92 ) | 96 ) |
| 93 | 97 |
| 94 def has_localizable_file(self, locale, filename): | 98 def has_localizable_file(self, locale, filename): |
| 95 return self.has_file(self.localizable_file_filename(locale, filename)) | 99 return self.has_file(self.localizable_file_filename(locale, filename)) |
| 96 | 100 |
| 97 def read_localizable_file(self, locale, filename): | 101 def read_localizable_file(self, locale, filename): |
| 98 return self.read_file(self.localizable_file_filename(locale, filename), bina
ry=True) | 102 return self.read_file(self.localizable_file_filename(locale, filename), bina
ry=True) |
| 99 | 103 |
| 100 # | 104 # |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 return os.path.isfile(self.get_path(filename)) | 239 return os.path.isfile(self.get_path(filename)) |
| 236 | 240 |
| 237 def read_file(self, filename, binary=False): | 241 def read_file(self, filename, binary=False): |
| 238 encoding = None if binary else "utf-8" | 242 encoding = None if binary else "utf-8" |
| 239 with codecs.open(self.get_path(filename), "rb", encoding=encoding) as handle
: | 243 with codecs.open(self.get_path(filename), "rb", encoding=encoding) as handle
: |
| 240 return handle.read() | 244 return handle.read() |
| 241 | 245 |
| 242 def list_files(self, subdir): | 246 def list_files(self, subdir): |
| 243 result = [] | 247 result = [] |
| 244 def do_list(dir, relpath): | 248 def do_list(dir, relpath): |
| 245 files = os.listdir(dir) | 249 try: |
| 250 files = os.listdir(dir) |
| 251 except OSError: |
| 252 return |
| 253 |
| 246 for filename in files: | 254 for filename in files: |
| 247 path = os.path.join(dir, filename) | 255 path = os.path.join(dir, filename) |
| 248 if os.path.isfile(path): | 256 if os.path.isfile(path): |
| 249 result.append(relpath + filename) | 257 result.append(relpath + filename) |
| 250 elif os.path.isdir(path): | 258 elif os.path.isdir(path): |
| 251 do_list(path, relpath + filename + "/") | 259 do_list(path, relpath + filename + "/") |
| 252 do_list(self.get_path(subdir), "") | 260 do_list(self.get_path(subdir), "") |
| 253 return result | 261 return result |
| OLD | NEW |