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

Side by Side 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.
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 | « no previous file | tests/test_rpy.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 Adblock Plus <https://adblockplus.org/>, 1 # This file is part of Adblock Plus <https://adblockplus.org/>,
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 """ 16 """
17 Functions for integrating with rPython. 17 Functions for integrating with rPython.
18 18
19 see: https://cran.r-project.org/web/packages/rPython/index.html 19 see: https://cran.r-project.org/web/packages/rPython/index.html
20 """ 20 """
21 21
22 from __future__ import unicode_literals 22 from __future__ import unicode_literals
23 23
24 from collections import OrderedDict
25
24 from abp.filters import parse_line 26 from abp.filters import parse_line
25 27
26 __all__ = ['line2dict'] 28 __all__ = ['line2dict']
27 29
28 30
31 def option_list_to_dict(options):
32 """Recursively parse filter options into dicts.
33
34 Parameters
35 ----------
36 options: A list of tuples
37 The filter options
38
39 Returns
40 -------
41 dict
42 The resulting dictionary
43
44 """
45 result = OrderedDict(options)
46 if 'domain' in result:
47 result['domain'] = option_list_to_dict(result['domain'])
48
49 return result
50
51
29 def tuple2dict(data): 52 def tuple2dict(data):
30 """Convert a parsed filter from a namedtuple to a dict. 53 """Convert a parsed filter from a namedtuple to a dict.
31 54
32 Parameters 55 Parameters
33 ---------- 56 ----------
34 data: namedtuple 57 data: namedtuple
35 The parsed filter. 58 The parsed filter.
36 59
37 Returns 60 Returns
38 ------- 61 -------
39 dict 62 dict
40 The resulting dictionary 63 The resulting dictionary
41 64
42 """ 65 """
43 result = dict(data._asdict()) 66 result = OrderedDict(data._asdict())
67 if 'options' in result:
68 result['options'] = option_list_to_dict(result['options'])
69
44 result['type'] = data.__class__.__name__ 70 result['type'] = data.__class__.__name__
45 71
46 return result 72 return result
47 73
48 74
49 def strings2utf8(data): 75 def strings2utf8(data):
50 """Convert strings in a data structure to utf8 byte strings. 76 """Convert strings in a data structure to utf8 byte strings.
51 77
52 Parameters 78 Parameters
53 ---------- 79 ----------
54 data: dict 80 data: dict
55 The data to convert. Can include nested dicts, lists and tuples. 81 The data to convert. Can include nested dicts, lists and tuples.
56 82
57 Returns 83 Returns
58 ------- 84 -------
59 dict 85 dict
60 With all strings encoded as unicode. 86 With all strings encoded as unicode.
61 87
62 """ 88 """
89 if isinstance(data, OrderedDict):
90 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. :
91 data.items())
63 if isinstance(data, dict): 92 if isinstance(data, dict):
64 return {strings2utf8(k): strings2utf8(v) for k, v in data.items()} 93 return {strings2utf8(k): strings2utf8(v) for k, v in data.items()}
65 if isinstance(data, list): 94 if isinstance(data, list):
66 return [strings2utf8(v) for v in data] 95 return [strings2utf8(v) for v in data]
67 if isinstance(data, tuple):
68 return tuple(strings2utf8(v) for v in data)
69 if isinstance(data, type('')): 96 if isinstance(data, type('')):
70 # The condition is a Python 2/3 way of saying "unicode string". 97 # The condition is a Python 2/3 way of saying "unicode string".
71 return data.encode('utf-8') 98 return data.encode('utf-8')
72 return data 99 return data
73 100
74 101
75 def line2dict(text, mode='body'): 102 def line2dict(text, mode='body'):
76 """Convert a filterlist line to a dictionary. 103 """Convert a filterlist line to a dictionary.
77 104
78 All strings in the output dictionary will be UTF8 byte strings. This is 105 All strings in the output dictionary will be UTF8 byte strings. This is
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 ------- 141 -------
115 list of dict 142 list of dict
116 With the parsing results and all strings converted to utf8 byte 143 With the parsing results and all strings converted to utf8 byte
117 strings. 144 strings.
118 145
119 """ 146 """
120 result = [] 147 result = []
121 for string in string_list: 148 for string in string_list:
122 result.append(line2dict(string, mode)) 149 result.append(line2dict(string, mode))
123 return result 150 return result
OLDNEW
« 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