 Issue 29884571:
  Issue 6945 - Add script to make filter list diffs  (Closed) 
  Base URL: https://hg.adblockplus.org/python-abp/
    
  
    Issue 29884571:
  Issue 6945 - Add script to make filter list diffs  (Closed) 
  Base URL: https://hg.adblockplus.org/python-abp/| Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 
| 2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH | 
| 3 # | 3 # | 
| 4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify | 
| 5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as | 
| 6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. | 
| 7 # | 7 # | 
| 8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, | 
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. | 
| 12 # | 12 # | 
| 13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License | 
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 
| 15 | 15 | 
| 16 """Command line script for rendering Adblock Plus filter list diffs.""" | 16 """Command line script for rendering Adblock Plus filter list diffs.""" | 
| 17 | 17 | 
| 18 from __future__ import print_function | |
| 19 | |
| 18 import argparse | 20 import argparse | 
| 19 import io | 21 import io | 
| 20 import sys | 22 import sys | 
| 21 | 23 | 
| 22 from collections import namedtuple | 24 from .renderer import render_diff | 
| 23 | 25 | 
| 24 from .sources import NotFound | |
| 25 from .renderer import render_diff | |
| 26 __all__ = ['main'] | 26 __all__ = ['main'] | 
| 27 | 27 | 
| 28 | 28 | 
| 29 def parse_args(): | 29 def parse_args(): | 
| 30 parser = argparse.ArgumentParser(description='Render a filter list diff.') | 30 parser = argparse.ArgumentParser(description='Render a filter list diff.') | 
| 31 parser.add_argument( | 31 parser.add_argument( | 
| 32 'base', help='the older filter list that needs to be updated', | 32 'base', help='the older filter list that needs to be updated', | 
| 33 default='-', nargs='?') | 33 nargs='?') | 
| 34 parser.add_argument( | 34 parser.add_argument( | 
| 35 'latest', help='the most recent version of the filter list', | 35 'latest', help='the most recent version of the filter list', | 
| 36 default='-', nargs='?') | 36 nargs='?') | 
| 37 parser.add_argument( | 37 parser.add_argument( | 
| 38 'outfile', help='output file for filter list diff', | 38 'outfile', help='output file for filter list diff', | 
| 39 default='-', nargs='?') | 39 default='-', nargs='?') | 
| 40 return parser.parse_args() | 40 return parser.parse_args() | 
| 41 | 41 | 
| 42 | 42 | 
| 43 def main(args_in=None): | 43 def main(): | 
| 44 """Entry point for the diff rendering script (diffrender).""" | 44 """Entry point for the diff rendering script (fldiff).""" | 
| 45 if args_in: | 45 args = parse_args() | 
| 46 Parse = namedtuple('Test', 'base, latest, outfile') | |
| 47 args = Parse(args_in['base'], args_in['latest'], args_in['outfile']) | |
| 48 else: | |
| 49 args = parse_args() | |
| 50 | 46 | 
| 51 base = open(args.base, 'r') | 47 with io.open(args.base, 'r', encoding='utf-8') as base, \ | 
| 52 latest = open(args.latest, 'r') | 48 io.open(args.latest, 'r', encoding='utf-8') as latest: | 
| 
Sebastian Noack
2018/09/18 16:56:12
Any reason you don't use io.open(..., encoding="ut
 
Sebastian Noack
2018/09/18 16:56:12
Please make sure all files are explicitly closed.
 
rhowell
2018/09/18 17:23:33
Done.
 | |
| 53 | 49 | 
| 54 try: | |
| 55 lines = render_diff(base, latest) | 50 lines = render_diff(base, latest) | 
| 56 if args.outfile == '-': | 51 if args.outfile == '-': | 
| 52 outfile = io.open(sys.stdout.fileno(), 'w', | |
| 53 closefd=False, | |
| 54 encoding=sys.stdout.encoding or 'utf-8') | |
| 55 else: | |
| 56 outfile = io.open(args.outfile, 'w', encoding='utf-8') | |
| 57 | |
| 58 with outfile: | |
| 57 for line in lines: | 59 for line in lines: | 
| 58 sys.stdout.write(line + '\n') | 60 print(line, file=outfile) | 
| 59 else: | |
| 60 open(args.outfile, 'w') | |
| 
rhowell
2018/09/18 16:01:54
This feels hacky..
 
Sebastian Noack
2018/09/18 16:56:12
Well, this is redundant. Why did you put it here?
 
rhowell
2018/09/18 17:23:33
Wow, you're right. I swore I was getting an error
 
Sebastian Noack
2018/09/18 19:13:18
Both, open(..., 'w') and io.open(..., 'w') create
 | |
| 61 with io.open(args.outfile, 'w', encoding='utf-8') as out_fp: | |
| 62 for line in lines: | |
| 63 out_fp.write(line + '\n') | |
| 64 except NotFound as exc: | |
| 65 sys.exit(exc) | |
| LEFT | RIGHT |