annotate launch @ 47:879ec681f7e3 draft

Add --compile option.
author David Barts <n5jrn@me.com>
date Thu, 30 May 2019 20:14:14 -0700
parents 997d0c8c174f
children f89b560b7278
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 #!/usr/bin/env python3
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
2 # XXX - This code must not be in tincan.py, because the built-in class
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 # loader will then confuse __main__.Page and tincan.Page, and fail to
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 # locate the code-behind.
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
5
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 # I m p o r t s
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
7
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import os, sys
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 from argparse import ArgumentParser
46
997d0c8c174f Default for the launch callable should be not to log.
David Barts <n5jrn@me.com>
parents: 40
diff changeset
10 import logging
37
ce67eac10fc7 Allow global character encoding specification.
David Barts <n5jrn@me.com>
parents: 25
diff changeset
11 from tincan import launch, ENCODING
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
12
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 # V a r i a b l e s
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
14
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 MYNAME = os.path.basename(sys.argv[0])
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
16
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 # M a i n P r o g r a m
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
18
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 parser = ArgumentParser(prog=sys.argv[0], usage="%(prog)s [options] [directory [path]]")
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 opt = parser.add_argument
37
ce67eac10fc7 Allow global character encoding specification.
David Barts <n5jrn@me.com>
parents: 25
diff changeset
21 opt("-b", "--bind", default="localhost", help="address to bind to (default: localhost)")
47
879ec681f7e3 Add --compile option.
David Barts <n5jrn@me.com>
parents: 46
diff changeset
22 opt("-c", "--compile", action="store_true", help="compile .py files only; do not run a server")
25
e93e5e746cc5 Preliminary debugging, still not fully tested.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
23 opt("-d", "--debug", action="store_true", help="enable debug mode")
37
ce67eac10fc7 Allow global character encoding specification.
David Barts <n5jrn@me.com>
parents: 25
diff changeset
24 opt("-e", "--encoding", default=ENCODING, help="encoding to use (default {0})".format(ENCODING))
25
e93e5e746cc5 Preliminary debugging, still not fully tested.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
25 opt("-f", "--force", action="store_true", help="do not abort on errors")
37
ce67eac10fc7 Allow global character encoding specification.
David Barts <n5jrn@me.com>
parents: 25
diff changeset
26 opt("-p", "--port", default=8080, help="port to listen on (default: 8080)")
40
df27cf08c093 Add support for serving static files.
David Barts <n5jrn@me.com>
parents: 37
diff changeset
27 opt("-s", "--static", action="store_true", help="serve static files")
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 opt("directory", default=".", help="directory to serve", nargs='?')
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 opt("path", default="/", help="URL path to serve", nargs='?')
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 args = parser.parse_args(sys.argv[1:])
46
997d0c8c174f Default for the launch callable should be not to log.
David Barts <n5jrn@me.com>
parents: 40
diff changeset
31 mylog = logging.getLogger(MYNAME)
997d0c8c174f Default for the launch callable should be not to log.
David Barts <n5jrn@me.com>
parents: 40
diff changeset
32 mylog.addHandler(logging.StreamHandler())
997d0c8c174f Default for the launch callable should be not to log.
David Barts <n5jrn@me.com>
parents: 40
diff changeset
33 mylog.setLevel(logging.DEBUG if args.debug else logging.INFO)
997d0c8c174f Default for the launch callable should be not to log.
David Barts <n5jrn@me.com>
parents: 40
diff changeset
34 app, errors = launch(fsroot=args.directory, urlroot=args.path, logger=mylog,
40
df27cf08c093 Add support for serving static files.
David Barts <n5jrn@me.com>
parents: 37
diff changeset
35 encoding=args.encoding, static=args.static)
4
0d47859f792a Finally got "hello, world" working. Still likely many bugs.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 if errors:
25
e93e5e746cc5 Preliminary debugging, still not fully tested.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
37 action = "continuing" if args.force else "aborting"
e93e5e746cc5 Preliminary debugging, still not fully tested.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
38 sys.stderr.write("{0}: {1} error{2} detected, {3}\n".format(
e93e5e746cc5 Preliminary debugging, still not fully tested.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
39 MYNAME, errors, "" if errors == 1 else "s", action))
e93e5e746cc5 Preliminary debugging, still not fully tested.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
40 if not args.force: sys.exit(1)
47
879ec681f7e3 Add --compile option.
David Barts <n5jrn@me.com>
parents: 46
diff changeset
41
879ec681f7e3 Add --compile option.
David Barts <n5jrn@me.com>
parents: 46
diff changeset
42 if not args.compile:
879ec681f7e3 Add --compile option.
David Barts <n5jrn@me.com>
parents: 46
diff changeset
43 app.run(host=args.bind, port=args.port)