|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | | -# click makes building cmdline tools easy, see docs at |
5 | | -# https://click.palletsprojects.com/en/8.0.x |
| 4 | +import click # cmdline generator! https://click.palletsprojects.com/en/8.0.x |
| 5 | +import webbrowser |
| 6 | +import os |
6 | 7 |
|
7 | | -import click |
| 8 | +from . import sites |
8 | 9 | from . import MiniFileServer |
9 | 10 |
|
| 11 | +default_port = 8000 |
| 12 | + |
10 | 13 | @click.group() |
11 | 14 | def cli(): |
12 | | - pass |
| 15 | + pass |
13 | 16 |
|
14 | 17 | @cli.command(help="Start a local file server in this folder") |
15 | | -@click.argument('port', default=8000) |
| 18 | +@click.argument('port', default=default_port) |
16 | 19 | def serve(port): |
17 | | - MiniFileServer.run_mini_file_server(port) |
| 20 | + MiniFileServer.run_mini_file_server(port) |
| 21 | + |
| 22 | +@cli.command(help="Open your browser and view this folder using SimWrapper") |
| 23 | +@click.argument('site', default='vsp') |
| 24 | +def open(site): |
| 25 | + port = MiniFileServer.find_free_port(default_port) |
| 26 | + # Build the full URL for this site, including the free port number |
| 27 | + url = '' |
| 28 | + if site in sites.sites: |
| 29 | + url = os.path.join(sites.sites[site],str(port)) |
| 30 | + else: |
| 31 | + url = os.path.join(site,str(port)) |
| 32 | + |
| 33 | + # Open web browser first, because this command returns immediately |
| 34 | + print("Opening:", url) |
| 35 | + webbrowser.open(url, new=2, autoraise=True) # in a new tab |
18 | 36 |
|
19 | | -# @cli.command(help="Open your browser and view files in this folder") |
20 | | -# def browse(): |
21 | | -# click.echo("OPEN!!") |
| 37 | + # Then start local fileserver |
| 38 | + MiniFileServer.run_mini_file_server(port) |
0 commit comments