comparison bin/launch @ 65:d59811b95a62 draft

Package-ize it.
author David Barts <n5jrn@me.com>
date Thu, 04 Jul 2019 08:52:15 -0700
parents launch@f89b560b7278
children
comparison
equal deleted inserted replaced
64:25fdd985d046 65:d59811b95a62
1 #!/usr/bin/env python3
2 # XXX - This code must not be in tincan.py, because the built-in class
3 # loader will then confuse __main__.Page and tincan.Page, and fail to
4 # locate the code-behind.
5
6 # I m p o r t s
7
8 import os, sys
9 from argparse import ArgumentParser
10 import logging
11 from tincan import launch, ENCODING
12
13 # V a r i a b l e s
14
15 MYNAME = os.path.basename(sys.argv[0])
16
17 # M a i n P r o g r a m
18
19 parser = ArgumentParser(prog=sys.argv[0], usage="%(prog)s [options] [directory [path]]")
20 opt = parser.add_argument
21 opt("-b", "--bind", default="localhost", help="address to bind to (default: localhost)")
22 opt("-c", "--compile", action="store_true", help="compile .py files only; do not run a server")
23 opt("-d", "--debug", action="store_true", help="enable debug mode")
24 opt("-e", "--encoding", default=ENCODING, help="encoding to use (default {0})".format(ENCODING))
25 opt("-f", "--force", action="store_true", help="do not abort on errors")
26 opt("-p", "--port", default=8080, help="port to listen on (default: 8080)")
27 opt("-s", "--static", action="store_true", help="serve static files")
28 opt("directory", default=".", help="directory to serve", nargs='?')
29 opt("path", default="/", help="URL path to serve", nargs='?')
30 args = parser.parse_args(sys.argv[1:])
31
32 mylog = logging.getLogger(MYNAME)
33 mylog.addHandler(logging.StreamHandler())
34 mylog.setLevel(logging.DEBUG if args.debug else logging.INFO)
35
36 app, errors = launch(fsroot=args.directory, urlroot=args.path, logger=mylog,
37 encoding=args.encoding, static=args.static)
38 if errors:
39 action = "continuing" if args.force else "aborting"
40 sys.stderr.write("{0}: {1} error{2} detected, {3}\n".format(
41 MYNAME, errors, "" if errors == 1 else "s", action))
42 if not args.force: sys.exit(1)
43
44 if not args.compile:
45 app.run(host=args.bind, port=args.port)
46
47 sys.exit(1 if errors else 0)