| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # vim:fileencoding=utf-8:et:ts=4:sts=4 | |
|
Felix Dahlke
2015/09/17 09:52:34
We generally don't add mode line headers: https://
René Jeschke
2015/09/17 10:39:49
Done.
| |
| 2 # | |
| 3 # This file is part of Adblock Plus | |
| 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 os | |
| 19 import re | |
| 20 | |
| 21 ABB_LOCALE_RE = re.compile("^[a-z]{2}(-[A-Z]{2})?$") | |
|
Felix Dahlke
2015/09/17 09:52:34
There is no need to prefix anything with "abb" her
René Jeschke
2015/09/17 10:39:49
Done.
| |
| 22 ABB_SEARCH_PROPS_RE = re.compile("^browser\.search\." | |
| 23 "(defaultenginename|order\.).*$") | |
| 24 ABB_SHORTNAME_RE = re.compile("^<ShortName>.*</ShortName>$") | |
| 25 | |
| 26 ABB_SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins") | |
| 27 ABB_LIST_TXT_PATH = os.path.join(ABB_SEARCHPLUGINS_PATH, "list.txt") | |
| 28 ABB_REGION_PROPS_PATH = os.path.join("browser", "region.properties") | |
| 29 | |
| 30 ABB_DEFAULT_LOCALE = "en-US" | |
| 31 | |
| 32 ABB_CFG = { | |
|
Felix Dahlke
2015/09/17 09:52:35
Looks like this could be simplified:
_SEARCH_ENGI
René Jeschke
2015/09/17 10:39:49
Done.
| |
| 33 "en-US": {"ordered-whitelist": ["duckduckgo", | |
| 34 "yahoo", | |
| 35 "google", | |
| 36 "wikipedia", | |
| 37 "amazon"]}, | |
| 38 "zh-CN": {"ordered-whitelist": ["baidu", | |
| 39 "duckduckgo", | |
| 40 "yahoo", | |
| 41 "google", | |
| 42 "wikipedia", | |
| 43 "amazon"]} | |
| 44 } | |
| 45 | |
| 46 | |
| 47 def abb_get_shortname_from_id(needle, engine_ids, engine_map): | |
| 48 """Fuzzy finds needle in engine_ids and returns ShortName""" | |
| 49 regex = re.compile("^%s.*$" % needle) | |
| 50 for engine in engine_ids: | |
| 51 if regex.match(engine.lower()): | |
| 52 return engine_map[engine] | |
| 53 return None | |
| 54 | |
| 55 | |
| 56 def abb_write_lines(filename, lines): | |
| 57 """Writes lines into file appending \\n""" | |
| 58 with open(filename, "w") as fd: | |
| 59 for l in lines: | |
| 60 fd.write("%s\n" % l) | |
| 61 | |
| 62 | |
| 63 def abb_transform_locale(locale, path, fns): | |
| 64 fns["info"]("Processing locale '%s'..." % locale) | |
| 65 | |
| 66 # Get configuration for current locale | |
| 67 cfg = ABB_CFG.get(locale, ABB_CFG[ABB_DEFAULT_LOCALE]) | |
| 68 | |
| 69 # Check for list.txt existence | |
| 70 list_file = os.path.join(path, ABB_LIST_TXT_PATH) | |
| 71 if not os.path.exists(list_file): | |
| 72 fns["fatal"]("Missing 'list.txt' for locale '%s'" % locale) | |
| 73 | |
| 74 # Check for region.properties existence | |
| 75 region_file = os.path.join(path, ABB_REGION_PROPS_PATH) | |
| 76 if not os.path.exists(region_file): | |
| 77 fns["fatal"]("Missing 'region.properties' for locale '%s'" % locale) | |
| 78 | |
| 79 # Get whitelist and build regex | |
| 80 whitelist = cfg["ordered-whitelist"] | |
| 81 white_re = re.compile("^(%s).*$" % ("|".join(whitelist))) | |
| 82 | |
| 83 # Read engine IDs from list.txt, discard engines not on whitelist | |
| 84 engine_ids = [] | |
| 85 for line in open(list_file, "r"): | |
| 86 line = line.strip() | |
| 87 if len(line) > 0: | |
| 88 if white_re.match(line.lower()): | |
| 89 engine_ids.append(line) | |
| 90 else: | |
| 91 fns["info"]("Removing '%s'" % line) | |
| 92 | |
| 93 # Make sure we still have search engines left | |
| 94 if len(engine_ids) == 0: | |
| 95 fns["fatal"]("No search engines left over for '%s'" % locale) | |
| 96 | |
| 97 # 'Parse' XML to get matching 'ShortName' for all engine IDs | |
| 98 engine_names = {} | |
| 99 for eid in engine_ids: | |
| 100 xml_file = os.path.join(path, ABB_SEARCHPLUGINS_PATH, "%s.xml" % eid) | |
| 101 if os.path.exists(xml_file): | |
| 102 short_name = None | |
| 103 for line in open(xml_file, "r"): | |
| 104 line = line.strip() | |
| 105 if ABB_SHORTNAME_RE.match(line): | |
| 106 short_name = line[11:-12].trim() | |
| 107 | |
| 108 if not short_name: | |
| 109 fns["fatal"]("No ShortName defined for '%s' in '%s" % | |
| 110 (eid, locale)) | |
| 111 engine_names[eid] = short_name | |
| 112 else: | |
| 113 fns["fatal"]("XML definiton for '%s' in '%s' missing" % | |
| 114 (eid, locale)) | |
| 115 | |
| 116 fns["info"]("Remaining engine IDs: %s" % ", ".join(engine_ids)) | |
| 117 | |
| 118 # Create search engine order with real engine names | |
| 119 engine_order = [] | |
| 120 for eid in whitelist: | |
| 121 sn = abb_get_shortname_from_id(eid, engine_ids, engine_names) | |
| 122 if sn: | |
| 123 engine_order.append(sn) | |
| 124 | |
| 125 fns["info"]("Resulting ordered list: %s" % (", ".join(engine_order))) | |
| 126 | |
| 127 # Read region.properties and remove browser.search.* lines | |
| 128 props = [] | |
| 129 for line in open(region_file, "r"): | |
| 130 line = line.rstrip("\r\n") | |
| 131 if not ABB_SEARCH_PROPS_RE.match(line.strip()): | |
| 132 props.append(line) | |
| 133 | |
| 134 # Append default search engine name | |
| 135 props.append("browser.search.defaultenginename=%s" % engine_order[0]) | |
| 136 | |
| 137 # Append search engine order | |
| 138 for i in range(0, min(3, len(engine_order))): | |
| 139 props.append("browser.search.order.%d=%s" % (i + 1, engine_order[i])) | |
| 140 | |
| 141 # Write back list.txt | |
| 142 abb_write_lines(list_file, engine_ids) | |
| 143 | |
| 144 # Write back region.properties | |
| 145 abb_write_lines(region_file, props) | |
| 146 | |
| 147 | |
| 148 def abb_print_info(obj): | |
| 149 """ Wrapper for 'self.info' (to be self-contained) """ | |
| 150 def fn(s): | |
| 151 if obj: | |
| 152 obj.info(s) | |
| 153 else: | |
| 154 print "INFO: %s" % s | |
| 155 return fn | |
| 156 | |
| 157 | |
| 158 def abb_print_error(obj): | |
| 159 """ Wrapper for 'self.error' (to be self-contained) """ | |
| 160 def fn(s): | |
| 161 if obj: | |
| 162 obj.error(s) | |
| 163 else: | |
| 164 print "ERROR: %s" % s | |
| 165 return fn | |
| 166 | |
| 167 | |
| 168 def abb_exit_fatal(obj): | |
| 169 """ Wrapper for 'self.fatal' (to be self-contained) """ | |
| 170 def fn(s): | |
| 171 if obj: | |
| 172 obj.fatal(s) | |
| 173 else: | |
| 174 print "FATAL: %s" % s | |
| 175 exit(1) | |
| 176 return fn | |
| 177 | |
| 178 | |
| 179 def abb_transform_locales_impl(obj_dir, obj): | |
| 180 fns = {"info": abb_print_info(obj), | |
| 181 "error": abb_print_error(obj), | |
| 182 "fatal": abb_exit_fatal(obj)} | |
| 183 | |
| 184 chrome_path = os.path.join(obj_dir, "dist", "bin", "chrome") | |
| 185 if not os.path.exists(chrome_path): | |
| 186 fns["fatal"]("'dist/bin/chrome' not existent in '%s'" % obj_dir) | |
| 187 | |
| 188 locales = [] | |
| 189 for p in next(os.walk(chrome_path))[1]: | |
| 190 if ABB_LOCALE_RE.match(p): | |
| 191 locales.append(p) | |
| 192 locales.sort() | |
| 193 | |
| 194 fns["info"]("Found %d locales" % len(locales)) | |
| 195 | |
| 196 for locale in locales: | |
| 197 locale_path = os.path.join(chrome_path, locale, "locale", locale) | |
| 198 if os.path.exists(locale_path): | |
| 199 abb_transform_locale(locale, locale_path, fns) | |
| 200 else: | |
| 201 fns["error"]("Missing 'locale' folder for '%s'" % locale) | |
| 202 | |
| OLD | NEW |