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