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

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

Issue 29527607: Issue 5458, 5457 - Refactor _parse_targetspec, add support for locales and blockTotal (Closed)
Patch Set: Use assert instead of assertEqual across the board, also test no additional targets are present Created Sept. 21, 2017, 10:10 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 | « sitescripts/notifications/README.md ('k') | 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 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH 2 # Copyright (C) 2006-present eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 import codecs 16 import codecs
17 import datetime 17 import datetime
18 import os 18 import os
19 import re 19 import re
20 import subprocess 20 import subprocess
21 import tarfile 21 import tarfile
22 import traceback 22 import traceback
23 from StringIO import StringIO 23 from StringIO import StringIO
24 24
25 from sitescripts.utils import get_config 25 from sitescripts.utils import get_config
26 26
27 27
28 def _parse_targetspec(value, name): 28 def _parse_targetspec(value, name):
29 target = {} 29 target = {}
30 for spec in value.split(): 30
31 known = False 31 items = [
32 for parameter in ('extension', 'application', 'platform'): 32 (r'^(extension|application|platform)(=)(.+)$', {
33 if spec.startswith(parameter + '='): 33 '=': (lambda k, v: {k: v}),
34 target[parameter] = spec[len(parameter + '='):] 34 }),
35 known = True 35 (r'^(extension|application|platform)Version(=|\>=|\<=)(.+)$', {
36 elif spec.startswith(parameter + 'Version>='): 36 '>=': (lambda k, v: {k + 'MinVersion': v}),
37 target[parameter + 'MinVersion'] = spec[len(parameter + 'Version >='):] 37 '<=': (lambda k, v: {k + 'MaxVersion': v}),
38 known = True 38 '=': (lambda k, v: {k + 'MinVersion': v, k + 'MaxVersion': v}),
39 elif spec.startswith(parameter + 'Version<='): 39 }),
40 target[parameter + 'MaxVersion'] = spec[len(parameter + 'Version <='):] 40 (r'^(blockedTotal)(=|\>=|\<=)(\d+)$', {
41 known = True 41 '>=': (lambda k, v: {k + 'Min': int(v)}),
42 elif spec.startswith(parameter + 'Version='): 42 '<=': (lambda k, v: {k + 'Max': int(v)}),
43 target[parameter + 'MinVersion'] = target[parameter + 'MaxVersio n'] = spec[len(parameter + 'Version='):] 43 '=': (lambda k, v: {k + 'Min': int(v), k + 'Max': int(v)}),
44 known = True 44 }),
45 if not known: 45 (r'^(locales)(=)([\w\-,]+)$', {
46 raise Exception("Unknown target specifier '%s' in file '%s'" % (spec , name)) 46 '=': (lambda k, v: {k: v.split(',')}),
47 }),
48 ]
49
50 try:
51 for spec in value.split():
52 for regx, ops in items:
53 m = re.search(regx, spec)
54 if m:
55 key, op, value = m.groups()
56 target.update(ops[op](key, value))
57 break
58 else:
59 raise ValueError
60 except (KeyError, ValueError):
61 raise Exception(
62 "Unknown target specifier '{}' in file '{}'".format(spec, name))
63
47 return target 64 return target
48 65
49 66
50 def _parse_notification(data, name): 67 def _parse_notification(data, name):
51 notification = {'id': name, 'severity': 'information', 'message': {}, 'title ': {}} 68 notification = {'id': name, 'severity': 'information', 'message': {}, 'title ': {}}
52 current = notification 69 current = notification
53 70
54 for line in data: 71 for line in data:
55 if not re.search(r'\S', line): 72 if not re.search(r'\S', line):
56 continue 73 continue
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 if not 'inactive' in notification: 148 if not 'inactive' in notification:
132 current_time = datetime.datetime.now() 149 current_time = datetime.datetime.now()
133 start = notification.pop('start', current_time) 150 start = notification.pop('start', current_time)
134 end = notification.pop('end', current_time) 151 end = notification.pop('end', current_time)
135 if not start <= current_time <= end: 152 if not start <= current_time <= end:
136 notification['inactive'] = True 153 notification['inactive'] = True
137 notifications.append(notification) 154 notifications.append(notification)
138 except: 155 except:
139 traceback.print_exc() 156 traceback.print_exc()
140 return notifications 157 return notifications
OLDNEW
« no previous file with comments | « sitescripts/notifications/README.md ('k') | sitescripts/notifications/test/parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld