Left: | ||
Right: |
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 """Combine filter list fragments to produce filter lists.""" | 16 """Combine filter list fragments to produce filter lists.""" |
17 | 17 |
18 from __future__ import unicode_literals | 18 from __future__ import unicode_literals |
19 | 19 |
20 import base64 | 20 import base64 |
21 import hashlib | 21 import hashlib |
22 import itertools | 22 import itertools |
23 import logging | 23 import logging |
24 import time | 24 import time |
25 | 25 |
26 from .parser import parse_filterlist, Comment, Metadata | 26 from .parser import parse_filterlist, Comment, Metadata |
27 from .sources import NotFound | 27 from .sources import NotFound |
28 | 28 |
29 __all__ = ['IncludeError', 'MissingHeader', 'render_filterlist'] | 29 __all__ = ['IncludeError', 'MissingHeader', 'render_filterlist'] |
Vasily Kuznetsov
2018/08/17 10:30:54
render_diff() should probably become a public expo
rhowell
2018/08/20 18:21:27
Done.
| |
30 | 30 |
31 _logger = logging.getLogger(__name__) | 31 _logger = logging.getLogger(__name__) |
32 | 32 |
33 | 33 |
34 class IncludeError(Exception): | 34 class IncludeError(Exception): |
35 """Error in processing include instruction.""" | 35 """Error in processing include instruction.""" |
36 | 36 |
37 def __init__(self, error, stack): | 37 def __init__(self, error, stack): |
38 stack_str = ' from '.join(map("'{}'".format, reversed(stack))) | 38 stack_str = ' from '.join(map("'{}'".format, reversed(stack))) |
39 if stack_str: | 39 if stack_str: |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 lead to rendering an invalid filter list, so we immediately abort. | 192 lead to rendering an invalid filter list, so we immediately abort. |
193 | 193 |
194 """ | 194 """ |
195 _logger.info('Rendering: %s', name) | 195 _logger.info('Rendering: %s', name) |
196 lines, default_source = _get_and_parse_fragment(name, sources, top_source) | 196 lines, default_source = _get_and_parse_fragment(name, sources, top_source) |
197 lines = _process_includes(sources, default_source, [name], lines) | 197 lines = _process_includes(sources, default_source, [name], lines) |
198 for proc in [_process_timestamps, _insert_version, _remove_duplicates, | 198 for proc in [_process_timestamps, _insert_version, _remove_duplicates, |
199 _insert_checksum, _validate]: | 199 _insert_checksum, _validate]: |
200 lines = proc(lines) | 200 lines = proc(lines) |
201 return lines | 201 return lines |
202 | |
203 | |
204 def render_diff(base, latest): | |
Vasily Kuznetsov
2018/08/17 10:30:54
I think it makes more sense to make `base`, `lates
rhowell
2018/08/20 18:21:27
Done.
| |
205 """Return a diff between two filter lists.""" | |
206 # Collect the special comments | |
207 diff = ['[Adblock Plus Diff]\n'] | |
208 latest_fl, latest_md, latest_keys = (set() for i in range(3)) | |
209 base_fl, base_md, base_keys = (set() for i in range(3)) | |
210 | |
211 for line in parse_filterlist(latest.splitlines()): | |
212 if line.type == 'metadata' and 'Checksum' not in line.to_string(): | |
Vasily Kuznetsov
2018/08/17 10:30:53
Note: If this lands after the checksum patch, the
rhowell
2018/08/20 18:21:27
Are we guaranteed that no filter lists will be enc
Vasily Kuznetsov
2018/08/21 14:59:59
Hm. No, for now we're not guaranteed that there's
Sebastian Noack
2018/08/21 19:42:45
Isn't python-abp stripping checksums when generati
Sebastian Noack
2018/08/22 16:03:29
Moving the discussion from IRC over here:
16:34:4
| |
213 latest_md.add(line.to_string()) | |
214 latest_keys.add(line.key) | |
215 elif line.type == 'filter': | |
216 latest_fl.add(line.to_string()) | |
217 | |
218 # Get the diff between the rest of the lines | |
219 for line in parse_filterlist(base.splitlines()): | |
Vasily Kuznetsov
2018/08/17 10:30:53
It would be good to move this repeated piece of co
rhowell
2018/08/20 18:21:27
Done.
| |
220 if line.type == 'metadata' and 'Checksum' not in line.to_string(): | |
221 base_md.add(line.to_string()) | |
222 base_keys.add(line.key) | |
223 elif line.type == 'filter': | |
224 base_fl.add(line.to_string()) | |
225 new_md = latest_md - base_md | |
226 removed_keys = base_keys - latest_keys | |
227 add_fl = latest_fl - base_fl | |
228 remove_fl = base_fl - latest_fl | |
229 for item in new_md: | |
230 diff.append('{}\n'.format(item)) | |
Vasily Kuznetsov
2018/08/17 10:30:53
If we return an iterable of strings from this func
rhowell
2018/08/20 18:21:27
Done.
| |
231 for key in removed_keys: | |
232 # If a special comment has been removed, enter it with a blank value | |
233 # so the client will set it back to the default value | |
234 diff.append('! {}:\n'.format(key)) | |
235 for item in add_fl: | |
236 diff.append('+ {}\n'.format(item)) | |
237 for item in remove_fl: | |
238 diff.append('- {}\n'.format(item)) | |
239 return ''.join(diff) | |
OLD | NEW |