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

Unified 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.
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/README.md ('k') | sitescripts/notifications/test/parser.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/notifications/parser.py
diff --git a/sitescripts/notifications/parser.py b/sitescripts/notifications/parser.py
index d8679b89b625495b593b2484b4b0cc6a27cc6329..33045408e90e96a42bcdc15d29d45c5427376b18 100644
--- a/sitescripts/notifications/parser.py
+++ b/sitescripts/notifications/parser.py
@@ -27,23 +27,40 @@ from sitescripts.utils import get_config
def _parse_targetspec(value, name):
target = {}
- for spec in value.split():
- known = False
- for parameter in ('extension', 'application', 'platform'):
- if spec.startswith(parameter + '='):
- target[parameter] = spec[len(parameter + '='):]
- known = True
- elif spec.startswith(parameter + 'Version>='):
- target[parameter + 'MinVersion'] = spec[len(parameter + 'Version>='):]
- known = True
- elif spec.startswith(parameter + 'Version<='):
- target[parameter + 'MaxVersion'] = spec[len(parameter + 'Version<='):]
- known = True
- elif spec.startswith(parameter + 'Version='):
- target[parameter + 'MinVersion'] = target[parameter + 'MaxVersion'] = spec[len(parameter + 'Version='):]
- known = True
- if not known:
- raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name))
+
+ items = [
+ (r'^(extension|application|platform)(=)(.+)$', {
+ '=': (lambda k, v: {k: v}),
+ }),
+ (r'^(extension|application|platform)Version(=|\>=|\<=)(.+)$', {
+ '>=': (lambda k, v: {k + 'MinVersion': v}),
+ '<=': (lambda k, v: {k + 'MaxVersion': v}),
+ '=': (lambda k, v: {k + 'MinVersion': v, k + 'MaxVersion': v}),
+ }),
+ (r'^(blockedTotal)(=|\>=|\<=)(\d+)$', {
+ '>=': (lambda k, v: {k + 'Min': int(v)}),
+ '<=': (lambda k, v: {k + 'Max': int(v)}),
+ '=': (lambda k, v: {k + 'Min': int(v), k + 'Max': int(v)}),
+ }),
+ (r'^(locales)(=)([\w\-,]+)$', {
+ '=': (lambda k, v: {k: v.split(',')}),
+ }),
+ ]
+
+ try:
+ for spec in value.split():
+ for regx, ops in items:
+ m = re.search(regx, spec)
+ if m:
+ key, op, value = m.groups()
+ target.update(ops[op](key, value))
+ break
+ else:
+ raise ValueError
+ except (KeyError, ValueError):
+ raise Exception(
+ "Unknown target specifier '{}' in file '{}'".format(spec, name))
+
return target
« 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