OLD | NEW |
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 # flake8: noqa | 16 """Tools for working with Adblock Plus filter lists. |
17 from .parser import * | 17 |
18 from .renderer import * | 18 This is a library for parsing and rendering filter lists that are used by |
19 from .sources import * | 19 adblocking software to determine what content to block and what to let through. |
| 20 |
| 21 Example |
| 22 ------- |
| 23 |
| 24 from abp.filters import parse_filterlist |
| 25 |
| 26 with open(fl_path, 'rt') as fl_file: |
| 27 for line in parse_filterlist(fl_file): |
| 28 if line.type == 'filter': |
| 29 print('Filter:' + line.text) |
| 30 print('| Selector: {0[type]}:{0[value]}'.format(line.selector)) |
| 31 print('| Action: ' + line.action) |
| 32 for key, value in line.options: |
| 33 print('| Option[{}]: {}'.format(key, value)) |
| 34 |
| 35 Exported members |
| 36 ---------------- |
| 37 |
| 38 Functions: |
| 39 |
| 40 - parse_filterlist - Parse a filter list from an iterable of strings. |
| 41 - parse_line - Parse one line of a filter list. |
| 42 - render_filterlist - Combine filter list fragments into a filter list. |
| 43 |
| 44 Filter list fragment sources for filter list rendering: |
| 45 |
| 46 - WebSource - loads fragments from the web. |
| 47 - FSSource - loads fragments from a directory on the filesystem. |
| 48 - TopSource - a specialized FSSource that represents current directory and |
| 49 should be used as the starting source of render_filterlist. |
| 50 |
| 51 Exceptions that thrown by the functions in this module: |
| 52 |
| 53 - ParseError - thrown by the parser when invalid input is encountered. |
| 54 - IncludeError - thrown by the renderer when an include instruction cannot |
| 55 be processed. |
| 56 - MissingHeader - thrown by the renderer when the output doesn't start with a |
| 57 header. |
| 58 |
| 59 Constants for code that works with filter lists: |
| 60 |
| 61 - SELECTOR_TYPE - Namespace for constants that determine how the filter |
| 62 matches content (for example: SELECTOR_TYPE.CSS): |
| 63 |
| 64 - URL_PATTERN - Match URL against a pattern (see |
| 65 https://adblockplus.org/filters#basic), |
| 66 - URL_REGEXP - Match URL against a regular expression, |
| 67 - CSS - Select elements via a CSS selector, |
| 68 - XCSS - CSS selector with extensions (to emulate CSS4), |
| 69 - ABP_SIMPLE - Deprecated simplified element selection syntax. |
| 70 |
| 71 - FILTER_ACTION - Namespace for constants that determine what the filter does |
| 72 with selected content (for example: FILTER_ACTION.BLOCK): |
| 73 |
| 74 - BLOCK - Block the request, |
| 75 - ALLOW - Allow the request (even if blocked by other filters), |
| 76 - HIDE - Hide selected element, |
| 77 - SHOW - Show selected element (even if hidden by other filters). |
| 78 |
| 79 - FILTER_OPTION - Namespace for filter option constants (for example |
| 80 FILTER_OPTION.IMAGE). See https://adblockplus.org/filters#options for the |
| 81 full list of options. |
| 82 |
| 83 See docstrings of module members for further information. |
| 84 |
| 85 Notes |
| 86 ----- |
| 87 `str` in function and method signatures always means a unicode string (Python3 |
| 88 meaning of `str`). |
| 89 |
| 90 """ |
| 91 |
| 92 from .parser import ( |
| 93 FILTER_ACTION, |
| 94 FILTER_OPTION, |
| 95 SELECTOR_TYPE, |
| 96 ParseError, |
| 97 parse_filterlist, |
| 98 parse_line, |
| 99 ) |
| 100 from .renderer import ( |
| 101 IncludeError, |
| 102 MissingHeader, |
| 103 render_filterlist, |
| 104 ) |
| 105 from .sources import ( |
| 106 FSSource, |
| 107 TopSource, |
| 108 WebSource, |
| 109 ) |
| 110 |
| 111 __all__ = [ |
| 112 # Constants |
| 113 'FILTER_ACTION', |
| 114 'FILTER_OPTION', |
| 115 'SELECTOR_TYPE', |
| 116 # Exceptions |
| 117 'ParseError', |
| 118 'IncludeError', |
| 119 'MissingHeader', |
| 120 # File sources |
| 121 'FSSource', |
| 122 'TopSource', |
| 123 'WebSource', |
| 124 # Functions |
| 125 'parse_filterlist', |
| 126 'parse_line', |
| 127 'render_filterlist', |
| 128 ] |
OLD | NEW |