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

Unified Diff: abp/filters/rpy.py

Issue 30031558: Issue 7391 - Let rpy recursively parse filter options to dicts (Closed) Base URL: https://hg.adblockplus.org/python-abp
Patch Set: Address comments on Patch Set 1 and bug fixes Created March 27, 2019, 9:24 p.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 | « no previous file | tests/test_rpy.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: abp/filters/rpy.py
===================================================================
--- a/abp/filters/rpy.py
+++ b/abp/filters/rpy.py
@@ -21,11 +21,34 @@
from __future__ import unicode_literals
+from collections import OrderedDict
+
from abp.filters import parse_line
__all__ = ['line2dict']
+def option_list_to_dict(options):
+ """Recursively parse filter options into dicts.
+
+ Parameters
+ ----------
+ options: A list of tuples
+ The filter options
+
+ Returns
+ -------
+ dict
+ The resulting dictionary
+
+ """
+ result = OrderedDict(options)
+ if 'domain' in result:
+ result['domain'] = option_list_to_dict(result['domain'])
+
+ return result
+
+
def tuple2dict(data):
"""Convert a parsed filter from a namedtuple to a dict.
@@ -40,7 +63,10 @@
The resulting dictionary
"""
- result = dict(data._asdict())
+ result = OrderedDict(data._asdict())
+ if 'options' in result:
+ result['options'] = option_list_to_dict(result['options'])
+
result['type'] = data.__class__.__name__
return result
@@ -60,12 +86,13 @@
With all strings encoded as unicode.
"""
+ if isinstance(data, OrderedDict):
+ return OrderedDict((strings2utf8(k), strings2utf8(v)) for k, v in
Vasily Kuznetsov 2019/04/02 17:36:22 Nit: it would be better to break the string before
rhowell 2019/04/09 00:47:39 Looks like we might not need these lines anyway. :
+ data.items())
if isinstance(data, dict):
return {strings2utf8(k): strings2utf8(v) for k, v in data.items()}
if isinstance(data, list):
return [strings2utf8(v) for v in data]
- if isinstance(data, tuple):
- return tuple(strings2utf8(v) for v in data)
if isinstance(data, type('')):
# The condition is a Python 2/3 way of saying "unicode string".
return data.encode('utf-8')
« no previous file with comments | « no previous file | tests/test_rpy.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld