OLD | NEW |
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 |
(...skipping 12 matching lines...) Expand all Loading... |
23 from .sources import FSSource, TopSource, WebSource, NotFound | 23 from .sources import FSSource, TopSource, WebSource, NotFound |
24 from .renderer import render_filterlist, IncludeError, MissingHeader | 24 from .renderer import render_filterlist, IncludeError, MissingHeader |
25 | 25 |
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.') | 30 parser = argparse.ArgumentParser(description='Render a filter list.') |
31 parser.add_argument( | 31 parser.add_argument( |
32 'infile', help='top level filter list fragment from which the ' | 32 'infile', help='top level filter list fragment from which the ' |
33 'rendering starts') | 33 'rendering starts', default='-', nargs='?') |
34 parser.add_argument('outfile', help='output filter list file') | 34 parser.add_argument('outfile', help='output filter list file', |
| 35 default='-', nargs='?') |
35 parser.add_argument( | 36 parser.add_argument( |
36 '-i', '--include', action='append', default=[], metavar='NAME=PATH', | 37 '-i', '--include', action='append', default=[], metavar='NAME=PATH', |
37 help='define include path (could be given multiple times)') | 38 help='define include path (could be given multiple times)') |
38 parser.add_argument( | 39 parser.add_argument( |
39 '-v', '--verbose', action='store_true', default=False, | 40 '-v', '--verbose', action='store_true', default=False, |
40 help='log included files and URLs') | 41 help='log included files and URLs') |
41 return parser.parse_args() | 42 return parser.parse_args() |
42 | 43 |
43 | 44 |
44 def main(): | 45 def main(): |
45 """Entry point for the rendering script (flrender).""" | 46 """Entry point for the rendering script (flrender).""" |
46 sources = { | 47 sources = { |
47 'http': WebSource('http'), | 48 'http': WebSource('http'), |
48 'https': WebSource('https'), | 49 'https': WebSource('https'), |
49 } | 50 } |
50 args = parse_args() | 51 args = parse_args() |
51 | |
52 if args.verbose: | 52 if args.verbose: |
53 logging.basicConfig(level=logging.INFO, stream=sys.stderr, | 53 logging.basicConfig(level=logging.INFO, stream=sys.stderr, |
54 format='%(message)s') | 54 format='%(message)s') |
55 | 55 |
56 for include_path in args.include: | 56 for include_path in args.include: |
57 name, path = include_path.split('=', 1) | 57 name, path = include_path.split('=', 1) |
58 sources[name] = FSSource(path) | 58 sources[name] = FSSource(path) |
59 | 59 |
60 try: | 60 try: |
61 lines = render_filterlist(args.infile, sources, TopSource()) | 61 lines = render_filterlist(args.infile, sources, TopSource()) |
62 with io.open(args.outfile, 'w', encoding='utf-8') as out_fp: | 62 if args.outfile == '-': |
63 for line in lines: | 63 for line in lines: |
64 out_fp.write(line.to_string() + '\n') | 64 sys.stdout.write(line.to_string() + '\n') |
| 65 else: |
| 66 with io.open(args.outfile, 'w', encoding='utf-8') as out_fp: |
| 67 for line in lines: |
| 68 out_fp.write(line.to_string() + '\n') |
65 except (MissingHeader, NotFound, IncludeError) as exc: | 69 except (MissingHeader, NotFound, IncludeError) as exc: |
66 sys.exit(exc) | 70 sys.exit(exc) |
OLD | NEW |