Mercurial > cgi-bin > hgweb.cgi > curlyq
view curlyq @ 8:05363e803272 v1_workspace
Improve help text.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 26 Dec 2019 21:54:10 -0800 |
parents | d5198c7ec54d |
children | 397c178c5b98 |
line wrap: on
line source
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os, sys import argparse import codecs from curlers import TextCurler, HtmlCurler, uncurl from workspace import Workspace from writer import CODECS_TO_NAME # V a r i a b l e s # Name invoked by MYNAME = os.path.basename(sys.argv[0]) # Streams input_fp = None output_fp = None # F u n c t i o n s def normal(): global input_fp, output_fp, args with Workspace() as ws: curler = TextCurler(ws) while True: line = input_fp.readline() ws.write(line) if line == "" or line == "\n": if args.force: uncurl(ws) curler.feed() output_fp.write(ws.getvalue()) ws.clear() if line == "": break def flowed(): global input_fp, output_fp, args with Workspace() as ws: curler = TextCurler(ws) while True: line = input_fp.readline() if line == "": break ws.write(line) if args.force: uncurl(ws) curler.feed() output_fp.write(ws.getvalue()) ws.clear() def html(): global input_fp, output_fp with Workspace(input_fp.read()) as ws: curler = HtmlCurler(ws) if args.force: uncurl(ws) curler.feed() output_fp.write(ws.getvalue()) def do_uncurl(): global input_fp, output_fp with Workspace(input_fp.read()) as ws: uncurl(ws) output_fp.write(ws.getvalue()) # M a i n P r o g r a m # Parse arguments parser = argparse.ArgumentParser( description='Make straight quotes curly.', prog=MYNAME) group = parser.add_mutually_exclusive_group() group.add_argument("--flowed", action="store_true", help="Input is flowed text.") group.add_argument("--html", action="store_true", help="Input is HTML.") group.add_argument("--uncurl", action="store_true", help="Uncurl quotes instead of curling them.") parser.add_argument("--force", action="store_true", help="Force all quotes to straight ones first.") parser.add_argument("--icoding", default="UTF-8", help="Input encoding (default UTF-8).") parser.add_argument("--inplace", action="store_true", help="Edit file in-place.") parser.add_argument("--ocoding", default="UTF-8", help="Output encoding (default UTF-8).") parser.add_argument("input", nargs="?", help="Input file.") parser.add_argument("output", nargs="?", help="Output file.") try: args = parser.parse_args() except SystemExit: sys.exit(2) # Sanity-check codings try: codec = codecs.lookup(args.icoding) codec = codecs.lookup(args.ocoding) except LookupError as e: sys.stderr.write("{0}: {1!s}\n".format(MYNAME, e)) sys.exit(2) if not CODECS_TO_NAME.get(codec, "").startswith("UTF-"): sys.stderr.write("{0}: {1!r} output coding does not support Unicode\n".format(MYNAME, args.ocoding)) sys.exit(1) del codec # Get streams try: if args.input and (not args.output) and args.inplace: args.output = args.input args.input += "~" os.rename(args.output, args.input) if args.input: input_fp = open(args.input, "r", encoding=args.icoding) else: input_fp = open(0, "r", encoding=args.icoding) if args.output: output_fp = open(args.output, "w", encoding=args.ocoding) else: output_fp = open(1, "w", encoding=args.ocoding) except (OSError, LookupError) as e: sys.stderr.write("{0}: {1!s}\n".format(MYNAME, e)) sys.exit(1) # Choose our mode if args.flowed: flowed() elif args.html: html() elif args.uncurl: do_uncurl() else: normal()