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-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 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 parsing and rendering.""" | 16 """Helper classes that handle IO for parsing and rendering.""" |
17 | 17 |
18 import codecs | 18 import io |
19 from os import path | 19 from os import path |
20 | 20 |
21 try: | 21 try: |
22 from urllib2 import urlopen, HTTPError | 22 from urllib2 import urlopen, HTTPError |
23 except ImportError: # The module was renamed in Python 3. | 23 except ImportError: # The module was renamed in Python 3. |
24 from urllib.request import urlopen | 24 from urllib.request import urlopen |
25 from urllib.error import HTTPError | 25 from urllib.error import HTTPError |
26 | 26 |
27 __all__ = ['FSSource', 'TopSource', 'WebSource', 'NotFound'] | 27 __all__ = ['FSSource', 'TopSource', 'WebSource', 'NotFound'] |
28 | 28 |
(...skipping 17 matching lines...) Expand all Loading... |
46 | 46 |
47 def __init__(self, root_path, encoding='utf-8'): | 47 def __init__(self, root_path, encoding='utf-8'): |
48 root_path = path.abspath(root_path) | 48 root_path = path.abspath(root_path) |
49 self.root_path = root_path | 49 self.root_path = root_path |
50 self.encoding = encoding | 50 self.encoding = encoding |
51 | 51 |
52 def resolve_path(self, path_in_source): | 52 def resolve_path(self, path_in_source): |
53 parts = path_in_source.split('/') | 53 parts = path_in_source.split('/') |
54 full_path = path.abspath(path.join(self.root_path, *parts)) | 54 full_path = path.abspath(path.join(self.root_path, *parts)) |
55 if not full_path.startswith(self.root_path): | 55 if not full_path.startswith(self.root_path): |
56 raise ValueError('Invalid path: \'{}\''.format(path_in_source)) | 56 raise ValueError("Invalid path: '{}'".format(path_in_source)) |
57 return full_path | 57 return full_path |
58 | 58 |
59 def get(self, path_in_source): | 59 def get(self, path_in_source): |
60 full_path = self.resolve_path(path_in_source) | 60 full_path = self.resolve_path(path_in_source) |
61 try: | 61 try: |
62 with codecs.open(full_path, encoding=self.encoding) as open_file: | 62 with io.open(full_path, encoding=self.encoding) as open_file: |
63 for line in open_file: | 63 for line in open_file: |
64 yield line.rstrip() | 64 yield line.rstrip() |
65 except IOError as exc: | 65 except IOError as exc: |
66 if exc.errno == 2: # No such file or directory. | 66 if exc.errno == 2: # No such file or directory. |
67 raise NotFound('File not found: \'{}\''.format(full_path)) | 67 raise NotFound("File not found: '{}'".format(full_path)) |
68 else: | 68 raise exc |
69 raise exc | |
70 | 69 |
71 | 70 |
72 class TopSource(FSSource): | 71 class TopSource(FSSource): |
73 """Current directory without path conversion. | 72 """Current directory without path conversion. |
74 | 73 |
75 Also supports absolute paths. This source is used for the top fragment. | 74 Also supports absolute paths. This source is used for the top fragment. |
76 | 75 |
77 :param encoding: Encoding to use for reading the files (default: utf-8). | 76 :param encoding: Encoding to use for reading the files (default: utf-8). |
78 """ | 77 """ |
79 | 78 |
(...skipping 27 matching lines...) Expand all Loading... |
107 info = response.info() | 106 info = response.info() |
108 # info.getparam became info.get_param in Python 3 so we'll | 107 # info.getparam became info.get_param in Python 3 so we'll |
109 # try both. | 108 # try both. |
110 get_param = (getattr(info, 'get_param', None) or | 109 get_param = (getattr(info, 'get_param', None) or |
111 getattr(info, 'getparam', None)) | 110 getattr(info, 'getparam', None)) |
112 encoding = get_param('charset') or self.default_encoding | 111 encoding = get_param('charset') or self.default_encoding |
113 for line in response: | 112 for line in response: |
114 yield line.decode(encoding).rstrip() | 113 yield line.decode(encoding).rstrip() |
115 except HTTPError as err: | 114 except HTTPError as err: |
116 if err.code == 404: | 115 if err.code == 404: |
117 raise NotFound('HTTP 404 Not found: \'{}:{}\'' | 116 raise NotFound("HTTP 404 Not found: '{}:{}'" |
118 .format(self.protocol, path_in_source)) | 117 .format(self.protocol, path_in_source)) |
119 else: | 118 raise err |
120 raise err | |
OLD | NEW |