Index: abp/filters/renderer.py |
=================================================================== |
--- a/abp/filters/renderer.py |
+++ b/abp/filters/renderer.py |
@@ -24,7 +24,7 @@ |
from .parser import parse_filterlist, Comment, Metadata |
from .sources import NotFound |
-__all__ = ['IncludeError', 'MissingHeader', 'render_filterlist'] |
+__all__ = ['IncludeError', 'MissingHeader', 'render_filterlist', 'render_diff'] |
_logger = logging.getLogger(__name__) |
@@ -124,11 +124,12 @@ |
"""Remove duplicate metadata and headers.""" |
# Always remove checksum -- a checksum coming from a fragment |
# will not match for the rendered list. |
- seen = {'Checksum'} |
+ seen = {'checksum'} |
for i, line in enumerate(lines): |
if line.type == 'metadata': |
- if line.key not in seen: |
- seen.add(line.key) |
+ key = line.key.lower() |
+ if key not in seen: |
+ seen.add(key) |
yield line |
elif line.type == 'header': |
if i == 0: |
@@ -180,3 +181,49 @@ |
_validate]: |
lines = proc(lines) |
return lines |
+ |
+ |
+def _split_list_for_diff(list_in): |
+ """Split a filter list into metadata, keys, and rules.""" |
+ metadata = {} |
+ rules = set() |
+ for line in parse_filterlist(list_in): |
+ if line.type == 'metadata': |
+ metadata[line.key.lower()] = line |
+ elif line.type == 'filter': |
+ rules.add(line.to_string()) |
+ return metadata, rules |
+ |
+ |
+def render_diff(base, latest): |
+ """Return a diff between two filter lists. |
+ |
+ Parameters |
+ ---------- |
+ base : iterator of str |
+ The base (old) list that we want to update to latest. |
+ lastest : iterator of str |
+ The latest (most recent) list that we want to update to. |
+ |
+ Returns |
+ ------- |
+ iterable of str |
+ A diff between two lists (https://issues.adblockplus.org/ticket/6685) |
+ |
+ """ |
+ latest_md, latest_rules = _split_list_for_diff(latest) |
+ base_md, base_rules = _split_list_for_diff(base) |
Sebastian Noack
2018/08/29 23:46:26
Nit: Is it necessary to abbreviate metadata to md
rhowell
2018/08/30 16:23:30
Done.
|
+ add_fl = latest_rules - base_rules |
Sebastian Noack
2018/08/29 23:46:25
Nit: These expressions could be inlined below. Thi
rhowell
2018/08/30 16:23:30
Done.
|
+ remove_fl = base_rules - latest_rules |
+ |
+ yield '[Adblock Plus Diff]' |
+ for key, latest in latest_md.items(): |
+ base = base_md.get(key) |
+ if not base or base.value != latest.value: |
+ yield latest.to_string() |
+ for key in set(base_md) - set(latest_md): |
+ yield '! {}:'.format(base_md[key].key) |
+ for item in remove_fl: |
+ yield '- {}'.format(item) |
+ for item in add_fl: |
+ yield '+ {}'.format(item) |