-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
64 lines (50 loc) · 2.34 KB
/
cli.py
File metadata and controls
64 lines (50 loc) · 2.34 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import click # cmdline generator! https://click.palletsprojects.com/en/8.0.x
import webbrowser
import os
from . import sites
from . import MiniFileServer
default_port = 8000
SINGLE_PAGE_APP_PORT = 8050
@click.group()
def cli():
pass
@cli.command(help="Start a local file server in this folder")
@click.argument('port', default=default_port)
@click.option('--cert', default=None, help="PEM Certificate filename. Provide both a certificate and key to serve HTTPS")
@click.option('--key', default=None, help="PEM Key filename. Provide both a certificate and key to serve HTTPS")
def serve(port, cert, key):
if (cert and not key) or (not cert and key):
raise click.BadParameter("need both a cert and a key to enable HTTPS")
MiniFileServer.run_mini_file_server(port, cert, key)
@cli.command(help="Open your browser and view this folder using SimWrapper")
@click.argument('site', default='live')
@click.option('--cert', default=None, help="PEM Certificate filename. Provide both a certificate and key to serve HTTPS")
@click.option('--key', default=None, help="PEM Key filename. Provide both a certificate and key to serve HTTPS")
def open(site, cert, key):
if (cert and not key) or (not cert and key):
raise click.BadParameter("need both a cert and a key to enable HTTPS")
if site == "live":
port = MiniFileServer.find_free_port(SINGLE_PAGE_APP_PORT)
url = os.path.join("http://localhost:" + str(port), "live")
# Open web browser first, because this command returns immediately
print("Opening:", url)
webbrowser.open(url, new=2, autoraise=True) # in a new tab
# Then start local fileserver
MiniFileServer.serve_entire_website(port)
else:
port = MiniFileServer.find_free_port(default_port)
# Build the full URL for this site, including the free port number
url = ''
if site in sites.sites:
url = os.path.join(sites.sites[site],str(port))
else:
url = os.path.join(site,str(port))
print("Opening:", url)
webbrowser.open(url, new=2, autoraise=True) # in a new tab
MiniFileServer.run_mini_file_server(port, cert, key)
@cli.command(help="Run a live SimWrapper Website locally")
def here():
port = MiniFileServer.find_free_port(SINGLE_PAGE_APP_PORT)
MiniFileServer.serve_entire_website(port)