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

Side by Side Diff: abp/filters/parser.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: Refine the API and add documentation Created May 9, 2019, 11:13 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
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
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 MATCH_CASE = 'match-case' 100 MATCH_CASE = 'match-case'
101 DOMAIN = 'domain' 101 DOMAIN = 'domain'
102 THIRD_PARTY = 'third-party' 102 THIRD_PARTY = 'third-party'
103 COLLAPSE = 'collapse' 103 COLLAPSE = 'collapse'
104 SITEKEY = 'sitekey' 104 SITEKEY = 'sitekey'
105 DONOTTRACK = 'donottrack' 105 DONOTTRACK = 'donottrack'
106 CSP = 'csp' 106 CSP = 'csp'
107 REWRITE = 'rewrite' 107 REWRITE = 'rewrite'
108 108
109 109
110 def _option_list_to_dict(options):
Vasily Kuznetsov 2019/05/09 11:18:59 These two functions were moved from `rpy.py` to pr
111 """Recursively parse filter options into dicts.
112
113 Parameters
114 ----------
115 options: A list of tuples
116 The filter options
117
118 Returns
119 -------
120 dict
121 The resulting dictionary
122
123 """
124 result = dict(options)
125 if 'domain' in result:
126 result['domain'] = _option_list_to_dict(result['domain'])
127
128 return result
129
130
131 def _to_dict(line):
132 """Convert a parsed filter list line from a namedtuple to a dict.
133
134 Parameters
135 ----------
136 line: namedtuple
137 The parsed filter.
138
139 Returns
140 -------
141 dict
142 The resulting dictionary
143
144 """
145 result = dict(line._asdict())
146 if 'options' in result:
147 result['options'] = _option_list_to_dict(result['options'])
148
149 result['type'] = line.__class__.__name__
150
151 return result
152
153
110 def _line_type(name, field_names, format_string): 154 def _line_type(name, field_names, format_string):
111 """Define a line type. 155 """Define a line type.
112 156
113 Parameters 157 Parameters
114 ---------- 158 ----------
115 name: str 159 name: str
116 The name of the line type to define. 160 The name of the line type to define.
117 field_names: str or list 161 field_names: str or list
118 A sequence of field names or one space-separated string that contains 162 A sequence of field names or one space-separated string that contains
119 all field names. 163 all field names.
120 format_string: str 164 format_string: str
121 A format specifier for converting this line type back to string 165 A format specifier for converting this line type back to string
122 representation. 166 representation.
123 167
124 Returns 168 Returns
125 ------- 169 -------
126 class 170 class
127 Class created with `namedtuple` that has `.type` set to lowercased 171 Class created with `namedtuple` that has `.type` set to lowercased
128 `name` and supports conversion back to string with `.to_string()` 172 `name` and supports conversion back to string with `.to_string()`
129 method. 173 method.
130 174
131 """ 175 """
132 lt = namedtuple(name, field_names) 176 lt = namedtuple(name, field_names)
133 lt.type = name.lower() 177 lt.type = name.lower()
134 lt.to_string = lambda self: format_string.format(self) 178 lt.to_string = lambda self: format_string.format(self)
179 lt.to_dict = _to_dict
135 return lt 180 return lt
136 181
137 182
138 Header = _line_type('Header', 'version', '[{.version}]') 183 Header = _line_type('Header', 'version', '[{.version}]')
139 EmptyLine = _line_type('EmptyLine', '', '') 184 EmptyLine = _line_type('EmptyLine', '', '')
140 Comment = _line_type('Comment', 'text', '! {.text}') 185 Comment = _line_type('Comment', 'text', '! {.text}')
141 Metadata = _line_type('Metadata', 'key value', '! {0.key}: {0.value}') 186 Metadata = _line_type('Metadata', 'key value', '! {0.key}: {0.value}')
142 Filter = _line_type('Filter', 'text selector action options', '{.text}') 187 Filter = _line_type('Filter', 'text selector action options', '{.text}')
143 Include = _line_type('Include', 'target', '%include {0.target}%') 188 Include = _line_type('Include', 'target', '%include {0.target}%')
144 189
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 for line in lines: 383 for line in lines:
339 parsed_line = parse_line(line, position) 384 parsed_line = parse_line(line, position)
340 yield parsed_line 385 yield parsed_line
341 386
342 if position != 'body' and parsed_line.type in {'header', 'metadata'}: 387 if position != 'body' and parsed_line.type in {'header', 'metadata'}:
343 # Continue parsing metadata until it's over... 388 # Continue parsing metadata until it's over...
344 position = 'metadata' 389 position = 'metadata'
345 else: 390 else:
346 # ...then switch to parsing the body. 391 # ...then switch to parsing the body.
347 position = 'body' 392 position = 'body'
OLDNEW

Powered by Google App Engine
This is Rietveld