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

Delta Between Two Patch Sets: 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
Left Patch Set: Address comments on Patch Set 1 and bug fixes Created March 27, 2019, 9:24 p.m.
Right Patch Set: Remove all OrderedDicts Created April 12, 2019, 6:30 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | tests/test_rpy.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
26 from abp.filters import parse_line 24 from abp.filters import parse_line
27 25
28 __all__ = ['line2dict'] 26 __all__ = ['line2dict']
29 27
30 28
31 def option_list_to_dict(options): 29 def option_list_to_dict(options):
32 """Recursively parse filter options into dicts. 30 """Recursively parse filter options into dicts.
33 31
34 Parameters 32 Parameters
35 ---------- 33 ----------
36 options: A list of tuples 34 options: A list of tuples
37 The filter options 35 The filter options
38 36
39 Returns 37 Returns
40 ------- 38 -------
41 dict 39 dict
42 The resulting dictionary 40 The resulting dictionary
43 41
44 """ 42 """
45 result = OrderedDict(options) 43 result = dict(options)
46 if 'domain' in result: 44 if 'domain' in result:
47 result['domain'] = option_list_to_dict(result['domain']) 45 result['domain'] = option_list_to_dict(result['domain'])
48 46
49 return result 47 return result
50 48
51 49
52 def tuple2dict(data): 50 def tuple2dict(data):
53 """Convert a parsed filter from a namedtuple to a dict. 51 """Convert a parsed filter from a namedtuple to a dict.
54 52
55 Parameters 53 Parameters
56 ---------- 54 ----------
57 data: namedtuple 55 data: namedtuple
58 The parsed filter. 56 The parsed filter.
59 57
60 Returns 58 Returns
61 ------- 59 -------
62 dict 60 dict
63 The resulting dictionary 61 The resulting dictionary
64 62
65 """ 63 """
66 result = OrderedDict(data._asdict()) 64 result = dict(data._asdict())
67 if 'options' in result: 65 if 'options' in result:
68 result['options'] = option_list_to_dict(result['options']) 66 result['options'] = option_list_to_dict(result['options'])
69 67
70 result['type'] = data.__class__.__name__ 68 result['type'] = data.__class__.__name__
71 69
72 return result 70 return result
73 71
74 72
75 def strings2utf8(data): 73 def strings2utf8(data):
76 """Convert strings in a data structure to utf8 byte strings. 74 """Convert strings in a data structure to utf8 byte strings.
77 75
78 Parameters 76 Parameters
79 ---------- 77 ----------
80 data: dict 78 data: dict
81 The data to convert. Can include nested dicts, lists and tuples. 79 The data to convert. Can include nested dicts, lists and tuples.
82 80
83 Returns 81 Returns
84 ------- 82 -------
85 dict 83 dict
86 With all strings encoded as unicode. 84 With all strings encoded as unicode.
87 85
88 """ 86 """
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())
92 if isinstance(data, dict): 87 if isinstance(data, dict):
93 return {strings2utf8(k): strings2utf8(v) for k, v in data.items()} 88 return {strings2utf8(k): strings2utf8(v) for k, v in data.items()}
94 if isinstance(data, list): 89 if isinstance(data, list):
95 return [strings2utf8(v) for v in data] 90 return [strings2utf8(v) for v in data]
96 if isinstance(data, type('')): 91 if isinstance(data, type('')):
97 # 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".
98 return data.encode('utf-8') 93 return data.encode('utf-8')
99 return data 94 return data
100 95
101 96
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 ------- 136 -------
142 list of dict 137 list of dict
143 With the parsing results and all strings converted to utf8 byte 138 With the parsing results and all strings converted to utf8 byte
144 strings. 139 strings.
145 140
146 """ 141 """
147 result = [] 142 result = []
148 for string in string_list: 143 for string in string_list:
149 result.append(line2dict(string, mode)) 144 result.append(line2dict(string, mode))
150 return result 145 return result
LEFTRIGHT
« no previous file | tests/test_rpy.py » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld