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

Unified Diff: sitescripts/notifications/parser.py

Issue 6308119894294528: Issue 2274 - Move notification parsing into a module (Closed)
Patch Set: Don't set the version in parser.load_notifications() Created April 15, 2015, 9:24 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sitescripts/notifications/__init__.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/notifications/parser.py
===================================================================
copy from sitescripts/management/bin/generateNotifications.py
copy to sitescripts/notifications/parser.py
--- a/sitescripts/management/bin/generateNotifications.py
+++ b/sitescripts/notifications/parser.py
@@ -15,11 +15,17 @@
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
-import os, re, subprocess, tarfile, codecs, time, traceback, json
+import codecs
+import os
+import re
+import subprocess
+import tarfile
+import traceback
from StringIO import StringIO
-from sitescripts.utils import get_config, setupStderr
-def parse_targetspec(value, name):
+from sitescripts.utils import get_config
+
+def _parse_targetspec(value, name):
target = {}
for spec in value.split():
known = False
@@ -40,7 +46,7 @@
raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name))
return target
-def parse_notification(data, name):
+def _parse_notification(data, name):
notification = {"id": name, "severity": "information", "message": {}, "title": {}}
for line in data:
@@ -67,7 +73,7 @@
locale = key[len("message."):]
notification["message"][locale] = value
elif key == "target":
- target = parse_targetspec(value, name)
+ target = _parse_targetspec(value, name)
if "targets" in notification:
notification["targets"].append(target)
else:
@@ -81,12 +87,14 @@
raise Exception("No message for en-US (default language) in file '%s'" % name)
return notification
-def generate_notifications(repo, path):
+def load_notifications():
+ repo = get_config().get("notifications", "repository")
+ subprocess.call(["hg", "-R", repo, "pull", "-q"])
command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar",
"-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"]
data = subprocess.check_output(command)
- result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notifications": []}
+ notifications = []
with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive:
for fileinfo in archive:
name = fileinfo.name
@@ -96,20 +104,10 @@
if fileinfo.type == tarfile.REGTYPE:
data = codecs.getreader("utf8")(archive.extractfile(fileinfo))
try:
- notification = parse_notification(data, name)
+ notification = _parse_notification(data, name)
if "inactive" in notification:
continue
- result["notifications"].append(notification)
+ notifications.append(notification)
except:
traceback.print_exc()
-
- with codecs.open(path, "wb", encoding="utf-8") as file:
- json.dump(result, file, ensure_ascii=False, indent=2,
- separators=(',', ': '), sort_keys=True)
-
-if __name__ == "__main__":
- setupStderr()
- repo = get_config().get("notifications", "repository")
- output = get_config().get("notifications", "output")
- subprocess.call(["hg", "-R", repo, "pull", "-q"])
- generate_notifications(repo, output)
+ return notifications
« 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