Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: abp/filters/renderer.py

Issue 29845767: Issue 6685 - Offer incremental filter list downloads (Closed) Base URL: https://hg.adblockplus.org/python-abp/
Patch Set: Address comments on PS5 Created Aug. 29, 2018, 9:43 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « abp/filters/parser.py ('k') | tests/test_differ.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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,51 @@
_validate]:
lines = proc(lines)
return lines
+
+
+def _split_list_for_diff(list_in):
+ filterlist, metadata, keys = set(), set(), set()
+ for line in parse_filterlist(list_in):
+ if line.type == 'metadata':
+ metadata.add(line.to_string())
+ keys.add(line.key.lower())
+ elif line.type == 'filter':
+ filterlist.add(line.to_string())
+ return filterlist, metadata, keys
+
+
+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_fl, latest_md, latest_keys = _split_list_for_diff(latest)
+ base_fl, base_md, base_keys = _split_list_for_diff(base)
+
+ new_md = latest_md - base_md
+ removed_keys = base_keys - latest_keys
+ add_fl = latest_fl - base_fl
+ remove_fl = base_fl - latest_fl
+
+ yield '[Adblock Plus Diff]'
+ for item in new_md:
+ yield item
+ 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
+ yield '! {}:'.format(key)
+ for item in remove_fl:
+ yield '- {}'.format(item)
+ for item in add_fl:
+ yield '+ {}'.format(item)
« no previous file with comments | « abp/filters/parser.py ('k') | tests/test_differ.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld