Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/notifications/parser.py

Issue 6582297721569280: Issue 2275 - Parse notification groups (Closed)
Patch Set: Rebased Created April 15, 2015, 9:26 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/management/bin/generateNotifications.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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,
(...skipping 30 matching lines...) Expand all
41 known = True 41 known = True
42 elif spec.startswith(parameter + "Version="): 42 elif spec.startswith(parameter + "Version="):
43 target[parameter + "MinVersion"] = target[parameter + "MaxVersion"] = sp ec[len(parameter + "Version="):] 43 target[parameter + "MinVersion"] = target[parameter + "MaxVersion"] = sp ec[len(parameter + "Version="):]
44 known = True 44 known = True
45 if not known: 45 if not known:
46 raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name )) 46 raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name ))
47 return target 47 return target
48 48
49 def _parse_notification(data, name): 49 def _parse_notification(data, name):
50 notification = {"id": name, "severity": "information", "message": {}, "title": {}} 50 notification = {"id": name, "severity": "information", "message": {}, "title": {}}
51 current = notification
51 52
52 for line in data: 53 for line in data:
53 if not re.search(r"\S", line): 54 if not re.search(r"\S", line):
54 continue 55 continue
55 56
57 if re.search(r"^\[.*\]$", line):
58 current = {"title": {}, "message": {}}
59 notification.setdefault("variants", []).append(current)
60 continue
61
56 if line.find("=") < 0: 62 if line.find("=") < 0:
57 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))
58 64
59 key, value = map(unicode.strip, line.split("=", 1)) 65 key, value = map(unicode.strip, line.split("=", 1))
66 is_variant = current != notification
60 67
61 if key == "inactive": 68 if key == "inactive" and not is_variant:
62 notification["inactive"] = True 69 current["inactive"] = True
63 elif key == "severity": 70 elif key == "severity":
64 if value not in ("information", "critical"): 71 if value not in ("information", "critical"):
65 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))
66 notification["severity"] = value 73 current["severity"] = value
67 elif key == "links": 74 elif key == "links":
68 notification["links"] = value.split() 75 current["links"] = value.split()
69 elif key.startswith("title."): 76 elif key.startswith("title."):
70 locale = key[len("title."):] 77 locale = key[len("title."):]
71 notification["title"][locale] = value 78 current["title"][locale] = value
72 elif key.startswith("message."): 79 elif key.startswith("message."):
73 locale = key[len("message."):] 80 locale = key[len("message."):]
74 notification["message"][locale] = value 81 current["message"][locale] = value
75 elif key == "target": 82 elif key == "target":
76 target = _parse_targetspec(value, name) 83 target = _parse_targetspec(value, name)
77 if "targets" in notification: 84 if "targets" in notification:
78 notification["targets"].append(target) 85 current["targets"].append(target)
79 else: 86 else:
80 notification["targets"] = [target] 87 current["targets"] = [target]
88 elif key == "sample" and is_variant:
89 current["sample"] = float(value)
81 else: 90 else:
82 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name)) 91 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name))
83 92
84 if "en-US" not in notification["title"]: 93 for text_key in ("title", "message"):
85 raise Exception("No title for en-US (default language) in file '%s'" % name) 94 def has_default_locale(variant): return "en-US" in variant[text_key]
86 if "en-US" not in notification["message"]: 95 if (not has_default_locale(notification) and
87 raise Exception("No message for en-US (default language) in file '%s'" % nam e) 96 not all(map(has_default_locale, notification.get("variants", [])))):
97 raise Exception("No %s for en-US (default language) in file '%s'" %
98 (text_key, name))
88 return notification 99 return notification
89 100
90 def load_notifications(): 101 def load_notifications():
91 repo = get_config().get("notifications", "repository") 102 repo = get_config().get("notifications", "repository")
92 subprocess.call(["hg", "-R", repo, "pull", "-q"]) 103 subprocess.call(["hg", "-R", repo, "pull", "-q"])
93 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar", 104 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar",
94 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"] 105 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"]
95 data = subprocess.check_output(command) 106 data = subprocess.check_output(command)
96 107
97 notifications = [] 108 notifications = []
98 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive: 109 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive:
99 for fileinfo in archive: 110 for fileinfo in archive:
100 name = fileinfo.name 111 name = fileinfo.name
101 if name.startswith("./"): 112 if name.startswith("./"):
102 name = name[2:] 113 name = name[2:]
103 114
104 if fileinfo.type == tarfile.REGTYPE: 115 if fileinfo.type == tarfile.REGTYPE:
105 data = codecs.getreader("utf8")(archive.extractfile(fileinfo)) 116 data = codecs.getreader("utf8")(archive.extractfile(fileinfo))
106 try: 117 try:
107 notification = _parse_notification(data, name) 118 notification = _parse_notification(data, name)
108 if "inactive" in notification: 119 if "inactive" in notification:
109 continue 120 continue
110 notifications.append(notification) 121 notifications.append(notification)
111 except: 122 except:
112 traceback.print_exc() 123 traceback.print_exc()
113 return notifications 124 return notifications
OLDNEW
« no previous file with comments | « sitescripts/management/bin/generateNotifications.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld