#!D:\PCR\NKAS\NIKKEAutoScript_\toolkit\python.exe
import argparse
from pywebio.platform import path_deploy, path_deploy_http

parser = argparse.ArgumentParser(description="Deploy the PyWebIO applications from a directory", add_help=False)
parser.add_argument("path", help="Base directory to load PyWebIO application")
parser.add_argument("-p", "--port", help="The port the server listens on", type=int, default=8080)
parser.add_argument("-h", "--host", help="The host the server listens on", default='0.0.0.0')
parser.add_argument("--no-index", help="Disable default index page", action="store_true")
parser.add_argument("--static-dir", help="Directory to store the application static files")
parser.add_argument("--no-cdn", help="Disable front-end static resources CDN", action="store_true")
parser.add_argument("-d", "--debug", help="Tornado Server's debug mode", action="store_true")
parser.add_argument("--http",
                    help="Use HTTP protocol to communication between server and browser, default use WebSocket",
                    action="store_true")
parser.add_argument("--help", help="Print help message", action='help')

group = parser.add_argument_group('http arguments', 'Extra arguments when set --http')
group.add_argument("--session-expire-seconds", help="Session expiration time, in seconds(default 600s)", type=int,
                   default=None)
group.add_argument("--session-cleanup-interval", help="Session cleanup interval, in seconds(default 300s)", type=int,
                   default=None)

group = parser.add_argument_group('websocket arguments', 'Extra arguments when not set --http')
group.add_argument("--websocket-max-message-size", help="Max bytes of a message which Tornado can accept")
group.add_argument("--websocket-ping-interval", type=int, default=None)
group.add_argument("--websocket-ping-timeout", type=int, default=None)

if __name__ == '__main__':
    args = parser.parse_args()
    kwargs = dict(vars(args))

    kwargs.pop('http')
    kwargs['base'] = kwargs.pop('path')
    kwargs['index'] = not kwargs.pop('no_index')
    kwargs['cdn'] = not kwargs.pop('no_cdn')

    if args.http:
        drop_key = ['websocket_max_message_size', 'websocket_ping_interval', 'websocket_ping_timeout']
    else:
        drop_key = ['session_expire_seconds', 'session_cleanup_interval']

    for i in drop_key:
        kwargs.pop(i, None)
    for k in list(kwargs):
        if kwargs[k] is None:
            del kwargs[k]

    if args.http:
        path_deploy_http(**kwargs)
    else:
        path_deploy(**kwargs)
