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

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

Issue 29321041: Issue 2707 - Support start/end notification parameters (Closed)
Patch Set: Change date format Created June 24, 2015, 7:52 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 | « no previous file | sitescripts/notifications/test/parser.py » ('j') | 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,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 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/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import codecs 18 import codecs
19 import datetime
19 import os 20 import os
20 import re 21 import re
21 import subprocess 22 import subprocess
22 import tarfile 23 import tarfile
23 import traceback 24 import traceback
24 from StringIO import StringIO 25 from StringIO import StringIO
25 26
26 from sitescripts.utils import get_config 27 from sitescripts.utils import get_config
27 28
28 def _parse_targetspec(value, name): 29 def _parse_targetspec(value, name):
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 locale = key[len("message."):] 81 locale = key[len("message."):]
81 current["message"][locale] = value 82 current["message"][locale] = value
82 elif key == "target": 83 elif key == "target":
83 target = _parse_targetspec(value, name) 84 target = _parse_targetspec(value, name)
84 if "targets" in notification: 85 if "targets" in notification:
85 current["targets"].append(target) 86 current["targets"].append(target)
86 else: 87 else:
87 current["targets"] = [target] 88 current["targets"] = [target]
88 elif key == "sample" and is_variant: 89 elif key == "sample" and is_variant:
89 current["sample"] = float(value) 90 current["sample"] = float(value)
91 elif key in ["start", "end"]:
92 current[key] = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M")
90 else: 93 else:
91 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name)) 94 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name))
92 95
93 for text_key in ("title", "message"): 96 for text_key in ("title", "message"):
94 def has_default_locale(variant): return "en-US" in variant[text_key] 97 def has_default_locale(variant): return "en-US" in variant[text_key]
95 if (not has_default_locale(notification) and 98 if (not has_default_locale(notification) and
96 not all(map(has_default_locale, notification.get("variants", [])))): 99 not all(map(has_default_locale, notification.get("variants", [])))):
97 raise Exception("No %s for en-US (default language) in file '%s'" % 100 raise Exception("No %s for en-US (default language) in file '%s'" %
98 (text_key, name)) 101 (text_key, name))
99 return notification 102 return notification
(...skipping 11 matching lines...) Expand all
111 name = fileinfo.name 114 name = fileinfo.name
112 if name.startswith("./"): 115 if name.startswith("./"):
113 name = name[2:] 116 name = name[2:]
114 117
115 if fileinfo.type == tarfile.REGTYPE: 118 if fileinfo.type == tarfile.REGTYPE:
116 data = codecs.getreader("utf8")(archive.extractfile(fileinfo)) 119 data = codecs.getreader("utf8")(archive.extractfile(fileinfo))
117 try: 120 try:
118 notification = _parse_notification(data, name) 121 notification = _parse_notification(data, name)
119 if "inactive" in notification: 122 if "inactive" in notification:
120 continue 123 continue
124 current_time = datetime.datetime.now()
125 if "start" in notification and current_time < notification["start"]:
126 continue
127 if "end" in notification and current_time > notification["end"]:
128 continue
121 notifications.append(notification) 129 notifications.append(notification)
122 except: 130 except:
123 traceback.print_exc() 131 traceback.print_exc()
124 return notifications 132 return notifications
OLDNEW
« no previous file with comments | « no previous file | sitescripts/notifications/test/parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld