3
|
1 #!/usr/bin/env python3
|
|
2 # -*- coding: utf-8 -*-
|
|
3
|
|
4 import os, sys
|
|
5 import argparse
|
|
6 import codecs
|
|
7
|
4
|
8 from curlers import TextCurler, HtmlCurler, uncurl
|
|
9 from workspace import Workspace
|
3
|
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():
|
4
|
24 global input_fp, output_fp, args
|
3
|
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":
|
4
|
31 if args.force: uncurl(ws)
|
3
|
32 curler.feed()
|
|
33 output_fp.write(ws.getvalue())
|
|
34 ws.clear()
|
|
35 if line == "":
|
|
36 break
|
|
37
|
|
38 def flowed():
|
|
39 global input_fp, output_fp, args
|
|
40 with Workspace() as ws:
|
|
41 curler = TextCurler(ws)
|
|
42 while True:
|
|
43 line = input_fp.readline()
|
|
44 if line == "":
|
|
45 break
|
|
46 ws.write(line)
|
4
|
47 if args.force: uncurl(ws)
|
3
|
48 curler.feed()
|
|
49 output_fp.write(ws.getvalue())
|
|
50 ws.clear()
|
|
51
|
|
52 def html():
|
|
53 global input_fp, output_fp
|
|
54 with Workspace(input_fp.read()) as ws:
|
|
55 curler = HtmlCurler(ws)
|
4
|
56 if args.force: uncurl(ws)
|
3
|
57 curler.feed()
|
|
58 output_fp.write(ws.getvalue())
|
|
59
|
|
60 # M a i n P r o g r a m
|
|
61
|
|
62 # Parse arguments
|
|
63 parser = argparse.ArgumentParser(description='Source code character checker.', prog=MYNAME)
|
|
64 group = parser.add_mutually_exclusive_group()
|
|
65 group.add_argument("--flowed", action="store_true", help="Input is flowed text.")
|
|
66 group.add_argument("--html", action="store_true", help="Input is HTML.")
|
|
67 parser.add_argument("--force", action="store_true", help="Force all quotes to straight ones first.")
|
|
68 parser.add_argument("--icoding", default="UTF-8", help="Input encoding.")
|
|
69 parser.add_argument("--inplace", action="store_true", help="Edit file in-place.")
|
|
70 parser.add_argument("--ocoding", default="UTF-8", help="Output encoding.")
|
|
71 parser.add_argument("input", nargs="?", help="Input file.")
|
|
72 parser.add_argument("output", nargs="?", help="Output file.")
|
|
73 try:
|
|
74 args = parser.parse_args()
|
|
75 except SystemExit:
|
|
76 sys.exit(2)
|
|
77
|
|
78 # Sanity-check codings
|
|
79 try:
|
|
80 codec = codecs.lookup(args.icoding)
|
|
81 codec = codecs.lookup(args.ocoding)
|
|
82 except LookupError as e:
|
|
83 sys.stderr.write("{0}: {1!s}\n".format(MYNAME, e))
|
|
84 sys.exit(2)
|
|
85 if not CODECS_TO_NAME.get(codec, "").startswith("UTF-"):
|
|
86 sys.stderr.write("{0}: {1!s} output coding does not support Unicode\n".format(MYNAME, args.ocoding))
|
|
87 sys.exit(1)
|
|
88 del codec
|
|
89
|
|
90 # Get streams
|
|
91 try:
|
|
92 if args.input and (not args.output) and args.inplace:
|
|
93 args.output = args.input
|
|
94 args.input += "~"
|
|
95 os.rename(args.input, args.output)
|
|
96 if args.input:
|
|
97 input_fp = open(args.input, "r", encoding=args.icoding)
|
|
98 else:
|
|
99 input_fp = open(0, "r", encoding=args.icoding)
|
|
100 if args.output:
|
|
101 output_fp = open(args.output, "w", encoding=args.ocoding)
|
|
102 else:
|
|
103 output_fp = open(1, "w", encoding=args.ocoding)
|
|
104 except (OSError, LookupError) as e:
|
|
105 sys.stderr.write("{0}: {1!s}\n".format(MYNAME, e))
|
|
106 sys.exit(1)
|
|
107
|
|
108 # Choose our mode
|
|
109 if args.flowed:
|
|
110 flowed()
|
|
111 elif args.html:
|
|
112 html()
|
|
113 else:
|
|
114 normal()
|