| OLD | NEW |
| (Empty) |
| 1 # coding: utf-8 | |
| 2 | |
| 3 # This file is part of the Adblock Plus web scripts, | |
| 4 # Copyright (C) 2006-2015 Eyeo GmbH | |
| 5 # | |
| 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 | |
| 8 # published by the Free Software Foundation. | |
| 9 # | |
| 10 # Adblock Plus is distributed in the hope that it will be useful, | |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 # GNU General Public License for more details. | |
| 14 # | |
| 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/>. | |
| 17 | |
| 18 import sys, os, subprocess, zipfile, json, urlparse, codecs | |
| 19 from StringIO import StringIO | |
| 20 from ConfigParser import SafeConfigParser | |
| 21 | |
| 22 class Source: | |
| 23 def resolve_link(self, url, locale): | |
| 24 parsed = urlparse.urlparse(url) | |
| 25 page = parsed.path | |
| 26 if parsed.scheme != "" or page.startswith("/") or page.startswith("."): | |
| 27 # Not a page link | |
| 28 return None, None | |
| 29 | |
| 30 if parsed.path == "" and url != "": | |
| 31 # Page-relative link | |
| 32 return None, None | |
| 33 | |
| 34 config = self.read_config() | |
| 35 default_locale = config.get("general", "defaultlocale") | |
| 36 default_page = config.get("general", "defaultpage") | |
| 37 | |
| 38 checked_page = page | |
| 39 if config.has_option("locale_overrides", 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 | |
| 45 elif self.has_locale(default_locale, checked_page): | |
| 46 if not self.has_locale(locale, checked_page): | |
| 47 locale = default_locale | |
| 48 else: | |
| 49 print >>sys.stderr, "Warning: Link to %s cannot be resolved" % page | |
| 50 | |
| 51 if page == default_page: | |
| 52 page = "" | |
| 53 | |
| 54 path = "/%s/%s" % (locale, page) | |
| 55 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) | |
| 56 | |
| 57 def read_config(self): | |
| 58 configdata = self.read_file("settings.ini") | |
| 59 config = SafeConfigParser() | |
| 60 config.readfp(StringIO(configdata)) | |
| 61 return config | |
| 62 | |
| 63 # | |
| 64 # Page helpers | |
| 65 # | |
| 66 | |
| 67 @staticmethod | |
| 68 def page_filename(page, format): | |
| 69 return "pages/%s.%s" % (page, format) | |
| 70 | |
| 71 def list_pages(self): | |
| 72 for filename in self.list_files("pages"): | |
| 73 root, ext = os.path.splitext(filename) | |
| 74 format = ext[1:].lower() | |
| 75 yield root, format | |
| 76 | |
| 77 def has_page(self, page, format): | |
| 78 return self.has_file(self.page_filename(page, format)) | |
| 79 | |
| 80 def read_page(self, page, format): | |
| 81 return self.read_file(self.page_filename(page, format)) | |
| 82 | |
| 83 # | |
| 84 # Localizable files helpers | |
| 85 # | |
| 86 | |
| 87 @staticmethod | |
| 88 def localizable_file_filename(locale, filename): | |
| 89 return "locales/%s/%s" % (locale, filename) | |
| 90 | |
| 91 def list_localizable_files(self): | |
| 92 default_locale = self.read_config().get("general", "defaultlocale") | |
| 93 return filter( | |
| 94 lambda f: os.path.splitext(f)[1].lower() != ".json", | |
| 95 self.list_files("locales/%s" % default_locale) | |
| 96 ) | |
| 97 | |
| 98 def has_localizable_file(self, locale, filename): | |
| 99 return self.has_file(self.localizable_file_filename(locale, filename)) | |
| 100 | |
| 101 def read_localizable_file(self, locale, filename): | |
| 102 return self.read_file(self.localizable_file_filename(locale, filename), bina
ry=True) | |
| 103 | |
| 104 # | |
| 105 # Static file helpers | |
| 106 # | |
| 107 | |
| 108 @staticmethod | |
| 109 def static_filename(filename): | |
| 110 return "static/%s" % filename | |
| 111 | |
| 112 def list_static(self): | |
| 113 return self.list_files("static") | |
| 114 | |
| 115 def has_static(self, filename): | |
| 116 return self.has_file(self.static_filename(filename)) | |
| 117 | |
| 118 def read_static(self, filename): | |
| 119 return self.read_file(self.static_filename(filename), binary=True) | |
| 120 | |
| 121 # | |
| 122 # Locale helpers | |
| 123 # | |
| 124 | |
| 125 @classmethod | |
| 126 def locale_filename(cls, locale, page): | |
| 127 return cls.localizable_file_filename(locale, page + ".json") | |
| 128 | |
| 129 def list_locales(self): | |
| 130 result = set() | |
| 131 for filename in self.list_files("locales"): | |
| 132 if "/" in filename: | |
| 133 locale, path = filename.split("/", 1) | |
| 134 result.add(locale) | |
| 135 return result | |
| 136 | |
| 137 def has_locale(self, locale, page): | |
| 138 config = self.read_config() | |
| 139 if config.has_option("locale_overrides", page): | |
| 140 page = config.get("locale_overrides", page) | |
| 141 return self.has_file(self.locale_filename(locale, page)) | |
| 142 | |
| 143 def read_locale(self, locale, page): | |
| 144 default_locale = self.read_config().get("general", "defaultlocale") | |
| 145 if locale == default_locale: | |
| 146 result = {} | |
| 147 else: | |
| 148 result = self.read_locale(default_locale, page) | |
| 149 | |
| 150 if self.has_locale(locale, page): | |
| 151 filedata = self.read_file(self.locale_filename(locale, page)) | |
| 152 localedata = json.loads(filedata) | |
| 153 for key, value in localedata.iteritems(): | |
| 154 result[key] = value["message"] | |
| 155 | |
| 156 return result | |
| 157 | |
| 158 # | |
| 159 # Template helpers | |
| 160 # | |
| 161 | |
| 162 @staticmethod | |
| 163 def template_filename(template): | |
| 164 return "templates/%s.tmpl" % template | |
| 165 | |
| 166 def read_template(self, template): | |
| 167 return self.read_file(self.template_filename(template)) | |
| 168 | |
| 169 # | |
| 170 # Include helpers | |
| 171 # | |
| 172 | |
| 173 @staticmethod | |
| 174 def include_filename(include, format): | |
| 175 return "includes/%s.%s" % (include, format) | |
| 176 | |
| 177 def has_include(self, include, format): | |
| 178 return self.has_file(self.include_filename(include, format)) | |
| 179 | |
| 180 def read_include(self, include, format): | |
| 181 return self.read_file(self.include_filename(include, format)) | |
| 182 | |
| 183 class MercurialSource(Source): | |
| 184 def __init__(self, repo): | |
| 185 command = ["hg", "-R", repo, "archive", "-r", "default", | |
| 186 "-t", "uzip", "-p", ".", "-"] | |
| 187 data = subprocess.check_output(command) | |
| 188 self._archive = zipfile.ZipFile(StringIO(data), mode="r") | |
| 189 | |
| 190 command = ["hg", "-R", repo, "id", "-n", "-r", "default"] | |
| 191 self.version = subprocess.check_output(command).strip() | |
| 192 | |
| 193 def __enter__(self): | |
| 194 return self | |
| 195 | |
| 196 def __exit__(self, type, value, traceback): | |
| 197 self.close() | |
| 198 return False | |
| 199 | |
| 200 def close(self): | |
| 201 self._archive.close() | |
| 202 | |
| 203 def has_file(self, filename): | |
| 204 try: | |
| 205 self._archive.getinfo("./%s" % filename) | |
| 206 except KeyError: | |
| 207 return False | |
| 208 return True | |
| 209 | |
| 210 def read_file(self, filename, binary=False): | |
| 211 result = self._archive.read("./%s" % filename) | |
| 212 if not binary: | |
| 213 result = result.decode("utf-8") | |
| 214 return result | |
| 215 | |
| 216 def list_files(self, subdir): | |
| 217 prefix = "./%s/" % subdir | |
| 218 for filename in self._archive.namelist(): | |
| 219 if filename.startswith(prefix): | |
| 220 yield filename[len(prefix):] | |
| 221 | |
| 222 class FileSource(Source): | |
| 223 def __init__(self, dir): | |
| 224 self._dir = dir | |
| 225 | |
| 226 def __enter__(self): | |
| 227 return self | |
| 228 | |
| 229 def __exit__(self, type, value, traceback): | |
| 230 return False | |
| 231 | |
| 232 def close(self): | |
| 233 pass | |
| 234 | |
| 235 def get_path(self, filename): | |
| 236 return os.path.join(self._dir, *filename.split("/")) | |
| 237 | |
| 238 def has_file(self, filename): | |
| 239 return os.path.isfile(self.get_path(filename)) | |
| 240 | |
| 241 def read_file(self, filename, binary=False): | |
| 242 encoding = None if binary else "utf-8" | |
| 243 with codecs.open(self.get_path(filename), "rb", encoding=encoding) as handle
: | |
| 244 return handle.read() | |
| 245 | |
| 246 def list_files(self, subdir): | |
| 247 result = [] | |
| 248 def do_list(dir, relpath): | |
| 249 try: | |
| 250 files = os.listdir(dir) | |
| 251 except OSError: | |
| 252 return | |
| 253 | |
| 254 for filename in files: | |
| 255 path = os.path.join(dir, filename) | |
| 256 if os.path.isfile(path): | |
| 257 result.append(relpath + filename) | |
| 258 elif os.path.isdir(path): | |
| 259 do_list(path, relpath + filename + "/") | |
| 260 do_list(self.get_path(subdir), "") | |
| 261 return result | |
| OLD | NEW |