Index: abp/filters/renderer.py |
=================================================================== |
--- a/abp/filters/renderer.py |
+++ b/abp/filters/renderer.py |
@@ -199,3 +199,41 @@ |
_insert_checksum, _validate]: |
lines = proc(lines) |
return lines |
+ |
+ |
+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.
|
+ """Return a diff between two filter lists.""" |
+ # Collect the special comments |
+ diff = ['[Adblock Plus Diff]\n'] |
+ latest_fl, latest_md, latest_keys = (set() for i in range(3)) |
+ base_fl, base_md, base_keys = (set() for i in range(3)) |
+ |
+ for line in parse_filterlist(latest.splitlines()): |
+ 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
|
+ latest_md.add(line.to_string()) |
+ latest_keys.add(line.key) |
+ elif line.type == 'filter': |
+ latest_fl.add(line.to_string()) |
+ |
+ # Get the diff between the rest of the lines |
+ 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.
|
+ if line.type == 'metadata' and 'Checksum' not in line.to_string(): |
+ base_md.add(line.to_string()) |
+ base_keys.add(line.key) |
+ elif line.type == 'filter': |
+ base_fl.add(line.to_string()) |
+ new_md = latest_md - base_md |
+ removed_keys = base_keys - latest_keys |
+ add_fl = latest_fl - base_fl |
+ remove_fl = base_fl - latest_fl |
+ for item in new_md: |
+ 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.
|
+ for key in removed_keys: |
+ # If a special comment has been removed, enter it with a blank value |
+ # so the client will set it back to the default value |
+ diff.append('! {}:\n'.format(key)) |
+ for item in add_fl: |
+ diff.append('+ {}\n'.format(item)) |
+ for item in remove_fl: |
+ diff.append('- {}\n'.format(item)) |
+ return ''.join(diff) |