Index: abp/filters/diff_script.py |
=================================================================== |
--- a/abp/filters/diff_script.py |
+++ b/abp/filters/diff_script.py |
@@ -20,41 +20,55 @@ |
import argparse |
import io |
import sys |
+import os.path |
Vasily Kuznetsov
2018/10/24 11:30:09
You can actually just `import os` here and `os.pat
rhowell
2018/10/26 21:42:21
Done.
|
+import re |
from .renderer import render_diff |
__all__ = ['main'] |
-def parse_args(): |
+class MissingFilterListError(Exception): |
+ """Error in finding a latest filter list, or base filter lists.""" |
+ |
+ |
+def parse_args(args_in=None): |
parser = argparse.ArgumentParser(description='Render a filter list diff.') |
parser.add_argument( |
Vasily Kuznetsov
2018/10/24 11:30:09
The formatting of this statement is different from
rhowell
2018/10/26 21:42:19
Done.
|
- 'base', help='the older filter list that needs to be updated', |
- nargs='?') |
+ '-o', '--output_dir', help='The directory to write the diffs to', |
+ default='-', nargs=1) |
Vasily Kuznetsov
2018/10/24 11:30:09
Since -o is now a directory, it can't default to `
rhowell
2018/10/26 21:42:18
Is `os.getcwd()` the best way to do that? It makes
|
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() |
+ '-l', '--latest', help='The most recent version of the filter list', |
+ nargs=1) |
+ return parser.parse_known_args(args_in) |
Vasily Kuznetsov
2018/10/24 11:30:09
Wouldn't it be better to add the positional argume
rhowell
2018/10/26 21:42:19
Done.
|
-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: |
+ try: |
+ if arg[0].latest is None: |
+ raise MissingFilterListError('The --latest list is required') |
Vasily Kuznetsov
2018/10/24 11:30:08
This would be better handled by the arguments pars
rhowell
2018/10/26 21:42:19
Yeah, using a positional argument makes sense, and
|
+ with io.open(arg[0].latest[0].strip(), 'r', |
+ encoding='utf8') as latest_list: |
+ latest = latest_list.readlines() |
+ if len(arg[1]) == 0: |
+ raise MissingFilterListError('Archived lists are required') |
Vasily Kuznetsov
2018/10/24 11:30:09
This should also be handled by the arguments parse
rhowell
2018/10/26 21:42:18
Done.
|
+ for base_file in arg[1]: |
+ with io.open(base_file, 'r', encoding='utf8') as base: |
+ out_dir = arg[0].output_dir[0].strip() |
Vasily Kuznetsov
2018/10/24 11:30:08
Do you think we need to strip the provided output
rhowell
2018/10/26 21:42:20
Good point. For now, when testing with the diction
|
- 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') |
- |
- with outfile: |
- for line in lines: |
- print(line, file=outfile) |
+ lines = render_diff(base, latest) |
+ if out_dir == '-': |
+ for line in lines: |
+ sys.stdout.write(line + '\n') |
+ else: |
+ version = re.search(r'.*(\d+)\.txt', base_file).groups()[0] |
Vasily Kuznetsov
2018/10/24 11:30:08
I wonder if we can really depend on the version be
rhowell
2018/10/26 21:42:20
Yeah, I wasn't sure how reliable this would be. Do
|
+ outfile = os.path.join(out_dir, |
+ 'diff{}.txt'.format(version)) |
Vasily Kuznetsov
2018/10/24 11:30:08
The format of the output filename should probably
rhowell
2018/10/26 21:42:20
Interesting idea. What did you have in mind? Like,
|
+ with io.open(outfile, 'w', encoding='utf-8') as out_fp: |
+ for line in lines: |
+ out_fp.write(line + '\n') |
+ except MissingFilterListError as exc: |
+ sys.exit(exc) |