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: Created March 21, 2019, 11:12 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
@@ -26,6 +26,27 @@
__all__ = ['line2dict']
+def parse_options(options):
Vasily Kuznetsov 2019/03/22 12:17:04 Maybe we should change this name to something like
rhowell 2019/03/27 19:14:06 Done.
+ """Recursively parse filter options into dicts.
+
+ Parameters
Vasily Kuznetsov 2019/03/22 12:17:04 Nit: The indentation of this line seems to be off.
rhowell 2019/03/27 19:14:06 Done.
+ ----------
+ options: A list of tuples or namedtuples
Vasily Kuznetsov 2019/03/22 12:17:04 We're treating them as just tuples so we probably
rhowell 2019/03/27 19:14:07 Done.
+ The filter options
+
+ Returns
+ -------
+ dict
+ The resulting dictionary
+
+ """
+ result = dict(options)
+ if 'domain' in result:
+ result['domain'] = parse_options(result.get('domain'))
Vasily Kuznetsov 2019/03/22 12:17:04 Nit: `result.get('domain')` could be just `result[
Vasily Kuznetsov 2019/03/22 12:17:04 This conversion will lose the order of the domain
sporz 2019/03/25 17:30:03 Thank you Vasily for pointing this out - I wasn't
sporz 2019/03/25 17:30:03 Interesting choice to put this as a function on it
rhowell 2019/03/27 19:14:06 I ran into a few issues trying to call tuple2dict
rhowell 2019/03/27 19:14:07 I was able to implement this using OrderedDict and
+
+ return result
+
+
def tuple2dict(data):
"""Convert a parsed filter from a namedtuple to a dict.
@@ -40,7 +61,16 @@
The resulting dictionary
"""
- result = dict(data._asdict())
+ result = {}
Vasily Kuznetsov 2019/03/22 12:17:04 Maybe we just add `result['options'] = parse_optio
rhowell 2019/03/27 19:14:07 Yeah, good idea. We also need to check that 'optio
+
+ for key in data._fields:
+ name = key
+ value = getattr(data, key)
+ if 'options' in name:
+ result[name] = parse_options(value)
+ else:
+ result[name] = value
+
result['type'] = data.__class__.__name__
return result
@@ -62,10 +92,6 @@
"""
if isinstance(data, dict):
return {strings2utf8(k): strings2utf8(v) for k, v in data.items()}
- if isinstance(data, list):
Vasily Kuznetsov 2019/03/22 12:17:04 Unfortunately we can't remove the list branch here
rhowell 2019/03/27 19:14:06 Ah, gotcha. Since we didn't have a test using the
Vasily Kuznetsov 2019/04/02 17:36:21 Yeah, seems reasonable to keep it as a list, unles
- 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