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') |