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

Side by Side Diff: abp/filters/rpy.py

Issue 30053555: Issue 7471 - Add an API for working with blocks of filters (Closed) Base URL: https://hg.adblockplus.org/python-abp
Patch Set: Adjust the API in response to review comments Created May 9, 2019, 4:22 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 | « abp/filters/parser.py ('k') | tests/data/expected_blocks.json » ('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
50 def tuple2dict(data):
51 """Convert a parsed filter from a namedtuple to a dict.
52
53 Parameters
54 ----------
55 data: namedtuple
56 The parsed filter.
57
58 Returns
59 -------
60 dict
61 The resulting dictionary
62
63 """
64 result = dict(data._asdict())
65 if 'options' in result:
66 result['options'] = option_list_to_dict(result['options'])
67
68 result['type'] = data.__class__.__name__
69
70 return result
71
72
73 def strings2utf8(data): 29 def strings2utf8(data):
74 """Convert strings in a data structure to utf8 byte strings. 30 """Convert strings in a data structure to utf8 byte strings.
75 31
76 Parameters 32 Parameters
77 ---------- 33 ----------
78 data: dict 34 data: dict
79 The data to convert. Can include nested dicts, lists and tuples. 35 The data to convert. Can include nested dicts, lists and tuples.
80 36
81 Returns 37 Returns
82 ------- 38 -------
(...skipping 24 matching lines...) Expand all
107 mode: str 63 mode: str
108 Parsing mode (see `abp.filters.parser.parse_line`). 64 Parsing mode (see `abp.filters.parser.parse_line`).
109 65
110 Returns 66 Returns
111 ------- 67 -------
112 dict 68 dict
113 With the parsing results and all strings converted to utf8 byte 69 With the parsing results and all strings converted to utf8 byte
114 strings. 70 strings.
115 71
116 """ 72 """
117 return strings2utf8(tuple2dict(parse_line(text, mode))) 73 return strings2utf8(parse_line(text, mode).to_dict())
118 74
119 75
120 def lines2dicts(string_list, mode='body'): 76 def lines2dicts(string_list, mode='body'):
121 """Convert a list of filterlist strings to a dictionary. 77 """Convert a list of filterlist strings to a dictionary.
122 78
123 All strings in the output dictionary will be UTF8 byte strings. This is 79 All strings in the output dictionary will be UTF8 byte strings. This is
124 necessary to prevent unicode encoding errors in rPython conversion layer. 80 necessary to prevent unicode encoding errors in rPython conversion layer.
125 81
126 Parameters 82 Parameters
127 ---------- 83 ----------
128 string_list: iterable of str 84 string_list: iterable of str
129 Each string in the list can be an empty line, include instruction, or 85 Each string in the list can be an empty line, include instruction, or
130 filter. If the mode is 'start', headers and metadata can also be 86 filter. If the mode is 'start', headers and metadata can also be
131 parsed. 87 parsed.
132 mode: str 88 mode: str
133 Parsing mode (see `abp.filters.parser.parse_line`). 89 Parsing mode (see `abp.filters.parser.parse_line`).
134 90
135 Returns 91 Returns
136 ------- 92 -------
137 list of dict 93 list of dict
138 With the parsing results and all strings converted to utf8 byte 94 With the parsing results and all strings converted to utf8 byte
139 strings. 95 strings.
140 96
141 """ 97 """
142 result = [] 98 result = []
143 for string in string_list: 99 for string in string_list:
144 result.append(line2dict(string, mode)) 100 result.append(line2dict(string, mode))
145 return result 101 return result
OLDNEW
« no previous file with comments | « abp/filters/parser.py ('k') | tests/data/expected_blocks.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld