serverwatcher 6.dev3__py3-none-any.whl → 6.dev4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- serverwatcher/cli.py +102 -79
- {serverwatcher-6.dev3.dist-info → serverwatcher-6.dev4.dist-info}/METADATA +1 -1
- {serverwatcher-6.dev3.dist-info → serverwatcher-6.dev4.dist-info}/RECORD +6 -6
- {serverwatcher-6.dev3.dist-info → serverwatcher-6.dev4.dist-info}/WHEEL +0 -0
- {serverwatcher-6.dev3.dist-info → serverwatcher-6.dev4.dist-info}/licenses/LICENSE +0 -0
- {serverwatcher-6.dev3.dist-info → serverwatcher-6.dev4.dist-info}/top_level.txt +0 -0
serverwatcher/cli.py
CHANGED
|
@@ -11,7 +11,11 @@ from .watcher import ServerWatcher
|
|
|
11
11
|
|
|
12
12
|
setGlobalMaps(maps.ascii_colors)
|
|
13
13
|
|
|
14
|
-
#
|
|
14
|
+
# command registry
|
|
15
|
+
COMMANDS: dict[str, callable] = {}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# parsed arguments
|
|
15
19
|
@dataclass
|
|
16
20
|
class ParsedArgs:
|
|
17
21
|
subcommand: str | None
|
|
@@ -32,10 +36,10 @@ def parse_line(raw: str) -> ParsedArgs:
|
|
|
32
36
|
params: dict[str, str] = {}
|
|
33
37
|
|
|
34
38
|
for token in parts[1:]:
|
|
35
|
-
if token.startswith(
|
|
39
|
+
if token.startswith("--"):
|
|
36
40
|
flags[token[2:]] = True
|
|
37
|
-
elif
|
|
38
|
-
key, value = token.split(
|
|
41
|
+
elif ":" in token:
|
|
42
|
+
key, value = token.split(":", 1)
|
|
39
43
|
params[key.lower()] = value
|
|
40
44
|
else:
|
|
41
45
|
positional.append(token)
|
|
@@ -43,10 +47,11 @@ def parse_line(raw: str) -> ParsedArgs:
|
|
|
43
47
|
return ParsedArgs(sub, positional, flags, params)
|
|
44
48
|
|
|
45
49
|
|
|
50
|
+
# command context
|
|
46
51
|
class CommandContext:
|
|
47
52
|
def __init__(self, parsed: ParsedArgs):
|
|
48
53
|
self.parsed = parsed
|
|
49
|
-
self.
|
|
54
|
+
self.children: dict[str, callable] = {}
|
|
50
55
|
|
|
51
56
|
def positional(self, index: int, default=None):
|
|
52
57
|
try:
|
|
@@ -55,6 +60,7 @@ class CommandContext:
|
|
|
55
60
|
return default
|
|
56
61
|
|
|
57
62
|
|
|
63
|
+
# DSL object
|
|
58
64
|
class CommandDSL:
|
|
59
65
|
def __init__(self):
|
|
60
66
|
self.registry: dict[str, callable] = {}
|
|
@@ -73,7 +79,7 @@ class CommandDSL:
|
|
|
73
79
|
|
|
74
80
|
def param(self, name: str, type: callable = str, default: object = None):
|
|
75
81
|
def deco(func: callable):
|
|
76
|
-
func.__arg_kind__ =
|
|
82
|
+
func.__arg_kind__ = "param"
|
|
77
83
|
func.__arg_name__ = name
|
|
78
84
|
func.__arg_type__ = type
|
|
79
85
|
func.__arg_default__ = default
|
|
@@ -82,7 +88,7 @@ class CommandDSL:
|
|
|
82
88
|
|
|
83
89
|
def flag(self, name: str):
|
|
84
90
|
def deco(func: callable):
|
|
85
|
-
func.__arg_kind__ =
|
|
91
|
+
func.__arg_kind__ = "flag"
|
|
86
92
|
func.__arg_name__ = name
|
|
87
93
|
return func
|
|
88
94
|
return deco
|
|
@@ -91,23 +97,25 @@ class CommandDSL:
|
|
|
91
97
|
command = CommandDSL()
|
|
92
98
|
|
|
93
99
|
|
|
100
|
+
# bind nested functions (children, params, flags)
|
|
94
101
|
def bind_nested_functions(ctx: CommandContext, namespace: dict):
|
|
95
102
|
for name, obj in namespace.items():
|
|
96
103
|
if not callable(obj):
|
|
97
104
|
continue
|
|
98
105
|
|
|
99
|
-
#
|
|
100
|
-
if hasattr(obj,
|
|
101
|
-
|
|
106
|
+
# children
|
|
107
|
+
if hasattr(obj, "__child_name__"):
|
|
108
|
+
child_name = obj.__child_name__
|
|
109
|
+
ctx.children[child_name] = obj
|
|
102
110
|
|
|
103
|
-
#
|
|
104
|
-
if hasattr(obj,
|
|
111
|
+
# params / flags
|
|
112
|
+
if hasattr(obj, "__arg_kind__"):
|
|
105
113
|
kind = obj.__arg_kind__
|
|
106
114
|
argname = obj.__arg_name__
|
|
107
|
-
default = getattr(obj,
|
|
108
|
-
type_ = getattr(obj,
|
|
115
|
+
default = getattr(obj, "__arg_default__", None)
|
|
116
|
+
type_ = getattr(obj, "__arg_type__", str)
|
|
109
117
|
|
|
110
|
-
if kind ==
|
|
118
|
+
if kind == "flag":
|
|
111
119
|
present = argname in ctx.parsed.flags
|
|
112
120
|
|
|
113
121
|
def wrapper(present=present):
|
|
@@ -115,7 +123,7 @@ def bind_nested_functions(ctx: CommandContext, namespace: dict):
|
|
|
115
123
|
|
|
116
124
|
namespace[name] = wrapper
|
|
117
125
|
|
|
118
|
-
elif kind ==
|
|
126
|
+
elif kind == "param":
|
|
119
127
|
if argname in ctx.parsed.params:
|
|
120
128
|
raw = ctx.parsed.params[argname]
|
|
121
129
|
|
|
@@ -133,15 +141,16 @@ def bind_nested_functions(ctx: CommandContext, namespace: dict):
|
|
|
133
141
|
namespace[name] = wrapper
|
|
134
142
|
|
|
135
143
|
|
|
136
|
-
#
|
|
144
|
+
# base CLI
|
|
137
145
|
class CLIBase:
|
|
138
|
-
def safePrint(self, msg: object =
|
|
146
|
+
def safePrint(self, msg: object = "", end: str = "\n") -> None:
|
|
139
147
|
print(str(msg), end=end)
|
|
140
148
|
|
|
141
149
|
def bprint(self, msg: object) -> None:
|
|
142
150
|
self.safePrint(msg)
|
|
143
151
|
|
|
144
152
|
|
|
153
|
+
# dispatcher
|
|
145
154
|
def dispatch(cli: CLIBase, line: str):
|
|
146
155
|
parsed = parse_line(line)
|
|
147
156
|
if not parsed.subcommand:
|
|
@@ -149,18 +158,26 @@ def dispatch(cli: CLIBase, line: str):
|
|
|
149
158
|
|
|
150
159
|
handler = command.registry.get(parsed.subcommand)
|
|
151
160
|
if handler is None:
|
|
152
|
-
cli.safePrint(f
|
|
161
|
+
cli.safePrint(f"Unknown command '{parsed.subcommand}'")
|
|
153
162
|
return
|
|
154
163
|
|
|
155
164
|
ctx = CommandContext(parsed)
|
|
156
165
|
handler(cli, ctx)
|
|
157
166
|
|
|
158
|
-
|
|
167
|
+
# choose child based on first positional token
|
|
168
|
+
sub = ctx.positional(0)
|
|
169
|
+
if sub and sub in ctx.children:
|
|
170
|
+
# second positional is the stat or argument
|
|
171
|
+
stat = ctx.positional(1)
|
|
172
|
+
return ctx.children[sub](stat)
|
|
173
|
+
elif len(ctx.children) == 1:
|
|
174
|
+
# single child, first positional is stat
|
|
175
|
+
only_child = next(iter(ctx.children.values()))
|
|
159
176
|
stat = ctx.positional(0)
|
|
160
|
-
return
|
|
177
|
+
return only_child(stat)
|
|
161
178
|
|
|
162
179
|
|
|
163
|
-
#
|
|
180
|
+
# buffering stdout
|
|
164
181
|
class BufferingStdout:
|
|
165
182
|
def __init__(self, buffer, real_stdout):
|
|
166
183
|
self.buffer = buffer
|
|
@@ -168,15 +185,16 @@ class BufferingStdout:
|
|
|
168
185
|
|
|
169
186
|
def write(self, text: str) -> None:
|
|
170
187
|
if self.buffer.enabled and text.strip():
|
|
171
|
-
self.buffer.captured.append(text.rstrip(
|
|
188
|
+
self.buffer.captured.append(text.rstrip("\n"))
|
|
172
189
|
self.real_stdout.write(text)
|
|
173
190
|
|
|
174
191
|
def flush(self) -> None:
|
|
175
192
|
self.real_stdout.flush()
|
|
176
193
|
|
|
177
194
|
|
|
195
|
+
# main CLI
|
|
178
196
|
class WatcherCLI(cmd.Cmd, CLIBase):
|
|
179
|
-
prompt =
|
|
197
|
+
prompt = ""
|
|
180
198
|
|
|
181
199
|
def __init__(self, watcher: ServerWatcher):
|
|
182
200
|
self.watcher = watcher
|
|
@@ -184,7 +202,7 @@ class WatcherCLI(cmd.Cmd, CLIBase):
|
|
|
184
202
|
self.real_stdout = sys.stdout
|
|
185
203
|
self.cli_stdout = BufferingStdout(self.buffer, self.real_stdout)
|
|
186
204
|
super().__init__(stdout=self.cli_stdout)
|
|
187
|
-
self.outputMode =
|
|
205
|
+
self.outputMode = "both"
|
|
188
206
|
self.use_rawinput = False
|
|
189
207
|
|
|
190
208
|
def read_line_raw(self) -> str:
|
|
@@ -195,10 +213,10 @@ class WatcherCLI(cmd.Cmd, CLIBase):
|
|
|
195
213
|
chars: list[str] = []
|
|
196
214
|
while True:
|
|
197
215
|
ch = sys.stdin.read(1)
|
|
198
|
-
if ch in (
|
|
216
|
+
if ch in ("\r", "\n"):
|
|
199
217
|
break
|
|
200
218
|
chars.append(ch)
|
|
201
|
-
return
|
|
219
|
+
return "".join(chars)
|
|
202
220
|
finally:
|
|
203
221
|
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
|
204
222
|
|
|
@@ -209,7 +227,7 @@ class WatcherCLI(cmd.Cmd, CLIBase):
|
|
|
209
227
|
line = line.strip()
|
|
210
228
|
if not line:
|
|
211
229
|
continue
|
|
212
|
-
if self.buffer.enabled and not line.startswith(
|
|
230
|
+
if self.buffer.enabled and not line.startswith("view"):
|
|
213
231
|
self.buffer.captured.append(line)
|
|
214
232
|
stop = await asyncio.to_thread(self.onecmd, line)
|
|
215
233
|
if stop:
|
|
@@ -225,36 +243,36 @@ class WatcherCLI(cmd.Cmd, CLIBase):
|
|
|
225
243
|
return dispatch(self, line)
|
|
226
244
|
|
|
227
245
|
def printHeader(self) -> None:
|
|
228
|
-
self.safePrint(res(
|
|
229
|
-
self.safePrint(res(
|
|
230
|
-
self.safePrint(res(
|
|
231
|
-
self.safePrint(
|
|
246
|
+
self.safePrint(res("<yellow>-----------------------------------"))
|
|
247
|
+
self.safePrint(res("<yellow>-------- <aqua>ServerWatcher CLI <yellow>--------"))
|
|
248
|
+
self.safePrint(res("<yellow>-----------------------------------"))
|
|
249
|
+
self.safePrint("\n")
|
|
232
250
|
|
|
233
251
|
|
|
234
|
-
#
|
|
235
|
-
@command(
|
|
252
|
+
# stats command
|
|
253
|
+
@command("stats")
|
|
236
254
|
def stats(self: WatcherCLI, ctx: CommandContext):
|
|
237
255
|
|
|
238
|
-
@command.child(
|
|
256
|
+
@command.child("get")
|
|
239
257
|
def get(stat: str):
|
|
240
258
|
|
|
241
|
-
@command.param(
|
|
259
|
+
@command.param("rounding", type=int, default=2)
|
|
242
260
|
def rounding(value):
|
|
243
261
|
return value
|
|
244
262
|
|
|
245
|
-
@command.param(
|
|
263
|
+
@command.param("mode", type=str, default="current")
|
|
246
264
|
def mode(value):
|
|
247
265
|
return value
|
|
248
266
|
|
|
249
|
-
@command.flag(
|
|
267
|
+
@command.flag("raw")
|
|
250
268
|
def raw():
|
|
251
269
|
return False
|
|
252
270
|
|
|
253
|
-
@command.flag(
|
|
271
|
+
@command.flag("formatted")
|
|
254
272
|
def formatted():
|
|
255
273
|
return True
|
|
256
274
|
|
|
257
|
-
@command.flag(
|
|
275
|
+
@command.flag("no-formatted")
|
|
258
276
|
def no_formatted():
|
|
259
277
|
return False
|
|
260
278
|
|
|
@@ -263,59 +281,63 @@ def stats(self: WatcherCLI, ctx: CommandContext):
|
|
|
263
281
|
self.watcher.server.refresh()
|
|
264
282
|
|
|
265
283
|
gb = not raw()
|
|
266
|
-
|
|
284
|
+
if formatted():
|
|
285
|
+
fmt = True
|
|
286
|
+
elif no_formatted():
|
|
287
|
+
fmt = False
|
|
288
|
+
else:
|
|
289
|
+
fmt = True
|
|
267
290
|
|
|
268
|
-
unit =
|
|
291
|
+
unit = ""
|
|
269
292
|
match stat:
|
|
270
|
-
case
|
|
293
|
+
case "ram":
|
|
271
294
|
value = self.watcher.server.getRAM(rounding=rounding(), gb=gb)
|
|
272
|
-
name =
|
|
273
|
-
unit =
|
|
274
|
-
case
|
|
295
|
+
name = "RAM"
|
|
296
|
+
unit = " GB" if gb else " MB"
|
|
297
|
+
case "cpu":
|
|
275
298
|
value = self.watcher.server.getCPU(rounding=rounding())
|
|
276
|
-
name =
|
|
277
|
-
unit =
|
|
278
|
-
case
|
|
299
|
+
name = "CPU"
|
|
300
|
+
unit = "%"
|
|
301
|
+
case "uptime":
|
|
279
302
|
value = self.watcher.server.getUptime(formatted=fmt)
|
|
280
|
-
name =
|
|
281
|
-
unit =
|
|
282
|
-
case
|
|
303
|
+
name = "Uptime"
|
|
304
|
+
unit = "" if fmt else "ms"
|
|
305
|
+
case "tps":
|
|
283
306
|
value = self.watcher.server.getTPS(rounding=rounding(), mode=mode())
|
|
284
|
-
name =
|
|
285
|
-
case
|
|
307
|
+
name = "TPS"
|
|
308
|
+
case "players":
|
|
286
309
|
value = self.watcher.server.getPlayers()
|
|
287
|
-
name =
|
|
288
|
-
case
|
|
310
|
+
name = "Players"
|
|
311
|
+
case "version":
|
|
289
312
|
value = self.watcher.server.version
|
|
290
|
-
name =
|
|
291
|
-
case
|
|
313
|
+
name = "Minecraft version"
|
|
314
|
+
case "platform":
|
|
292
315
|
value = self.watcher.server.platform
|
|
293
|
-
name =
|
|
316
|
+
name = "Server platform"
|
|
294
317
|
case _:
|
|
295
|
-
return self.safePrint(
|
|
318
|
+
return self.safePrint("Unknown stat")
|
|
296
319
|
|
|
297
|
-
self.bprint(f
|
|
320
|
+
self.bprint(f"{name}: {value}{unit}")
|
|
298
321
|
|
|
299
322
|
|
|
300
|
-
|
|
323
|
+
# view command
|
|
324
|
+
@command("view")
|
|
301
325
|
def view(self: WatcherCLI, ctx: CommandContext):
|
|
302
326
|
|
|
303
|
-
@command.child(
|
|
327
|
+
@command.child("cli")
|
|
304
328
|
def cli_view(_stat: str):
|
|
305
|
-
|
|
306
329
|
self.watcher.router.disableOriginOutput()
|
|
307
|
-
self.outputMode =
|
|
330
|
+
self.outputMode = "cli"
|
|
308
331
|
self.buffer.enabled = True
|
|
309
332
|
utils.clearTerminal()
|
|
310
333
|
self.printHeader()
|
|
311
334
|
for msg in self.buffer.captured:
|
|
312
335
|
self.safePrint(msg)
|
|
313
336
|
|
|
314
|
-
@command.child(
|
|
337
|
+
@command.child("watcher")
|
|
315
338
|
def watcher_view(_stat: str):
|
|
316
|
-
|
|
317
339
|
self.watcher.router.enableOriginOutput()
|
|
318
|
-
self.outputMode =
|
|
340
|
+
self.outputMode = "both"
|
|
319
341
|
utils.clearTerminal()
|
|
320
342
|
self.printHeader()
|
|
321
343
|
for msg in self.watcher.router.buffer.captured:
|
|
@@ -324,12 +346,13 @@ def view(self: WatcherCLI, ctx: CommandContext):
|
|
|
324
346
|
bind_nested_functions(ctx, locals())
|
|
325
347
|
|
|
326
348
|
|
|
327
|
-
|
|
349
|
+
# clear command
|
|
350
|
+
@command("clear")
|
|
328
351
|
def clear(self: WatcherCLI, ctx: CommandContext):
|
|
329
352
|
|
|
330
|
-
@command.param(
|
|
353
|
+
@command.param("buffer", type=str, default="true")
|
|
331
354
|
def buffer_param(value: str):
|
|
332
|
-
return value.lower() ==
|
|
355
|
+
return value.lower() == "true"
|
|
333
356
|
|
|
334
357
|
bind_nested_functions(ctx, locals())
|
|
335
358
|
|
|
@@ -341,26 +364,26 @@ def clear(self: WatcherCLI, ctx: CommandContext):
|
|
|
341
364
|
self.printHeader()
|
|
342
365
|
|
|
343
366
|
|
|
344
|
-
|
|
367
|
+
# watcher command
|
|
368
|
+
@command("watcher")
|
|
345
369
|
def watcher(self: WatcherCLI, ctx: CommandContext):
|
|
346
370
|
|
|
347
|
-
@command.child(
|
|
371
|
+
@command.child("restart")
|
|
348
372
|
def restart(_stat: str):
|
|
349
373
|
self.watcher.restart_and_wait()
|
|
350
374
|
|
|
351
|
-
@command.child(
|
|
375
|
+
@command.child("schedule")
|
|
352
376
|
def schedule(_stat: str):
|
|
353
|
-
|
|
354
|
-
minutes = ctx.positional(1)
|
|
377
|
+
minutes = ctx.positional(2)
|
|
355
378
|
if minutes is None:
|
|
356
|
-
return self.safePrint(
|
|
379
|
+
return self.safePrint("Usage: watcher schedule <minutes>")
|
|
357
380
|
try:
|
|
358
381
|
minutes = int(minutes)
|
|
359
382
|
except ValueError:
|
|
360
|
-
return self.safePrint(
|
|
383
|
+
return self.safePrint("Minutes must be an integer")
|
|
361
384
|
self.watcher.schedule_restart(minutes)
|
|
362
385
|
|
|
363
|
-
@command.child(
|
|
386
|
+
@command.child("shutdown")
|
|
364
387
|
def shutdown(_stat: str):
|
|
365
388
|
self.watcher.shutdown()
|
|
366
389
|
return True
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
serverwatcher/__init__.py,sha256=Q_afN0XU29n48d6Gf0-LT2lLo3RiTWlHRT6rC_fYbjw,486
|
|
2
2
|
serverwatcher/__main__.py,sha256=XjGcQ_-8p4ttuVYlYEvYKUEvcAsvZUQaEA0mX0ajRbU,3097
|
|
3
|
-
serverwatcher/cli.py,sha256=
|
|
3
|
+
serverwatcher/cli.py,sha256=DL8o-IBl6IqvbyZBfPiNsEKKl6gexOt63T7H44hUmyc,10867
|
|
4
4
|
serverwatcher/validator.py,sha256=aD6GHOz1S5VFC93Mj89WO9vZG1bTjhg7QZbdlvQf4wo,1778
|
|
5
5
|
serverwatcher/watcher.py,sha256=ZiocJpeyhKTGOwYYak36MXNEFpLnpdl9v6BnMPGrBmM,13593
|
|
6
6
|
serverwatcher/configclasses/__init__.py,sha256=YS0KhxtAJGdMcaiFstqTRNxIPLnR7s9v3Tkt5h2uKE4,182
|
|
@@ -12,8 +12,8 @@ serverwatcher/defaultconfigs/messages.yaml,sha256=jSVEl-RYqPHhe5U5KLl8zfJtUBymvy
|
|
|
12
12
|
serverwatcher/defaultconfigs/watcher.yaml,sha256=GlAEKCuzFUSF6wWOqQLFL1CeuPltb1ltfYxFe_FUClw,723
|
|
13
13
|
serverwatcher/tests/test.py,sha256=yvt0ExSpKCwmWGs5tXS_MTmWLcEW6gCOwz0PR4_tgek,1768
|
|
14
14
|
serverwatcher/tests/test2.py,sha256=pDFB3LTZZXdrkofCF9tDGlkUzNXq3AzK225Ww6H2EAw,1702
|
|
15
|
-
serverwatcher-6.
|
|
16
|
-
serverwatcher-6.
|
|
17
|
-
serverwatcher-6.
|
|
18
|
-
serverwatcher-6.
|
|
19
|
-
serverwatcher-6.
|
|
15
|
+
serverwatcher-6.dev4.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
16
|
+
serverwatcher-6.dev4.dist-info/METADATA,sha256=h4va81fCQlOd_0AVoEI0pEt0TcOqTgAjqrGMG1OsPnE,2646
|
|
17
|
+
serverwatcher-6.dev4.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
18
|
+
serverwatcher-6.dev4.dist-info/top_level.txt,sha256=8DJAf8WmmglgtZHkp8aoTriRJ2YPcS4F3DfOIkytASo,14
|
|
19
|
+
serverwatcher-6.dev4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|