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: Created April 11, 2015, 9:27 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
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 if not "variants" in notification:
61 notification["variants"] = []
62 notification["variants"].append(current)
63 continue
64
57 if line.find("=") < 0: 65 if line.find("=") < 0:
58 raise Exception("Could not process line '%s' in file '%s'" % (line.strip() , name)) 66 raise Exception("Could not process line '%s' in file '%s'" % (line.strip() , name))
59 67
60 key, value = map(unicode.strip, line.split("=", 1)) 68 key, value = map(unicode.strip, line.split("=", 1))
69 is_variant = current != notification
61 70
62 if key == "inactive": 71 if key == "inactive" and not is_variant:
63 notification["inactive"] = True 72 current["inactive"] = True
64 elif key == "severity": 73 elif key == "severity":
65 if value not in ("information", "critical"): 74 if value not in ("information", "critical"):
66 raise Exception("Unknown severity value '%s' in file '%s'" % (value, nam e)) 75 raise Exception("Unknown severity value '%s' in file '%s'" % (value, nam e))
67 notification["severity"] = value 76 current["severity"] = value
68 elif key == "links": 77 elif key == "links":
69 notification["links"] = value.split() 78 current["links"] = value.split()
70 elif key.startswith("title."): 79 elif key.startswith("title."):
71 locale = key[len("title."):] 80 locale = key[len("title."):]
72 notification["title"][locale] = value 81 current["title"][locale] = value
73 elif key.startswith("message."): 82 elif key.startswith("message."):
74 locale = key[len("message."):] 83 locale = key[len("message."):]
75 notification["message"][locale] = value 84 current["message"][locale] = value
76 elif key == "target": 85 elif key == "target":
77 target = _parse_targetspec(value, name) 86 target = _parse_targetspec(value, name)
78 if "targets" in notification: 87 if "targets" in notification:
79 notification["targets"].append(target) 88 current["targets"].append(target)
80 else: 89 else:
81 notification["targets"] = [target] 90 current["targets"] = [target]
91 elif key == "sample" and is_variant:
92 current["sample"] = value
82 else: 93 else:
83 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name)) 94 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name))
84 95
85 if "en-US" not in notification["title"]: 96 for text_key in ["title", "message"]:
Sebastian Noack 2015/04/13 09:04:37 Please use tuples for sequences that don't need to
86 raise Exception("No title for en-US (default language) in file '%s'" % name) 97 if "en-US" not in notification[text_key] and \
87 if "en-US" not in notification["message"]: 98 (not "variants" in notification or \
Sebastian Noack 2015/04/13 09:04:37 Hint: Newlines don't need to be escaped within par
88 raise Exception("No message for en-US (default language) in file '%s'" % nam e) 99 not all("en-US" in variant[text_key]
100 for variant in notification["variants"])):
101 raise Exception("No %s for en-US (default language) in file '%s'" %
102 (text_key, name))
89 return notification 103 return notification
90 104
91 def load_notifications(): 105 def load_notifications():
92 repo = get_config().get("notifications", "repository") 106 repo = get_config().get("notifications", "repository")
93 subprocess.call(["hg", "-R", repo, "pull", "-q"]) 107 subprocess.call(["hg", "-R", repo, "pull", "-q"])
94 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar", 108 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar",
95 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"] 109 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"]
96 data = subprocess.check_output(command) 110 data = subprocess.check_output(command)
97 111
98 result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notification s": []} 112 result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notification s": []}
99 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive: 113 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive:
100 for fileinfo in archive: 114 for fileinfo in archive:
101 name = fileinfo.name 115 name = fileinfo.name
102 if name.startswith("./"): 116 if name.startswith("./"):
103 name = name[2:] 117 name = name[2:]
104 118
105 if fileinfo.type == tarfile.REGTYPE: 119 if fileinfo.type == tarfile.REGTYPE:
106 data = codecs.getreader("utf8")(archive.extractfile(fileinfo)) 120 data = codecs.getreader("utf8")(archive.extractfile(fileinfo))
107 try: 121 try:
108 notification = _parse_notification(data, name) 122 notification = _parse_notification(data, name)
109 if "inactive" in notification: 123 if "inactive" in notification:
110 continue 124 continue
111 result["notifications"].append(notification) 125 result["notifications"].append(notification)
112 except: 126 except:
113 traceback.print_exc() 127 traceback.print_exc()
114 return result 128 return result
OLDNEW

Powered by Google App Engine
This is Rietveld