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

Unified Diff: abp/filters/diff_script.py

Issue 29922555: Issue 7059 - Modify fldiff so it can handle multiple files (Closed) Base URL: https://hg.adblockplus.org/python-abp
Patch Set: Address comments on PS1 Created Oct. 26, 2018, 9:40 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
Index: abp/filters/diff_script.py
===================================================================
--- a/abp/filters/diff_script.py
+++ b/abp/filters/diff_script.py
@@ -20,41 +20,58 @@
import argparse
import io
import sys
+import os
from .renderer import render_diff
+from .parser import parse_filterlist
__all__ = ['main']
-def parse_args():
+class MissingFilterListError(Exception):
+ """Error in finding a latest filter list, or base filter lists."""
+
+
+def _get_version(filterlist):
+ for line in parse_filterlist(filterlist):
+ if line.type == 'header':
+ base = line.version
+ if line.type == 'metadata' and line.key == 'Version':
+ return line.value
+ raise MissingFilterListError('Unable to find Version in [{}]'.format(base))
Vasily Kuznetsov 2018/10/29 16:31:48 Wouldn't it be more helpful to give filename in th
rhowell 2018/10/29 20:01:40 Yeah. With this (old) method, it returns the heade
+
+
+def parse_args(args_in=None):
parser = argparse.ArgumentParser(description='Render a filter list diff.')
- parser.add_argument(
- 'base', help='the older filter list that needs to be updated',
- nargs='?')
- parser.add_argument(
- 'latest', help='the most recent version of the filter list',
- nargs='?')
- parser.add_argument(
- 'outfile', help='output file for filter list diff',
- default='-', nargs='?')
- return parser.parse_args()
+ parser.add_argument('latest',
+ help='The most recent version of the filter list')
+ parser.add_argument('-o', '--output_dir', default=os.getcwd(),
+ help='The directory to write the diffs to')
+ parser.add_argument('base_files', nargs='+',
+ help='One or more archived filter lists')
+ return parser.parse_args(args_in)
-def main():
+def main(args_in=None):
"""Entry point for the diff rendering script (fldiff)."""
- args = parse_args()
+ arg = parse_args(args_in)
- with io.open(args.base, 'r', encoding='utf-8') as base, \
- io.open(args.latest, 'r', encoding='utf-8') as latest:
+ with io.open(arg.latest, 'r', encoding='utf8') as latest_list:
+ latest = latest_list.readlines()
- lines = render_diff(base, latest)
- if args.outfile == '-':
- outfile = io.open(sys.stdout.fileno(), 'w',
- closefd=False,
- encoding=sys.stdout.encoding or 'utf-8')
- else:
- outfile = io.open(args.outfile, 'w', encoding='utf-8')
+ for base_file in arg.base_files:
+ with io.open(base_file, 'r', encoding='utf8') as base_file:
+ base = base_file.readlines()
+ lines = render_diff(base, latest)
+ try:
+ version = _get_version(base)
+ except MissingFilterListError as exc:
+ sys.exit(exc)
- with outfile:
- for line in lines:
- print(line, file=outfile)
+ # TODO: After removing the dict test, this .strip() can go
+ outfile = os.path.join(arg.output_dir.strip(),
+ 'diff{}.txt'.format(version))
+
+ with io.open(outfile, 'w', encoding='utf-8') as out_fp:
+ for line in lines:
+ out_fp.write(line + '\n')
« no previous file with comments | « README.md ('k') | tests/test_diff_script.py » ('j') | tests/test_diff_script.py » ('J')

Powered by Google App Engine
This is Rietveld