Mercurial > cgi-bin > hgweb.cgi > curlyq
comparison curlyq @ 3:091c03f1b2e8
Getting it working...
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 26 Dec 2019 19:54:45 -0800 |
parents | |
children | 7a83e82e65a6 |
comparison
equal
deleted
inserted
replaced
2:8884b0bf779d | 3:091c03f1b2e8 |
---|---|
1 #!/usr/bin/env python3 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 import os, sys | |
5 import argparse | |
6 import codecs | |
7 | |
8 from curlers import TextCurler, HtmlCurler | |
9 from workspace import Workspace, Bounds, Mapping, SegmentedView | |
10 from writer import CODECS_TO_NAME | |
11 | |
12 # V a r i a b l e s | |
13 | |
14 # Name invoked by | |
15 MYNAME = os.path.basename(sys.argv[0]) | |
16 | |
17 # Streams | |
18 input_fp = None | |
19 output_fp = None | |
20 | |
21 # F u n c t i o n s | |
22 | |
23 def normal(): | |
24 global input_fp, output_fp | |
25 with Workspace() as ws: | |
26 curler = TextCurler(ws) | |
27 while True: | |
28 line = input_fp.readline() | |
29 ws.write(line) | |
30 if line == "" or line == "\n": | |
31 curler.feed() | |
32 output_fp.write(ws.getvalue()) | |
33 ws.clear() | |
34 if line == "": | |
35 break | |
36 | |
37 def flowed(): | |
38 global input_fp, output_fp, args | |
39 with Workspace() as ws: | |
40 curler = TextCurler(ws) | |
41 while True: | |
42 line = input_fp.readline() | |
43 if line == "": | |
44 break | |
45 ws.write(line) | |
46 curler.feed() | |
47 output_fp.write(ws.getvalue()) | |
48 ws.clear() | |
49 | |
50 def html(): | |
51 global input_fp, output_fp | |
52 with Workspace(input_fp.read()) as ws: | |
53 curler = HtmlCurler(ws) | |
54 curler.feed() | |
55 output_fp.write(ws.getvalue()) | |
56 | |
57 # M a i n P r o g r a m | |
58 | |
59 # Parse arguments | |
60 parser = argparse.ArgumentParser(description='Source code character checker.', prog=MYNAME) | |
61 group = parser.add_mutually_exclusive_group() | |
62 group.add_argument("--flowed", action="store_true", help="Input is flowed text.") | |
63 group.add_argument("--html", action="store_true", help="Input is HTML.") | |
64 parser.add_argument("--force", action="store_true", help="Force all quotes to straight ones first.") | |
65 parser.add_argument("--icoding", default="UTF-8", help="Input encoding.") | |
66 parser.add_argument("--inplace", action="store_true", help="Edit file in-place.") | |
67 parser.add_argument("--ocoding", default="UTF-8", help="Output encoding.") | |
68 parser.add_argument("input", nargs="?", help="Input file.") | |
69 parser.add_argument("output", nargs="?", help="Output file.") | |
70 try: | |
71 args = parser.parse_args() | |
72 except SystemExit: | |
73 sys.exit(2) | |
74 | |
75 # Sanity-check codings | |
76 try: | |
77 codec = codecs.lookup(args.icoding) | |
78 codec = codecs.lookup(args.ocoding) | |
79 except LookupError as e: | |
80 sys.stderr.write("{0}: {1!s}\n".format(MYNAME, e)) | |
81 sys.exit(2) | |
82 if not CODECS_TO_NAME.get(codec, "").startswith("UTF-"): | |
83 sys.stderr.write("{0}: {1!s} output coding does not support Unicode\n".format(MYNAME, args.ocoding)) | |
84 sys.exit(1) | |
85 del codec | |
86 | |
87 # Get streams | |
88 try: | |
89 if args.input and (not args.output) and args.inplace: | |
90 args.output = args.input | |
91 args.input += "~" | |
92 os.rename(args.input, args.output) | |
93 if args.input: | |
94 input_fp = open(args.input, "r", encoding=args.icoding) | |
95 else: | |
96 input_fp = open(0, "r", encoding=args.icoding) | |
97 if args.output: | |
98 output_fp = open(args.output, "w", encoding=args.ocoding) | |
99 else: | |
100 output_fp = open(1, "w", encoding=args.ocoding) | |
101 except (OSError, LookupError) as e: | |
102 sys.stderr.write("{0}: {1!s}\n".format(MYNAME, e)) | |
103 sys.exit(1) | |
104 | |
105 # Choose our mode | |
106 if args.flowed: | |
107 flowed() | |
108 elif args.html: | |
109 html() | |
110 else: | |
111 normal() |