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: Removed OrderedDict Created April 9, 2019, 12:46 a.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 abp.filters import parse_line 24 from abp.filters import parse_line
25 25
26 __all__ = ['line2dict'] 26 __all__ = ['line2dict']
27 27
28 28
29 def option_list_to_dict(options):
30 """Recursively parse filter options into dicts.
31
32 Parameters
33 ----------
34 options: A list of tuples
35 The filter options
36
37 Returns
38 -------
39 dict
40 The resulting dictionary
41
42 """
43 result = dict(options)
44 if 'domain' in result:
45 result['domain'] = option_list_to_dict(result['domain'])
46
47 return result
48
49
29 def tuple2dict(data): 50 def tuple2dict(data):
30 """Convert a parsed filter from a namedtuple to a dict. 51 """Convert a parsed filter from a namedtuple to a dict.
31 52
32 Parameters 53 Parameters
33 ---------- 54 ----------
34 data: namedtuple 55 data: namedtuple
35 The parsed filter. 56 The parsed filter.
36 57
37 Returns 58 Returns
38 ------- 59 -------
39 dict 60 dict
40 The resulting dictionary 61 The resulting dictionary
41 62
42 """ 63 """
43 result = dict(data._asdict()) 64 result = dict(data._asdict())
65 if 'options' in result:
66 result['options'] = option_list_to_dict(result['options'])
67
44 result['type'] = data.__class__.__name__ 68 result['type'] = data.__class__.__name__
45 69
46 return result 70 return result
47 71
48 72
49 def strings2utf8(data): 73 def strings2utf8(data):
50 """Convert strings in a data structure to utf8 byte strings. 74 """Convert strings in a data structure to utf8 byte strings.
51 75
52 Parameters 76 Parameters
53 ---------- 77 ----------
54 data: dict 78 data: dict
55 The data to convert. Can include nested dicts, lists and tuples. 79 The data to convert. Can include nested dicts, lists and tuples.
56 80
57 Returns 81 Returns
58 ------- 82 -------
59 dict 83 dict
60 With all strings encoded as unicode. 84 With all strings encoded as unicode.
61 85
62 """ 86 """
63 if isinstance(data, dict): 87 if isinstance(data, dict):
64 return {strings2utf8(k): strings2utf8(v) for k, v in data.items()} 88 return {strings2utf8(k): strings2utf8(v) for k, v in data.items()}
65 if isinstance(data, list): 89 if isinstance(data, list):
66 return [strings2utf8(v) for v in data] 90 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('')): 91 if isinstance(data, type('')):
70 # The condition is a Python 2/3 way of saying "unicode string". 92 # The condition is a Python 2/3 way of saying "unicode string".
71 return data.encode('utf-8') 93 return data.encode('utf-8')
72 return data 94 return data
73 95
74 96
75 def line2dict(text, mode='body'): 97 def line2dict(text, mode='body'):
76 """Convert a filterlist line to a dictionary. 98 """Convert a filterlist line to a dictionary.
77 99
78 All strings in the output dictionary will be UTF8 byte strings. This is 100 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 ------- 136 -------
115 list of dict 137 list of dict
116 With the parsing results and all strings converted to utf8 byte 138 With the parsing results and all strings converted to utf8 byte
117 strings. 139 strings.
118 140
119 """ 141 """
120 result = [] 142 result = []
121 for string in string_list: 143 for string in string_list:
122 result.append(line2dict(string, mode)) 144 result.append(line2dict(string, mode))
123 return result 145 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