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: Created March 21, 2019, 11:12 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 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 parse_options(options): 29 def option_list_to_dict(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.
30 """Recursively parse filter options into dicts. 30 """Recursively parse filter options into dicts.
31 31
32 Parameters 32 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.
33 ---------- 33 ----------
34 options: A list of tuples or namedtuples 34 options: A list of tuples
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.
35 The filter options 35 The filter options
36 36
37 Returns 37 Returns
38 ------- 38 -------
39 dict 39 dict
40 The resulting dictionary 40 The resulting dictionary
41 41
42 """ 42 """
43 result = dict(options) 43 result = dict(options)
44 if 'domain' in result: 44 if 'domain' in result:
45 result['domain'] = parse_options(result.get('domain')) 45 result['domain'] = option_list_to_dict(result['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
46 46
47 return result 47 return result
48 48
49 49
50 def tuple2dict(data): 50 def tuple2dict(data):
51 """Convert a parsed filter from a namedtuple to a dict. 51 """Convert a parsed filter from a namedtuple to a dict.
52 52
53 Parameters 53 Parameters
54 ---------- 54 ----------
55 data: namedtuple 55 data: namedtuple
56 The parsed filter. 56 The parsed filter.
57 57
58 Returns 58 Returns
59 ------- 59 -------
60 dict 60 dict
61 The resulting dictionary 61 The resulting dictionary
62 62
63 """ 63 """
64 result = {} 64 result = dict(data._asdict())
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
65 65 if 'options' in result:
66 for key in data._fields: 66 result['options'] = option_list_to_dict(result['options'])
67 name = key
68 value = getattr(data, key)
69 if 'options' in name:
70 result[name] = parse_options(value)
71 else:
72 result[name] = value
73 67
74 result['type'] = data.__class__.__name__ 68 result['type'] = data.__class__.__name__
75 69
76 return result 70 return result
77 71
78 72
79 def strings2utf8(data): 73 def strings2utf8(data):
80 """Convert strings in a data structure to utf8 byte strings. 74 """Convert strings in a data structure to utf8 byte strings.
81 75
82 Parameters 76 Parameters
83 ---------- 77 ----------
84 data: dict 78 data: dict
85 The data to convert. Can include nested dicts, lists and tuples. 79 The data to convert. Can include nested dicts, lists and tuples.
86 80
87 Returns 81 Returns
88 ------- 82 -------
89 dict 83 dict
90 With all strings encoded as unicode. 84 With all strings encoded as unicode.
91 85
92 """ 86 """
93 if isinstance(data, dict): 87 if isinstance(data, dict):
94 return {strings2utf8(k): strings2utf8(v) for k, v in data.items()} 88 return {strings2utf8(k): strings2utf8(v) for k, v in data.items()}
89 if isinstance(data, list):
90 return [strings2utf8(v) for v in data]
95 if isinstance(data, type('')): 91 if isinstance(data, type('')):
96 # 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".
97 return data.encode('utf-8') 93 return data.encode('utf-8')
98 return data 94 return data
99 95
100 96
101 def line2dict(text, mode='body'): 97 def line2dict(text, mode='body'):
102 """Convert a filterlist line to a dictionary. 98 """Convert a filterlist line to a dictionary.
103 99
104 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
140 ------- 136 -------
141 list of dict 137 list of dict
142 With the parsing results and all strings converted to utf8 byte 138 With the parsing results and all strings converted to utf8 byte
143 strings. 139 strings.
144 140
145 """ 141 """
146 result = [] 142 result = []
147 for string in string_list: 143 for string in string_list:
148 result.append(line2dict(string, mode)) 144 result.append(line2dict(string, mode))
149 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