-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
208 lines (160 loc) · 5.04 KB
/
cli.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import click
import json
import os
import subprocess
from lib.util import write_config, default_options
from gui import main
@click.group("pyzdl")
def pyzdl():
pass
@pyzdl.group("profiles")
def profiles():
pass
@pyzdl.group("iwads")
def iwads():
pass
@pyzdl.group("ports")
def ports():
pass
@profiles.command("ls")
@default_options
def ls_profiles(app, config_path):
for profile_name, profile in app.profiles.items():
click.echo(f"{profile_name} ({profile.port.name})")
@profiles.command("launch-args")
@click.argument("name")
@default_options
def show_profile_launch_args(app, config_path, name):
click.echo(app.get_profile_launch_args(app.profiles[name]))
@ports.command("ls")
@default_options
def ls_ports(app, config_path):
for source_port_name, source_port in app.source_ports.items():
click.echo(f"{source_port_name} ({source_port.executable.path})")
@iwads.command("ls")
@default_options
def ls_iwads(app, config_path):
for iwad_name, iwad in app.iwads.items():
click.echo(f"{iwad_name} ({iwad.path})")
@ports.command("add")
@click.argument("name", type=click.STRING)
@click.argument("path", type=click.Path(exists=True))
@default_options
def add_source_port(app, config_path, name, path):
app.add_source_port(name, path)
write_config(app, config_path)
@ports.command("rm")
@click.argument("name", type=click.STRING)
@default_options
def rm_source_port(app, config_path, name):
port = app.rm_source_port(name)
if port:
write_config(app, config_path)
@iwads.command("add")
@click.argument("name", type=click.STRING)
@click.argument("path", type=click.Path(exists=True))
@default_options
def add_iwad(app, config_path, name, path):
app.add_iwad(name, path)
write_config(app, config_path)
@iwads.command("rm")
@click.argument("name", type=click.STRING)
@default_options
def rm_iwad(app, config_path, name):
iwad = app.rm_iwad(name)
if iwad:
write_config(app, config_path)
@profiles.command("add")
@click.argument("name", type=click.STRING)
@click.argument("port", type=click.STRING)
@click.argument("iwad", type=click.STRING)
@click.option("--file", "files", type=click.Path(exists=True), multiple=True)
@default_options
def add_profile(app, config_path, name, port, iwad, files):
app.add_profile(name, port, iwad, files)
write_config(app, config_path)
@profiles.command("rm")
@click.argument("name", type=click.STRING)
@default_options
def rm_profile(app, config_path, name):
profile = app.rm_profile(name)
if profile:
write_config(app, config_path)
@pyzdl.group("config")
def config():
pass
@config.command("inspect")
@click.option(
"--format",
"format_type",
type=click.Choice(choices=["json", "zdl", "ini"]),
default="json",
)
@default_options
def show_config(app, config_path, format_type):
if format_type == "zdl" or format_type == "ini":
app.to_zdl_ini(click.get_text_stream("stdout"))
else:
json.dump(
app.to_json(),
click.get_text_stream("stdout"),
indent=2,
)
@config.command("edit")
@click.argument("editor", required=False)
@default_options
def edit_config(app, config_path, editor):
if not editor:
editor = os.getenv("EDITOR")
cmd = [editor, config_path]
subprocess.call(cmd)
@pyzdl.group("run")
def run():
pass
@run.command("profile", context_settings={"ignore_unknown_options": True})
@click.argument("name", required=False)
@click.argument("extra_args", nargs=-1, type=click.UNPROCESSED)
@default_options
def run_profile(app, config_path, name, extra_args):
if not app.profiles:
raise click.ClickException("No available profiles.")
if not name:
name = next(iter(app.profiles.keys()))
click.echo(f"No profile given, assuming {name}.", err=True)
try:
profile = app.profiles[name]
except KeyError:
raise click.ClickException(f"Could not find profile {name} in config.")
app.launch_profile(profile.name, extra_args=extra_args)
@run.command("zdl", context_settings={"ignore_unknown_options": True})
@click.argument("path")
@click.argument("extra_args", nargs=-1, type=click.UNPROCESSED)
@default_options
def run_zdl(app, config_path, path, extra_args):
app.launch_zdl(path, extra_args=extra_args)
@profiles.command("import")
@click.argument("path")
@click.argument("name", required=False)
@default_options
def import_profile(app, config_path, path, name):
app.import_profile(path, name)
write_config(app, config_path)
@profiles.command("inspect")
@click.argument("name")
@click.option(
"--format",
"format_type",
type=click.Choice(choices=["json", "zdl", "ini"]),
default="json",
)
@default_options
def inspect_profile(app, config_path, name, format_type):
fp = click.get_text_stream("stdout")
profile = app.profiles[name]
if format_type == "json":
json.dump(profile.to_json(), fp, indent=2)
elif format_type == "zdl" or format_type == "ini":
profile.to_zdl_ini(fp)
pyzdl.add_command(main, "gui")
if __name__ == "__main__":
pyzdl()