| Left: | ||
| Right: |
| 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-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 codecs | 18 import codecs |
| 19 import os | 19 import os |
| 20 import re | 20 import re |
| 21 import subprocess | 21 import subprocess |
| 22 import tarfile | 22 import tarfile |
| 23 import time | |
| 24 import traceback | 23 import traceback |
| 25 from StringIO import StringIO | 24 from StringIO import StringIO |
| 26 | 25 |
| 27 from sitescripts.utils import get_config | 26 from sitescripts.utils import get_config |
| 28 | 27 |
| 29 def _parse_targetspec(value, name): | 28 def _parse_targetspec(value, name): |
| 30 target = {} | 29 target = {} |
| 31 for spec in value.split(): | 30 for spec in value.split(): |
| 32 known = False | 31 known = False |
| 33 for parameter in ("extension", "application", "platform"): | 32 for parameter in ("extension", "application", "platform"): |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 50 def _parse_notification(data, name): | 49 def _parse_notification(data, name): |
| 51 notification = {"id": name, "severity": "information", "message": {}, "title": {}} | 50 notification = {"id": name, "severity": "information", "message": {}, "title": {}} |
| 52 current = notification | 51 current = notification |
| 53 | 52 |
| 54 for line in data: | 53 for line in data: |
| 55 if not re.search(r"\S", line): | 54 if not re.search(r"\S", line): |
| 56 continue | 55 continue |
| 57 | 56 |
| 58 if re.search(r"^\[.*\]$", line): | 57 if re.search(r"^\[.*\]$", line): |
| 59 current = {"title": {}, "message": {}} | 58 current = {"title": {}, "message": {}} |
| 60 if not "variants" in notification: | 59 notification.setdefault("variants", []).append(current) |
| 61 notification["variants"] = [] | |
| 62 notification["variants"].append(current) | |
| 63 continue | 60 continue |
| 64 | 61 |
| 65 if line.find("=") < 0: | 62 if line.find("=") < 0: |
| 66 raise Exception("Could not process line '%s' in file '%s'" % (line.strip() , name)) | 63 raise Exception("Could not process line '%s' in file '%s'" % (line.strip() , name)) |
| 67 | 64 |
| 68 key, value = map(unicode.strip, line.split("=", 1)) | 65 key, value = map(unicode.strip, line.split("=", 1)) |
| 69 is_variant = current != notification | 66 is_variant = current != notification |
| 70 | 67 |
| 71 if key == "inactive" and not is_variant: | 68 if key == "inactive" and not is_variant: |
| 72 current["inactive"] = True | 69 current["inactive"] = True |
| 73 elif key == "severity": | 70 elif key == "severity": |
| 74 if value not in ("information", "critical"): | 71 if value not in ("information", "critical"): |
| 75 raise Exception("Unknown severity value '%s' in file '%s'" % (value, nam e)) | 72 raise Exception("Unknown severity value '%s' in file '%s'" % (value, nam e)) |
| 76 current["severity"] = value | 73 current["severity"] = value |
| 77 elif key == "links": | 74 elif key == "links": |
| 78 current["links"] = value.split() | 75 current["links"] = value.split() |
| 79 elif key.startswith("title."): | 76 elif key.startswith("title."): |
| 80 locale = key[len("title."):] | 77 locale = key[len("title."):] |
| 81 current["title"][locale] = value | 78 current["title"][locale] = value |
| 82 elif key.startswith("message."): | 79 elif key.startswith("message."): |
| 83 locale = key[len("message."):] | 80 locale = key[len("message."):] |
| 84 current["message"][locale] = value | 81 current["message"][locale] = value |
| 85 elif key == "target": | 82 elif key == "target": |
| 86 target = _parse_targetspec(value, name) | 83 target = _parse_targetspec(value, name) |
| 87 if "targets" in notification: | 84 if "targets" in notification: |
| 88 current["targets"].append(target) | 85 current["targets"].append(target) |
| 89 else: | 86 else: |
| 90 current["targets"] = [target] | 87 current["targets"] = [target] |
| 91 elif key == "sample" and is_variant: | 88 elif key == "sample" and is_variant: |
| 92 current["sample"] = value | 89 current["sample"] = float(value) |
| 93 else: | 90 else: |
| 94 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name)) | 91 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name)) |
| 95 | 92 |
| 96 for text_key in ["title", "message"]: | 93 for text_key in ("title", "message"): |
|
Sebastian Noack
2015/04/13 09:04:37
Please use tuples for sequences that don't need to
| |
| 97 if "en-US" not in notification[text_key] and \ | 94 def has_default_locale(variant): return "en-US" in variant[text_key] |
| 98 (not "variants" in notification or \ | 95 if (not has_default_locale(notification) and |
|
Sebastian Noack
2015/04/13 09:04:37
Hint: Newlines don't need to be escaped within par
| |
| 99 not all("en-US" in variant[text_key] | 96 not all(map(has_default_locale, notification.get("variants", [])))): |
| 100 for variant in notification["variants"])): | |
| 101 raise Exception("No %s for en-US (default language) in file '%s'" % | 97 raise Exception("No %s for en-US (default language) in file '%s'" % |
| 102 (text_key, name)) | 98 (text_key, name)) |
| 103 return notification | 99 return notification |
| 104 | 100 |
| 105 def load_notifications(): | 101 def load_notifications(): |
| 106 repo = get_config().get("notifications", "repository") | 102 repo = get_config().get("notifications", "repository") |
| 107 subprocess.call(["hg", "-R", repo, "pull", "-q"]) | 103 subprocess.call(["hg", "-R", repo, "pull", "-q"]) |
| 108 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar", | 104 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar", |
| 109 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"] | 105 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"] |
| 110 data = subprocess.check_output(command) | 106 data = subprocess.check_output(command) |
| 111 | 107 |
| 112 result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notification s": []} | 108 notifications = [] |
| 113 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive: | 109 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive: |
| 114 for fileinfo in archive: | 110 for fileinfo in archive: |
| 115 name = fileinfo.name | 111 name = fileinfo.name |
| 116 if name.startswith("./"): | 112 if name.startswith("./"): |
| 117 name = name[2:] | 113 name = name[2:] |
| 118 | 114 |
| 119 if fileinfo.type == tarfile.REGTYPE: | 115 if fileinfo.type == tarfile.REGTYPE: |
| 120 data = codecs.getreader("utf8")(archive.extractfile(fileinfo)) | 116 data = codecs.getreader("utf8")(archive.extractfile(fileinfo)) |
| 121 try: | 117 try: |
| 122 notification = _parse_notification(data, name) | 118 notification = _parse_notification(data, name) |
| 123 if "inactive" in notification: | 119 if "inactive" in notification: |
| 124 continue | 120 continue |
| 125 result["notifications"].append(notification) | 121 notifications.append(notification) |
| 126 except: | 122 except: |
| 127 traceback.print_exc() | 123 traceback.print_exc() |
| 128 return result | 124 return notifications |
| LEFT | RIGHT |