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 """Helper classes that handle IO for filter list parsing and rendering.""" | 16 """Helper classes that handle IO for filter list parsing and rendering.""" |
17 | 17 |
18 import io | 18 import io |
19 from os import path | 19 from os import path |
| 20 import sys |
20 | 21 |
21 try: | 22 try: |
22 from urllib2 import urlopen, HTTPError | 23 from urllib2 import urlopen, HTTPError |
23 except ImportError: # The module was renamed in Python 3. | 24 except ImportError: # The module was renamed in Python 3. |
24 from urllib.request import urlopen | 25 from urllib.request import urlopen |
25 from urllib.error import HTTPError | 26 from urllib.error import HTTPError |
26 | 27 |
27 __all__ = ['NotFound', 'FSSource', 'TopSource', 'WebSource'] | 28 __all__ = ['NotFound', 'FSSource', 'TopSource', 'WebSource'] |
28 | 29 |
29 | 30 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 """ | 100 """ |
100 | 101 |
101 is_inheritable = False | 102 is_inheritable = False |
102 | 103 |
103 def __init__(self, encoding='utf-8'): | 104 def __init__(self, encoding='utf-8'): |
104 super(TopSource, self).__init__('.', encoding) | 105 super(TopSource, self).__init__('.', encoding) |
105 | 106 |
106 def _resolve_path(self, path_in_source): | 107 def _resolve_path(self, path_in_source): |
107 return path_in_source | 108 return path_in_source |
108 | 109 |
| 110 def get(self, path_in_source): |
| 111 """Read the data. Handles stdin, on top of file input. |
| 112 |
| 113 Parameters |
| 114 ---------- |
| 115 path_in_source : str |
| 116 Path to the file inside of source or '-' for stdin. |
| 117 |
| 118 Returns |
| 119 ------- |
| 120 generator or str |
| 121 Lines in the file/ from stdin. |
| 122 |
| 123 """ |
| 124 if path_in_source == '-': |
| 125 lines = sys.stdin.readlines() |
| 126 for line in lines: |
| 127 yield line.rstrip('\n') |
| 128 else: |
| 129 lines = super(TopSource, self).get(path_in_source) |
| 130 for line in lines: |
| 131 yield line |
| 132 |
109 | 133 |
110 class WebSource(object): | 134 class WebSource(object): |
111 """Handler for http or https. | 135 """Handler for http or https. |
112 | 136 |
113 Parameters | 137 Parameters |
114 ---------- | 138 ---------- |
115 protocol : str | 139 protocol : str |
116 Protocol to use: "http" or "https". | 140 Protocol to use: "http" or "https". |
117 default_encoding : str | 141 default_encoding : str |
118 Encoding to use when the server doesn't specify it (default: utf-8). | 142 Encoding to use when the server doesn't specify it (default: utf-8). |
(...skipping 29 matching lines...) Expand all Loading... |
148 get_param = (getattr(info, 'get_param', None) or | 172 get_param = (getattr(info, 'get_param', None) or |
149 getattr(info, 'getparam', None)) | 173 getattr(info, 'getparam', None)) |
150 encoding = get_param('charset') or self.default_encoding | 174 encoding = get_param('charset') or self.default_encoding |
151 for line in response: | 175 for line in response: |
152 yield line.decode(encoding).rstrip() | 176 yield line.decode(encoding).rstrip() |
153 except HTTPError as err: | 177 except HTTPError as err: |
154 if err.code == 404: | 178 if err.code == 404: |
155 raise NotFound("HTTP 404 Not found: '{}:{}'" | 179 raise NotFound("HTTP 404 Not found: '{}:{}'" |
156 .format(self.protocol, path_in_source)) | 180 .format(self.protocol, path_in_source)) |
157 raise err | 181 raise err |
OLD | NEW |