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: Address comments, parse sample Created April 15, 2015, 8:39 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 known = True 42 known = True
43 elif spec.startswith(parameter + "Version="): 43 elif spec.startswith(parameter + "Version="):
44 target[parameter + "MinVersion"] = target[parameter + "MaxVersion"] = sp ec[len(parameter + "Version="):] 44 target[parameter + "MinVersion"] = target[parameter + "MaxVersion"] = sp ec[len(parameter + "Version="):]
45 known = True 45 known = True
46 if not known: 46 if not known:
47 raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name )) 47 raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name ))
48 return target 48 return target
49 49
50 def _parse_notification(data, name): 50 def _parse_notification(data, name):
51 notification = {"id": name, "severity": "information", "message": {}, "title": {}} 51 notification = {"id": name, "severity": "information", "message": {}, "title": {}}
52 current = notification
52 53
53 for line in data: 54 for line in data:
54 if not re.search(r"\S", line): 55 if not re.search(r"\S", line):
55 continue 56 continue
56 57
58 if re.search(r"^\[.*\]$", line):
59 current = {"title": {}, "message": {}}
60 notification.setdefault("variants", []).append(current)
61 continue
62
57 if line.find("=") < 0: 63 if line.find("=") < 0:
58 raise Exception("Could not process line '%s' in file '%s'" % (line.strip() , name)) 64 raise Exception("Could not process line '%s' in file '%s'" % (line.strip() , name))
59 65
60 key, value = map(unicode.strip, line.split("=", 1)) 66 key, value = map(unicode.strip, line.split("=", 1))
67 is_variant = current != notification
61 68
62 if key == "inactive": 69 if key == "inactive" and not is_variant:
63 notification["inactive"] = True 70 current["inactive"] = True
64 elif key == "severity": 71 elif key == "severity":
65 if value not in ("information", "critical"): 72 if value not in ("information", "critical"):
66 raise Exception("Unknown severity value '%s' in file '%s'" % (value, nam e)) 73 raise Exception("Unknown severity value '%s' in file '%s'" % (value, nam e))
67 notification["severity"] = value 74 current["severity"] = value
68 elif key == "links": 75 elif key == "links":
69 notification["links"] = value.split() 76 current["links"] = value.split()
70 elif key.startswith("title."): 77 elif key.startswith("title."):
71 locale = key[len("title."):] 78 locale = key[len("title."):]
72 notification["title"][locale] = value 79 current["title"][locale] = value
73 elif key.startswith("message."): 80 elif key.startswith("message."):
74 locale = key[len("message."):] 81 locale = key[len("message."):]
75 notification["message"][locale] = value 82 current["message"][locale] = value
76 elif key == "target": 83 elif key == "target":
77 target = _parse_targetspec(value, name) 84 target = _parse_targetspec(value, name)
78 if "targets" in notification: 85 if "targets" in notification:
79 notification["targets"].append(target) 86 current["targets"].append(target)
80 else: 87 else:
81 notification["targets"] = [target] 88 current["targets"] = [target]
89 elif key == "sample" and is_variant:
90 current["sample"] = float(value)
82 else: 91 else:
83 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name)) 92 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name))
84 93
85 if "en-US" not in notification["title"]: 94 for text_key in ("title", "message"):
86 raise Exception("No title for en-US (default language) in file '%s'" % name) 95 def has_default_locale(variant): return "en-US" in variant[text_key]
87 if "en-US" not in notification["message"]: 96 if (not has_default_locale(notification) and
88 raise Exception("No message for en-US (default language) in file '%s'" % nam e) 97 not all(map(has_default_locale, notification.get("variants", [])))):
98 raise Exception("No %s for en-US (default language) in file '%s'" %
99 (text_key, name))
89 return notification 100 return notification
90 101
91 def load_notifications(): 102 def load_notifications():
92 repo = get_config().get("notifications", "repository") 103 repo = get_config().get("notifications", "repository")
93 subprocess.call(["hg", "-R", repo, "pull", "-q"]) 104 subprocess.call(["hg", "-R", repo, "pull", "-q"])
94 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar", 105 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar",
95 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"] 106 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"]
96 data = subprocess.check_output(command) 107 data = subprocess.check_output(command)
97 108
98 result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notification s": []} 109 result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notification s": []}
99 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive: 110 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive:
100 for fileinfo in archive: 111 for fileinfo in archive:
101 name = fileinfo.name 112 name = fileinfo.name
102 if name.startswith("./"): 113 if name.startswith("./"):
103 name = name[2:] 114 name = name[2:]
104 115
105 if fileinfo.type == tarfile.REGTYPE: 116 if fileinfo.type == tarfile.REGTYPE:
106 data = codecs.getreader("utf8")(archive.extractfile(fileinfo)) 117 data = codecs.getreader("utf8")(archive.extractfile(fileinfo))
107 try: 118 try:
108 notification = _parse_notification(data, name) 119 notification = _parse_notification(data, name)
109 if "inactive" in notification: 120 if "inactive" in notification:
110 continue 121 continue
111 result["notifications"].append(notification) 122 result["notifications"].append(notification)
112 except: 123 except:
113 traceback.print_exc() 124 traceback.print_exc()
114 return result 125 return result
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