forked from uggla/async_rust_tui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·88 lines (71 loc) · 2.14 KB
/
server.py
File metadata and controls
executable file
·88 lines (71 loc) · 2.14 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env -S uv run
# -*- coding: utf-8 -*-
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "staticjinja",
# ]
# ///
import http.server
import socketserver
import os
import subprocess
import sys
import argparse
PORT = os.environ.get("PORT", "8000")
HOST = os.environ.get("HOST", "localhost")
SLIDES_DIR = "slides"
Handler = http.server.SimpleHTTPRequestHandler
class ReuseTCPServer(socketserver.TCPServer):
allow_reuse_address = True
def check_tools():
try:
subprocess.check_output(
"which staticjinja >/dev/null 2>&1", shell=True)
if not os.path.exists("reveal.js/package.json"):
raise FileNotFoundError
except subprocess.CalledProcessError:
print(
"staticjinja is missing, " "Please install it with pip install staticjinja",
file=sys.stderr,
)
sys.exit(1)
except FileNotFoundError:
print(
"Could not find reveal.js, "
"Please get the submodules using "
"git submodule init && git submodule update",
file=sys.stderr,
)
def serve_content():
with ReuseTCPServer((HOST, int(PORT)), Handler) as httpd:
print("serving at http://{}:{}".format(HOST, PORT))
httpd.serve_forever()
def build_presentations():
print("Building presentation for {}".format(SLIDES_DIR))
os.chdir(SLIDES_DIR)
subprocess.check_output("staticjinja build", shell=True)
os.chdir("..")
def watch():
print("\033[32mStaticjinja watch for {}\033[0m".format(SLIDES_DIR))
os.chdir(SLIDES_DIR)
# Replace with your shell command and arguments
command = ["staticjinja", "watch"]
# Run the command in the background
process = subprocess.Popen(
command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
os.chdir("..")
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--watch", help="Watch for template changes", action="store_true"
)
args = parser.parse_args()
check_tools()
build_presentations()
if args.watch:
watch()
serve_content()
if __name__ == "__main__":
main()