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: Fix import order Created June 23, 2015, 3:51 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 | « 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
27 import dateutil.parser
28
26 from sitescripts.utils import get_config 29 from sitescripts.utils import get_config
27 30
28 def _parse_targetspec(value, name): 31 def _parse_targetspec(value, name):
29 target = {} 32 target = {}
30 for spec in value.split(): 33 for spec in value.split():
31 known = False 34 known = False
32 for parameter in ("extension", "application", "platform"): 35 for parameter in ("extension", "application", "platform"):
33 if spec.startswith(parameter + "="): 36 if spec.startswith(parameter + "="):
34 target[parameter] = spec[len(parameter + "="):] 37 target[parameter] = spec[len(parameter + "="):]
35 known = True 38 known = True
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 locale = key[len("message."):] 83 locale = key[len("message."):]
81 current["message"][locale] = value 84 current["message"][locale] = value
82 elif key == "target": 85 elif key == "target":
83 target = _parse_targetspec(value, name) 86 target = _parse_targetspec(value, name)
84 if "targets" in notification: 87 if "targets" in notification:
85 current["targets"].append(target) 88 current["targets"].append(target)
86 else: 89 else:
87 current["targets"] = [target] 90 current["targets"] = [target]
88 elif key == "sample" and is_variant: 91 elif key == "sample" and is_variant:
89 current["sample"] = float(value) 92 current["sample"] = float(value)
93 elif key in ["start", "end"]:
94 current[key] = dateutil.parser.parse(value)
90 else: 95 else:
91 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name)) 96 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name))
92 97
93 for text_key in ("title", "message"): 98 for text_key in ("title", "message"):
94 def has_default_locale(variant): return "en-US" in variant[text_key] 99 def has_default_locale(variant): return "en-US" in variant[text_key]
95 if (not has_default_locale(notification) and 100 if (not has_default_locale(notification) and
96 not all(map(has_default_locale, notification.get("variants", [])))): 101 not all(map(has_default_locale, notification.get("variants", [])))):
97 raise Exception("No %s for en-US (default language) in file '%s'" % 102 raise Exception("No %s for en-US (default language) in file '%s'" %
98 (text_key, name)) 103 (text_key, name))
99 return notification 104 return notification
(...skipping 11 matching lines...) Expand all
111 name = fileinfo.name 116 name = fileinfo.name
112 if name.startswith("./"): 117 if name.startswith("./"):
113 name = name[2:] 118 name = name[2:]
114 119
115 if fileinfo.type == tarfile.REGTYPE: 120 if fileinfo.type == tarfile.REGTYPE:
116 data = codecs.getreader("utf8")(archive.extractfile(fileinfo)) 121 data = codecs.getreader("utf8")(archive.extractfile(fileinfo))
117 try: 122 try:
118 notification = _parse_notification(data, name) 123 notification = _parse_notification(data, name)
119 if "inactive" in notification: 124 if "inactive" in notification:
120 continue 125 continue
126 current_time = datetime.datetime.now()
127 if "start" in notification and current_time < notification["start"]:
128 continue
129 if "end" in notification and current_time > notification["end"]:
130 continue
121 notifications.append(notification) 131 notifications.append(notification)
122 except: 132 except:
123 traceback.print_exc() 133 traceback.print_exc()
124 return notifications 134 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