~sirn/fanboi2

ref: 834edf0edc5dd633c0ecea16231b6ed2d728476d fanboi2/fanboi2/cmd/ctl.py -rw-r--r-- 1.9 KiB
834edf0eKridsada Thanabulpong Massive cleanup in preparation for 0.30 (#25) 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import argparse
import code
import sys

from ..version import __VERSION__, __PYRAMID__


SHELL_BANNER = """\
      ___  ___
    / /     / /
   /_/_   _/_/    fanboi2 %s
  / /   / /       pyramid %s
 /_/   /_/__

Loaded locals:

    app           Fanboi2 WSGI application
    config        Pyramid configurator
    registry      Pyramid registry
    request       Pyramid request context
    root          Pyramid root
    root_factory  Pyramid root factory
"""


def shell(args):
    """Run the interactive shell for the application."""
    from ..wsgi import app, config
    import pyramid.scripting
    with pyramid.scripting.prepare() as env:
        code.interact(
            SHELL_BANNER % (__VERSION__, __PYRAMID__),
            local={
                "app": app,
                "config": config,
                **env
            })


def serve(args):
    """Run the web server for the application."""
    from ..wsgi import app
    from waitress import serve as waitress_serve
    waitress_serve(app, host=args.host, port=args.port)
    sys.exit(0)


def gensecret(args):
    """Generates a NaCl secret."""
    from pyramid_nacl_session import generate_secret
    print(generate_secret(as_hex=True).decode('utf-8'))
    sys.exit(0)


def main():
    """Parse the command line arguments."""
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest='subparser', title='subcommands')

    pshell = subparsers.add_parser('shell')
    pshell.set_defaults(func=shell)

    pserve = subparsers.add_parser('serve')
    pserve.add_argument('--port', type=int, default=6543)
    pserve.add_argument('--host', default='0.0.0.0')
    pserve.set_defaults(func=serve)

    gsecret = subparsers.add_parser('gensecret')
    gsecret.set_defaults(func=gensecret)

    args = parser.parse_args()
    if args.subparser is None:
        parser.print_usage()
        sys.exit(1)

    args.func(args)