-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·122 lines (103 loc) · 3.33 KB
/
main.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
"""main.py
A common command-line interface for all CSC 582 labs.
[//]: # (markdown comment # noqa)
Usage:
main.py lab1 IN_TXT_FILE
[ --verbose | -v ]
[ --debug | -d ]
main.py lab2
[ --verbose | -v ]
[ --debug | -d ]
main.py lab3
[ --verbose | -v ]
[ --debug | -d ]
main.py (-h | --help)
[ --verbose | -v ]
[ --debug | -d ]
Options:
-h --help Show this screen.
--debug -d printouts while running, extra debugging.
lab1 A robot producer. Given an overview, outputs a movie cast & crew.
lab2 Do the lab2 thing.
lab3 Do the lab3 thing.
IN_TXT_FILE a file to read from.
Example:
$ python main.py lab1 inputs/in1.txt
...
Resources:
* [docopt is cool](http://docopt.org)
"""
import os
import subprocess
from enum import Enum
from docopt import docopt
from utils.terminal_colors import print_colored_doc, print_debug
class OS_NAME(Enum):
# https://stackoverflow.com/a/1325587/5411712
WINDOWS = "nt"
UNIX = "posix"
OTHER = "java"
def print_help():
to_color_green_bold = (
"main.py",
"(-h | --help)",
)
to_color_yellow_bold = (
"lab1",
"IN_TXT_FILE",
"lab2",
"lab3",
"lab4",
"lab5",
)
to_color_white_bold = (
"Do the main thing.",
"Usage:",
"Options:",
"Example:",
"Resources:",
"Changelog:",
)
to_color_white_bold_patterns = (r"(\$.*)",)
to_color_red_bold_patterns = (r"(defaults to.*)",)
to_color_grey_out = ("[//]: # (markdown comment # noqa)",)
print_colored_doc(
doc=__doc__,
to_color_green_bold=to_color_green_bold,
to_color_yellow_bold=to_color_yellow_bold,
to_color_white_bold=to_color_white_bold,
to_color_white_bold_patterns=to_color_white_bold_patterns,
to_color_red_bold_patterns=to_color_red_bold_patterns,
to_color_grey_out=to_color_grey_out,
)
if __name__ == "__main__":
# My basic docopt setup...
arguments = docopt(__doc__, version="csc582 Main 1.0.0", help=False)
VERBOSE = arguments["--verbose"] or arguments["-v"]
DEBUG = arguments["--debug"]
print(arguments) if DEBUG else None
print("VERBOSE:", VERBOSE) if DEBUG else None
print("DEBUG:", DEBUG) if DEBUG else None
if arguments["--help"]:
print_help()
exit()
IN_TXT_FILE = arguments["IN_TXT_FILE"]
if arguments["lab1"]:
# https://stackoverflow.com/a/1325587/5411712
set_options = [a for a, v in arguments.items() if v and a.startswith("-")]
if os.name == OS_NAME.WINDOWS.value:
# often Windows does not have `python3` as alias
subprocess.run(["python", "robotproducer.py", IN_TXT_FILE, *set_options])
elif os.name == OS_NAME.UNIX.value:
subprocess.run(["python3", "robotproducer.py", IN_TXT_FILE, *set_options])
else:
print_debug(f"why is this Python running on {os.name}??")
subprocess.run(["python", "robotproducer.py", IN_TXT_FILE, *set_options])
exit()
if arguments["lab2"]:
subprocess.run(["echo", "lab2"])
exit()
if arguments["lab3"]:
subprocess.run(["echo", "lab2"])
exit()