annotate curlyq @ 3:091c03f1b2e8

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