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

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

Issue 6308119894294528: Issue 2274 - Move notification parsing into a module (Closed)
Patch Set: Created April 4, 2015, 11:44 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
« no previous file with comments | « sitescripts/notifications/__init__.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
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
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 codecs
19 import os
20 import re
21 import subprocess
22 import tarfile
23 import time
24 import traceback
25 from StringIO import StringIO
26
27 from sitescripts.utils import get_config
28
29 def _parse_targetspec(value, name):
30 target = {}
31 for spec in value.split():
32 known = False
33 for parameter in ("extension", "application", "platform"):
34 if spec.startswith(parameter + "="):
35 target[parameter] = spec[len(parameter + "="):]
36 known = True
37 elif spec.startswith(parameter + "Version>="):
38 target[parameter + "MinVersion"] = spec[len(parameter + "Version>="):]
39 known = True
40 elif spec.startswith(parameter + "Version<="):
41 target[parameter + "MaxVersion"] = spec[len(parameter + "Version<="):]
42 known = True
43 elif spec.startswith(parameter + "Version="):
44 target[parameter + "MinVersion"] = target[parameter + "MaxVersion"] = sp ec[len(parameter + "Version="):]
45 known = True
46 if not known:
47 raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name ))
48 return target
49
50 def _parse_notification(data, name):
51 notification = {"id": name, "severity": "information", "message": {}, "title": {}}
52
53 for line in data:
54 if not re.search(r"\S", line):
55 continue
56
57 if line.find("=") < 0:
58 raise Exception("Could not process line '%s' in file '%s'" % (line.strip() , name))
59
60 key, value = map(unicode.strip, line.split("=", 1))
61
62 if key == "inactive":
63 notification["inactive"] = True
64 elif key == "severity":
65 if value not in ("information", "critical"):
66 raise Exception("Unknown severity value '%s' in file '%s'" % (value, nam e))
67 notification["severity"] = value
68 elif key == "links":
69 notification["links"] = value.split()
70 elif key.startswith("title."):
71 locale = key[len("title."):]
72 notification["title"][locale] = value
73 elif key.startswith("message."):
74 locale = key[len("message."):]
75 notification["message"][locale] = value
76 elif key == "target":
77 target = _parse_targetspec(value, name)
78 if "targets" in notification:
79 notification["targets"].append(target)
80 else:
81 notification["targets"] = [target]
82 else:
83 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name))
84
85 if "en-US" not in notification["title"]:
86 raise Exception("No title for en-US (default language) in file '%s'" % name)
87 if "en-US" not in notification["message"]:
88 raise Exception("No message for en-US (default language) in file '%s'" % nam e)
89 return notification
90
91 def load_notifications():
92 repo = get_config().get("notifications", "repository")
93 subprocess.call(["hg", "-R", repo, "pull", "-q"])
94 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar",
95 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"]
96 data = subprocess.check_output(command)
97
98 result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notification s": []}
99 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive:
100 for fileinfo in archive:
101 name = fileinfo.name
102 if name.startswith("./"):
103 name = name[2:]
104
105 if fileinfo.type == tarfile.REGTYPE:
106 data = codecs.getreader("utf8")(archive.extractfile(fileinfo))
107 try:
108 notification = _parse_notification(data, name)
109 if "inactive" in notification:
110 continue
111 result["notifications"].append(notification)
112 except:
113 traceback.print_exc()
114 return result
OLDNEW
« no previous file with comments | « sitescripts/notifications/__init__.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld