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

Delta Between Two Patch Sets: abp/filters/parser.py

Issue 29465720: Issue 4970 - Document the library API of python-abp (Closed)
Left Patch Set: Improve the docstrings and help() behavior, shorten the README, add Development documentation and a… Created Oct. 10, 2017, 4:25 p.m.
Right Patch Set: Rebase to match the new master and retouche the docstrings. Created Oct. 24, 2017, 4:06 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
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-2017 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 #
(...skipping 19 matching lines...) Expand all
32 32
33 class ParseError(Exception): 33 class ParseError(Exception):
34 """Exception thrown by the parser when it encounters invalid input. 34 """Exception thrown by the parser when it encounters invalid input.
35 35
36 Parameters 36 Parameters
37 ---------- 37 ----------
38 error : str 38 error : str
39 Description of the error. 39 Description of the error.
40 text : str 40 text : str
41 The source text that caused an error. 41 The source text that caused an error.
42
Vasily Kuznetsov 2017/10/24 16:11:00 pep8-docstrings demands an empty line at the end o
42 """ 43 """
43 44
44 def __init__(self, error, text): 45 def __init__(self, error, text):
45 Exception.__init__(self, '{} in "{}"'.format(error, text)) 46 Exception.__init__(self, '{} in "{}"'.format(error, text))
46 self.text = text 47 self.text = text
47 self.error = error 48 self.error = error
48 49
49 50
50 # Constants related to filters (see https://adblockplus.org/filters). 51 # Constants related to filters (see https://adblockplus.org/filters).
51 class SELECTOR_TYPE: # flake8: noqa (this is a namespace of constants). 52 class SELECTOR_TYPE: # flake8: noqa (this is a namespace of constants).
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 format_string: str 115 format_string: str
115 A format specifier for converting this line type back to string 116 A format specifier for converting this line type back to string
116 representation. 117 representation.
117 118
118 Returns 119 Returns
119 ------- 120 -------
120 class 121 class
121 Class created with `namedtuple` that has `.type` set to lowercased 122 Class created with `namedtuple` that has `.type` set to lowercased
122 `name` and supports conversion back to string with `.to_string()` 123 `name` and supports conversion back to string with `.to_string()`
123 method. 124 method.
125
124 """ 126 """
125 lt = namedtuple(name, field_names) 127 lt = namedtuple(name, field_names)
126 lt.type = name.lower() 128 lt.type = name.lower()
127 lt.to_string = lambda self: format_string.format(self) 129 lt.to_string = lambda self: format_string.format(self)
128 return lt 130 return lt
129 131
130 132
131 Header = _line_type('Header', 'version', '[{.version}]') 133 Header = _line_type('Header', 'version', '[{.version}]')
132 EmptyLine = _line_type('EmptyLine', '', '') 134 EmptyLine = _line_type('EmptyLine', '', '')
133 Comment = _line_type('Comment', 'text', '! {.text}') 135 Comment = _line_type('Comment', 'text', '! {.text}')
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 242
241 Parameters 243 Parameters
242 ---------- 244 ----------
243 text : str 245 text : str
244 Filter to parse in ABP filter list syntax. 246 Filter to parse in ABP filter list syntax.
245 247
246 Returns 248 Returns
247 ------- 249 -------
248 namedtuple 250 namedtuple
249 Parsed filter. 251 Parsed filter.
252
250 """ 253 """
251 if '#' in text: 254 if '#' in text:
252 match = HIDING_FILTER_REGEXP.search(text) 255 match = HIDING_FILTER_REGEXP.search(text)
253 if match: 256 if match:
254 return _parse_hiding_filter(text, *match.groups()) 257 return _parse_hiding_filter(text, *match.groups())
255 return _parse_blocking_filter(text) 258 return _parse_blocking_filter(text)
256 259
257 260
258 def parse_line(line_text): 261 def parse_line(line_text):
259 """Parse one line of a filter list. 262 """Parse one line of a filter list.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 303
301 Returns 304 Returns
302 ------- 305 -------
303 iterator of namedtuple 306 iterator of namedtuple
304 Parsed lines of the filter list. 307 Parsed lines of the filter list.
305 308
306 Raises 309 Raises
307 ------ 310 ------
308 ParseError 311 ParseError
309 Thrown during iteration for invalid filter list lines. 312 Thrown during iteration for invalid filter list lines.
313 TypeError
Vasily Kuznetsov 2017/10/24 16:11:00 As suggested by Matze, this error is easy to fores
314 If `lines` is not iterable.
315
310 """ 316 """
311 for line in lines: 317 for line in lines:
312 yield parse_line(line) 318 yield parse_line(line)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld