serverwatcher 6.dev12__tar.gz → 6.dev14__tar.gz
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-6.dev12/src/serverwatcher.egg-info → serverwatcher-6.dev14}/PKG-INFO +1 -1
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/pyproject.toml +1 -1
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/__main__.py +2 -2
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/cli.py +12 -12
- serverwatcher-6.dev14/src/serverwatcher/livecli.py +284 -0
- serverwatcher-6.dev14/src/serverwatcher/tests/test.py +124 -0
- serverwatcher-6.dev14/src/serverwatcher/watchercli.py +198 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14/src/serverwatcher.egg-info}/PKG-INFO +1 -1
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher.egg-info/SOURCES.txt +2 -0
- serverwatcher-6.dev12/src/serverwatcher/tests/test.py +0 -57
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/LICENSE +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/README.md +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/setup.cfg +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/__init__.py +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/configclasses/__init__.py +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/configclasses/config.py +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/configclasses/messages.py +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/configclasses/watcher.py +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/defaultconfigs/config.yaml +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/defaultconfigs/messages.yaml +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/defaultconfigs/watcher.yaml +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/tests/test2.py +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/validator.py +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/watcher.py +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher.egg-info/dependency_links.txt +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher.egg-info/requires.txt +0 -0
- {serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher.egg-info/top_level.txt +0 -0
|
@@ -2,7 +2,7 @@ from mapres import rprint, setGlobalMaps, ascii_colors
|
|
|
2
2
|
import time
|
|
3
3
|
import asyncio
|
|
4
4
|
import threading
|
|
5
|
-
from .
|
|
5
|
+
from .watchercli import WatcherCLI
|
|
6
6
|
|
|
7
7
|
from hungerlib.validator import (
|
|
8
8
|
ValidationError,
|
|
@@ -90,7 +90,7 @@ def main():
|
|
|
90
90
|
rprint('<aqua>Please check your configuration and environment.')
|
|
91
91
|
return
|
|
92
92
|
|
|
93
|
-
#
|
|
93
|
+
# Start watcher thread
|
|
94
94
|
watcher = ServerWatcher()
|
|
95
95
|
threading.Thread(target=watcher.run, daemon=True).start()
|
|
96
96
|
|
|
@@ -122,7 +122,7 @@ def dispatch(cli: CLIBase, line: str):
|
|
|
122
122
|
if cmd is None:
|
|
123
123
|
return cli.safePrint(f'Unknown command {parsed.subcommand}')
|
|
124
124
|
|
|
125
|
-
cmd.func(
|
|
125
|
+
cmd.func()
|
|
126
126
|
|
|
127
127
|
if not parsed.positional:
|
|
128
128
|
return cli.safePrint('Missing subcommand')
|
|
@@ -238,12 +238,12 @@ class WatcherCLI(cmd.Cmd, CLIBase):
|
|
|
238
238
|
|
|
239
239
|
# stats command
|
|
240
240
|
@command('stats')
|
|
241
|
-
def stats(
|
|
241
|
+
def stats():
|
|
242
242
|
pass
|
|
243
243
|
|
|
244
244
|
|
|
245
245
|
@stats.child('get')
|
|
246
|
-
def stats_get(self
|
|
246
|
+
def stats_get(self, stat: str, **kwargs):
|
|
247
247
|
rounding = kwargs['rounding']
|
|
248
248
|
mode = kwargs['mode']
|
|
249
249
|
raw = kwargs['raw']
|
|
@@ -319,12 +319,12 @@ def no_formatted():
|
|
|
319
319
|
|
|
320
320
|
# view command
|
|
321
321
|
@command('view')
|
|
322
|
-
def view(
|
|
322
|
+
def view():
|
|
323
323
|
pass
|
|
324
324
|
|
|
325
325
|
|
|
326
326
|
@view.child('cli')
|
|
327
|
-
def view_cli(self
|
|
327
|
+
def view_cli(self, _arg: str, **kwargs):
|
|
328
328
|
self.watcher.router.disableOriginOutput()
|
|
329
329
|
self.outputMode = 'cli'
|
|
330
330
|
self.buffer.enabled = True
|
|
@@ -335,7 +335,7 @@ def view_cli(self: WatcherCLI, _arg: str, **kwargs):
|
|
|
335
335
|
|
|
336
336
|
|
|
337
337
|
@view.child('watcher')
|
|
338
|
-
def view_watcher(self
|
|
338
|
+
def view_watcher(self, _arg: str, **kwargs):
|
|
339
339
|
self.watcher.router.enableOriginOutput()
|
|
340
340
|
self.outputMode = 'both'
|
|
341
341
|
utils.clearTerminal()
|
|
@@ -346,12 +346,12 @@ def view_watcher(self: WatcherCLI, _arg: str, **kwargs):
|
|
|
346
346
|
|
|
347
347
|
# clear command
|
|
348
348
|
@command('clear')
|
|
349
|
-
def clear(
|
|
349
|
+
def clear():
|
|
350
350
|
pass
|
|
351
351
|
|
|
352
352
|
|
|
353
353
|
@clear.child('buffer')
|
|
354
|
-
def clear_buffer(self
|
|
354
|
+
def clear_buffer(self, _arg: str, **kwargs):
|
|
355
355
|
buffer = kwargs['buffer']
|
|
356
356
|
|
|
357
357
|
if buffer():
|
|
@@ -368,17 +368,17 @@ def buffer(value: str):
|
|
|
368
368
|
|
|
369
369
|
# watcher command
|
|
370
370
|
@command('watcher')
|
|
371
|
-
def watcher(
|
|
371
|
+
def watcher():
|
|
372
372
|
pass
|
|
373
373
|
|
|
374
374
|
|
|
375
375
|
@watcher.child('restart')
|
|
376
|
-
def watcher_restart(self
|
|
376
|
+
def watcher_restart(self, _arg: str, **kwargs):
|
|
377
377
|
self.watcher.restart_and_wait()
|
|
378
378
|
|
|
379
379
|
|
|
380
380
|
@watcher.child('schedule')
|
|
381
|
-
def watcher_schedule(self
|
|
381
|
+
def watcher_schedule(self, _arg: str, **kwargs):
|
|
382
382
|
minutes = kwargs['minutes']
|
|
383
383
|
|
|
384
384
|
val = minutes()
|
|
@@ -388,7 +388,7 @@ def watcher_schedule(self: WatcherCLI, _arg: str, **kwargs):
|
|
|
388
388
|
|
|
389
389
|
|
|
390
390
|
@watcher.child('shutdown')
|
|
391
|
-
def watcher_shutdown(self
|
|
391
|
+
def watcher_shutdown(self, _arg: str, **kwargs):
|
|
392
392
|
self.watcher.shutdown()
|
|
393
393
|
return True
|
|
394
394
|
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# livecli.py
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
# global registry
|
|
6
|
+
COMMANDS: dict[str, "CommandSpec"] = {}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class ParamSpec:
|
|
11
|
+
name: str
|
|
12
|
+
type_: type
|
|
13
|
+
default: object
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class FlagSpec:
|
|
18
|
+
name: str
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ChildSpec:
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
name,
|
|
25
|
+
func,
|
|
26
|
+
requires_children=False,
|
|
27
|
+
requires_arguments=False,
|
|
28
|
+
requires_params=False,
|
|
29
|
+
requires_flags=False,
|
|
30
|
+
has_arguments=False,
|
|
31
|
+
):
|
|
32
|
+
if has_arguments and requires_children:
|
|
33
|
+
raise ValueError(
|
|
34
|
+
f"Child '{name}' cannot both have arguments and require children"
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
self.name = name
|
|
38
|
+
self.func = func
|
|
39
|
+
self.requires_children = requires_children
|
|
40
|
+
self.requires_arguments = requires_arguments
|
|
41
|
+
self.requires_params = requires_params
|
|
42
|
+
self.requires_flags = requires_flags
|
|
43
|
+
self.has_arguments = has_arguments
|
|
44
|
+
|
|
45
|
+
self.params = {}
|
|
46
|
+
self.flags = {}
|
|
47
|
+
self.children = {}
|
|
48
|
+
|
|
49
|
+
self.description = getattr(func, "__description__", None)
|
|
50
|
+
|
|
51
|
+
def param(self, name, type=str, default=None):
|
|
52
|
+
def deco(func):
|
|
53
|
+
pyname = name.replace("-", "_")
|
|
54
|
+
self.params[pyname] = ParamSpec(name, type, default)
|
|
55
|
+
return func
|
|
56
|
+
return deco
|
|
57
|
+
|
|
58
|
+
def flag(self, name):
|
|
59
|
+
def deco(func):
|
|
60
|
+
pyname = name.replace("-", "_")
|
|
61
|
+
self.flags[pyname] = FlagSpec(name)
|
|
62
|
+
return func
|
|
63
|
+
return deco
|
|
64
|
+
|
|
65
|
+
def child(self, name, **meta):
|
|
66
|
+
def deco(func):
|
|
67
|
+
child = ChildSpec(name, func, **meta)
|
|
68
|
+
self.children[name] = child
|
|
69
|
+
setattr(self, name, child)
|
|
70
|
+
return child
|
|
71
|
+
return deco
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class CommandSpec:
|
|
75
|
+
def __init__(
|
|
76
|
+
self,
|
|
77
|
+
name,
|
|
78
|
+
func,
|
|
79
|
+
requires_children=False,
|
|
80
|
+
requires_arguments=False,
|
|
81
|
+
requires_params=False,
|
|
82
|
+
requires_flags=False,
|
|
83
|
+
has_arguments=False,
|
|
84
|
+
):
|
|
85
|
+
if has_arguments and requires_children:
|
|
86
|
+
raise ValueError(
|
|
87
|
+
f"Command '{name}' cannot both have arguments and require children"
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
self.name = name
|
|
91
|
+
self.func = func
|
|
92
|
+
self.requires_children = requires_children
|
|
93
|
+
self.requires_arguments = requires_arguments
|
|
94
|
+
self.requires_params = requires_params
|
|
95
|
+
self.requires_flags = requires_flags
|
|
96
|
+
self.has_arguments = has_arguments
|
|
97
|
+
|
|
98
|
+
self.children = {}
|
|
99
|
+
self.description = getattr(func, "__description__", None)
|
|
100
|
+
|
|
101
|
+
def child(self, name, **meta):
|
|
102
|
+
def deco(func):
|
|
103
|
+
child = ChildSpec(name, func, **meta)
|
|
104
|
+
self.children[name] = child
|
|
105
|
+
setattr(self, name, child)
|
|
106
|
+
return child
|
|
107
|
+
return deco
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class CommandDSL:
|
|
111
|
+
def __call__(self, name, **meta):
|
|
112
|
+
def deco(func):
|
|
113
|
+
spec = CommandSpec(name, func, **meta)
|
|
114
|
+
COMMANDS[name] = spec
|
|
115
|
+
return spec
|
|
116
|
+
return deco
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
command = CommandDSL()
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
# -------------------------
|
|
123
|
+
# HELP GENERATION
|
|
124
|
+
# -------------------------
|
|
125
|
+
|
|
126
|
+
def generate_help(cmd: CommandSpec) -> str:
|
|
127
|
+
lines = []
|
|
128
|
+
|
|
129
|
+
lines.append(f"{cmd.name}: {cmd.description or 'No description'}")
|
|
130
|
+
|
|
131
|
+
if cmd.children:
|
|
132
|
+
lines.append("")
|
|
133
|
+
lines.append("Children:")
|
|
134
|
+
for cname, child in cmd.children.items():
|
|
135
|
+
lines.append(f" {cname}: {child.description or 'No description'}")
|
|
136
|
+
|
|
137
|
+
if child.params:
|
|
138
|
+
lines.append(" Params:")
|
|
139
|
+
for pname, pspec in child.params.items():
|
|
140
|
+
lines.append(
|
|
141
|
+
f" {pname} (CLI: {pspec.name}, type={pspec.type_.__name__}, default={pspec.default})"
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
if child.flags:
|
|
145
|
+
lines.append(" Flags:")
|
|
146
|
+
for fname, fspec in child.flags.items():
|
|
147
|
+
lines.append(f" --{fspec.name}")
|
|
148
|
+
|
|
149
|
+
return "\n".join(lines)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
# -------------------------
|
|
153
|
+
# PARSING + DISPATCH
|
|
154
|
+
# -------------------------
|
|
155
|
+
|
|
156
|
+
@dataclass
|
|
157
|
+
class ParsedArgs:
|
|
158
|
+
subcommand: str | None
|
|
159
|
+
positional: list[str]
|
|
160
|
+
flags: dict[str, bool]
|
|
161
|
+
params: dict[str, str]
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def parse_line(raw: str) -> ParsedArgs:
|
|
165
|
+
raw = raw.strip()
|
|
166
|
+
if not raw:
|
|
167
|
+
return ParsedArgs(None, [], {}, {})
|
|
168
|
+
|
|
169
|
+
parts = raw.split()
|
|
170
|
+
sub = parts[0]
|
|
171
|
+
positional = []
|
|
172
|
+
flags = {}
|
|
173
|
+
params = {}
|
|
174
|
+
|
|
175
|
+
for token in parts[1:]:
|
|
176
|
+
if token.startswith("--"):
|
|
177
|
+
flags[token[2:]] = True
|
|
178
|
+
elif ":" in token:
|
|
179
|
+
key, value = token.split(":", 1)
|
|
180
|
+
params[key.lower()] = value
|
|
181
|
+
else:
|
|
182
|
+
positional.append(token)
|
|
183
|
+
|
|
184
|
+
return ParsedArgs(sub, positional, flags, params)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def dispatch(cli, line: str):
|
|
188
|
+
parsed = parse_line(line)
|
|
189
|
+
if not parsed.subcommand:
|
|
190
|
+
return
|
|
191
|
+
|
|
192
|
+
cmd = COMMANDS.get(parsed.subcommand)
|
|
193
|
+
if cmd is None:
|
|
194
|
+
return cli.safePrint(f"Unknown command '{parsed.subcommand}'")
|
|
195
|
+
|
|
196
|
+
if cmd.requires_children and not cmd.children:
|
|
197
|
+
return cli.safePrint(f"Command '{cmd.name}' requires children")
|
|
198
|
+
|
|
199
|
+
cmd.func()
|
|
200
|
+
|
|
201
|
+
if not parsed.positional:
|
|
202
|
+
if cmd.requires_arguments:
|
|
203
|
+
return cli.safePrint(f"Command '{cmd.name}' requires an argument")
|
|
204
|
+
return cli.safePrint("Missing subcommand")
|
|
205
|
+
|
|
206
|
+
child_name = parsed.positional[0]
|
|
207
|
+
child = cmd.children.get(child_name)
|
|
208
|
+
if child is None:
|
|
209
|
+
return cli.safePrint(f"Unknown subcommand '{child_name}'")
|
|
210
|
+
|
|
211
|
+
arg = parsed.positional[1] if len(parsed.positional) > 1 else None
|
|
212
|
+
|
|
213
|
+
if child.requires_arguments and arg is None:
|
|
214
|
+
return cli.safePrint(f"Subcommand '{child.name}' requires an argument")
|
|
215
|
+
|
|
216
|
+
wrappers = {}
|
|
217
|
+
|
|
218
|
+
for pname, pspec in child.params.items():
|
|
219
|
+
if pname in parsed.params:
|
|
220
|
+
raw = parsed.params[pname]
|
|
221
|
+
|
|
222
|
+
def make_param(raw=raw, pspec=pspec):
|
|
223
|
+
try:
|
|
224
|
+
return pspec.type_(raw)
|
|
225
|
+
except Exception:
|
|
226
|
+
return pspec.default
|
|
227
|
+
|
|
228
|
+
wrappers[pname] = make_param
|
|
229
|
+
else:
|
|
230
|
+
wrappers[pname] = lambda pspec=pspec: pspec.default
|
|
231
|
+
|
|
232
|
+
for fname, _fspec in child.flags.items():
|
|
233
|
+
present = fname in parsed.flags
|
|
234
|
+
wrappers[fname] = lambda present=present: present
|
|
235
|
+
|
|
236
|
+
return child.func(cli, arg, **wrappers)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
# -------------------------
|
|
240
|
+
# BASE LiveCLI (reusable)
|
|
241
|
+
# -------------------------
|
|
242
|
+
|
|
243
|
+
import cmd
|
|
244
|
+
import sys
|
|
245
|
+
import termios
|
|
246
|
+
import tty
|
|
247
|
+
import asyncio
|
|
248
|
+
|
|
249
|
+
class LiveCLI(cmd.Cmd):
|
|
250
|
+
prompt = ""
|
|
251
|
+
|
|
252
|
+
def safePrint(self, msg="", end="\n"):
|
|
253
|
+
print(str(msg), end=end)
|
|
254
|
+
|
|
255
|
+
def bprint(self, msg):
|
|
256
|
+
self.safePrint(msg)
|
|
257
|
+
|
|
258
|
+
def read_line_raw(self):
|
|
259
|
+
fd = sys.stdin.fileno()
|
|
260
|
+
old = termios.tcgetattr(fd)
|
|
261
|
+
try:
|
|
262
|
+
tty.setraw(fd)
|
|
263
|
+
chars = []
|
|
264
|
+
while True:
|
|
265
|
+
ch = sys.stdin.read(1)
|
|
266
|
+
if ch in ("\r", "\n"):
|
|
267
|
+
break
|
|
268
|
+
chars.append(ch)
|
|
269
|
+
return "".join(chars)
|
|
270
|
+
finally:
|
|
271
|
+
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
|
272
|
+
|
|
273
|
+
async def run(self):
|
|
274
|
+
while True:
|
|
275
|
+
line = await asyncio.to_thread(self.read_line_raw)
|
|
276
|
+
line = line.strip()
|
|
277
|
+
if not line:
|
|
278
|
+
continue
|
|
279
|
+
stop = await asyncio.to_thread(self.onecmd, line)
|
|
280
|
+
if stop:
|
|
281
|
+
break
|
|
282
|
+
|
|
283
|
+
def onecmd(self, line):
|
|
284
|
+
return dispatch(self, line)
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# @command (name='name', requires_children=False, requires_arguments=False, requires_params=False, requires_flags=False)
|
|
2
|
+
# @command.child(name='name', requires_children=False, requires_arguments=False, requires_params=False, requires_flags=False)
|
|
3
|
+
# a command or child command that has arguments cannot have children. it can have params and flags though.
|
|
4
|
+
|
|
5
|
+
@command('stats', requires_children=True)
|
|
6
|
+
def stats():
|
|
7
|
+
__description__ = 'Retrieve and print server statistics'
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@stats.child('get', requires_arguments=False)
|
|
11
|
+
def stats_get(self, arg=None):
|
|
12
|
+
__description__ = 'Retrieve and print all or specific server statistics'
|
|
13
|
+
|
|
14
|
+
self.watcher.server.refresh()
|
|
15
|
+
|
|
16
|
+
if arg is not None:
|
|
17
|
+
# flag setup
|
|
18
|
+
gb = not raw()
|
|
19
|
+
if formatted():
|
|
20
|
+
fmt = True
|
|
21
|
+
elif no_formatted():
|
|
22
|
+
fmt = False
|
|
23
|
+
else:
|
|
24
|
+
fmt = True
|
|
25
|
+
|
|
26
|
+
unit = ''
|
|
27
|
+
match arg:
|
|
28
|
+
case 'ram':
|
|
29
|
+
value = self.watcher.server.getRAM(rounding=rounding(), gb=gb)
|
|
30
|
+
name = 'RAM'
|
|
31
|
+
unit = ' GB' if gb else ' MB'
|
|
32
|
+
case 'cpu':
|
|
33
|
+
value = self.watcher.server.getCPU(rounding=rounding())
|
|
34
|
+
name = 'CPU'
|
|
35
|
+
unit = '%'
|
|
36
|
+
case 'uptime':
|
|
37
|
+
value = self.watcher.server.getUptime(formatted=fmt)
|
|
38
|
+
name = 'Uptime'
|
|
39
|
+
unit = '' if fmt else 'ms'
|
|
40
|
+
case 'tps':
|
|
41
|
+
value = self.watcher.server.getTPS(rounding=rounding(), mode=mode())
|
|
42
|
+
name = 'TPS'
|
|
43
|
+
case 'players':
|
|
44
|
+
value = self.watcher.server.getPlayers()
|
|
45
|
+
name = 'Players'
|
|
46
|
+
case 'version':
|
|
47
|
+
value = self.watcher.server.version
|
|
48
|
+
name = 'Minecraft version'
|
|
49
|
+
case 'platform':
|
|
50
|
+
value = self.watcher.server.platform
|
|
51
|
+
name = 'Server platform'
|
|
52
|
+
case _:
|
|
53
|
+
return self.safePrint('Unknown stat')
|
|
54
|
+
|
|
55
|
+
self.bprint(f'{name}: {value}{unit}')
|
|
56
|
+
else:
|
|
57
|
+
return # later i'll make it print ALL the stats, this is valid, because `stats get` (correct usage)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@stats.get.param('rounding', type=int, default=2)
|
|
61
|
+
def rounding(value: int):
|
|
62
|
+
__description__ = 'placeholder'
|
|
63
|
+
return value
|
|
64
|
+
|
|
65
|
+
@stats.get.param('mode', type=str, default='current')
|
|
66
|
+
def mode(value: str):
|
|
67
|
+
return value
|
|
68
|
+
|
|
69
|
+
@stats.get.flag('raw')
|
|
70
|
+
def raw():
|
|
71
|
+
__description__ = 'placeholder'
|
|
72
|
+
return True # should mean that without flag, is False, with is True
|
|
73
|
+
|
|
74
|
+
@stats.get.flag('formatted')
|
|
75
|
+
def formatted():
|
|
76
|
+
return True # should mean that without flag, is False, with is True
|
|
77
|
+
|
|
78
|
+
@stats.get.flag('no-formatted')
|
|
79
|
+
def no_formatted():
|
|
80
|
+
return False # should mean that without flag, is True, with is False
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
# view command
|
|
87
|
+
@command('view', requires_children=True)
|
|
88
|
+
def view():
|
|
89
|
+
__description__ = 'Switch terminal view'
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@view.child('cli')
|
|
93
|
+
def view_cli(self):
|
|
94
|
+
__description__ = 'Switch to CLI view'
|
|
95
|
+
|
|
96
|
+
self.watcher.router.disableOriginOutput()
|
|
97
|
+
self.outputMode = 'cli'
|
|
98
|
+
self.buffer.enabled = True
|
|
99
|
+
utils.clearTerminal()
|
|
100
|
+
self.printHeader()
|
|
101
|
+
for msg in self.buffer.captured:
|
|
102
|
+
self.safePrint(msg)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@view.child('watcher')
|
|
106
|
+
def view_watcher(self):
|
|
107
|
+
__description__ = 'Switch to Watcher view'
|
|
108
|
+
|
|
109
|
+
self.watcher.router.enableOriginOutput()
|
|
110
|
+
self.outputMode = 'both'
|
|
111
|
+
utils.clearTerminal()
|
|
112
|
+
self.printHeader()
|
|
113
|
+
for msg in self.watcher.router.buffer.captured:
|
|
114
|
+
self.safePrint(msg)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# clear command
|
|
118
|
+
@command('clear')
|
|
119
|
+
def clear():
|
|
120
|
+
__description__ = 'Clear the CLI terminal'
|
|
121
|
+
|
|
122
|
+
@clear.flag('no-buffer')
|
|
123
|
+
def no_buffer():
|
|
124
|
+
return False # should clear buffer by default, with flag should not
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# watchercli.py
|
|
2
|
+
|
|
3
|
+
from .livecli import (
|
|
4
|
+
LiveCLI,
|
|
5
|
+
command,
|
|
6
|
+
COMMANDS,
|
|
7
|
+
generate_help,
|
|
8
|
+
)
|
|
9
|
+
from hungerlib import utils
|
|
10
|
+
from mapres import res, maps, setGlobalMaps
|
|
11
|
+
from .watcher import ServerWatcher
|
|
12
|
+
|
|
13
|
+
setGlobalMaps(maps.ascii_colors)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# -------------------------
|
|
17
|
+
# WatcherCLI
|
|
18
|
+
# -------------------------
|
|
19
|
+
|
|
20
|
+
class WatcherCLI(LiveCLI):
|
|
21
|
+
def __init__(self, watcher: ServerWatcher):
|
|
22
|
+
super().__init__()
|
|
23
|
+
self.watcher = watcher
|
|
24
|
+
self.buffer = utils.Buffer(enabled=True)
|
|
25
|
+
self.outputMode = "both"
|
|
26
|
+
|
|
27
|
+
def printHeader(self):
|
|
28
|
+
self.safePrint(res("<yellow>-----------------------------------"))
|
|
29
|
+
self.safePrint(res("<yellow>-------- <aqua>ServerWatcher CLI <yellow>--------"))
|
|
30
|
+
self.safePrint(res("<yellow>-----------------------------------"))
|
|
31
|
+
self.safePrint("")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# -------------------------
|
|
35
|
+
# Commands
|
|
36
|
+
# -------------------------
|
|
37
|
+
|
|
38
|
+
@command("stats", requires_children=True)
|
|
39
|
+
def stats():
|
|
40
|
+
__description__ = "Retrieve and print server statistics"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@stats.child("get", requires_arguments=False)
|
|
44
|
+
def stats_get(self: WatcherCLI, arg=None, **kwargs):
|
|
45
|
+
__description__ = "Retrieve and print all or specific server statistics"
|
|
46
|
+
|
|
47
|
+
rounding = kwargs["rounding"]
|
|
48
|
+
mode = kwargs["mode"]
|
|
49
|
+
raw = kwargs["raw"]
|
|
50
|
+
formatted = kwargs["formatted"]
|
|
51
|
+
no_formatted = kwargs["no_formatted"]
|
|
52
|
+
|
|
53
|
+
self.watcher.server.refresh()
|
|
54
|
+
|
|
55
|
+
if arg is None:
|
|
56
|
+
return # later: print all stats
|
|
57
|
+
|
|
58
|
+
gb = not raw()
|
|
59
|
+
fmt = formatted() or not no_formatted()
|
|
60
|
+
|
|
61
|
+
unit = ""
|
|
62
|
+
match arg:
|
|
63
|
+
case "ram":
|
|
64
|
+
value = self.watcher.server.getRAM(rounding=rounding(), gb=gb)
|
|
65
|
+
name = "RAM"
|
|
66
|
+
unit = " GB" if gb else " MB"
|
|
67
|
+
case "cpu":
|
|
68
|
+
value = self.watcher.server.getCPU(rounding=rounding())
|
|
69
|
+
name = "CPU"
|
|
70
|
+
unit = "%"
|
|
71
|
+
case "uptime":
|
|
72
|
+
value = self.watcher.server.getUptime(formatted=fmt)
|
|
73
|
+
name = "Uptime"
|
|
74
|
+
unit = "" if fmt else "ms"
|
|
75
|
+
case "tps":
|
|
76
|
+
value = self.watcher.server.getTPS(rounding=rounding(), mode=mode())
|
|
77
|
+
name = "TPS"
|
|
78
|
+
case "players":
|
|
79
|
+
value = self.watcher.server.getPlayers()
|
|
80
|
+
name = "Players"
|
|
81
|
+
case "version":
|
|
82
|
+
value = self.watcher.server.version
|
|
83
|
+
name = "Minecraft version"
|
|
84
|
+
case "platform":
|
|
85
|
+
value = self.watcher.server.platform
|
|
86
|
+
name = "Server platform"
|
|
87
|
+
case _:
|
|
88
|
+
return self.safePrint("Unknown stat")
|
|
89
|
+
|
|
90
|
+
self.bprint(f"{name}: {value}{unit}")
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@stats.get.param("rounding", type=int, default=2)
|
|
94
|
+
def rounding(value):
|
|
95
|
+
return value
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@stats.get.param("mode", type=str, default="current")
|
|
99
|
+
def mode(value):
|
|
100
|
+
return value
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@stats.get.flag("raw")
|
|
104
|
+
def raw():
|
|
105
|
+
return True
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@stats.get.flag("formatted")
|
|
109
|
+
def formatted():
|
|
110
|
+
return True
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@stats.get.flag("no-formatted")
|
|
114
|
+
def no_formatted():
|
|
115
|
+
return False
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# -------------------------
|
|
119
|
+
# view command
|
|
120
|
+
# -------------------------
|
|
121
|
+
|
|
122
|
+
@command("view", requires_children=True)
|
|
123
|
+
def view():
|
|
124
|
+
__description__ = "Switch terminal view"
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@view.child("cli")
|
|
128
|
+
def view_cli(self: WatcherCLI, arg=None, **kwargs):
|
|
129
|
+
__description__ = "Switch to CLI view"
|
|
130
|
+
|
|
131
|
+
self.watcher.router.disableOriginOutput()
|
|
132
|
+
self.outputMode = "cli"
|
|
133
|
+
utils.clearTerminal()
|
|
134
|
+
self.printHeader()
|
|
135
|
+
for msg in self.buffer.captured:
|
|
136
|
+
self.safePrint(msg)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@view.child("watcher")
|
|
140
|
+
def view_watcher(self: WatcherCLI, arg=None, **kwargs):
|
|
141
|
+
__description__ = "Switch to Watcher view"
|
|
142
|
+
|
|
143
|
+
self.watcher.router.enableOriginOutput()
|
|
144
|
+
self.outputMode = "both"
|
|
145
|
+
utils.clearTerminal()
|
|
146
|
+
self.printHeader()
|
|
147
|
+
for msg in self.watcher.router.buffer.captured:
|
|
148
|
+
self.safePrint(msg)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
# -------------------------
|
|
152
|
+
# clear command
|
|
153
|
+
# -------------------------
|
|
154
|
+
|
|
155
|
+
@command("clear")
|
|
156
|
+
def clear():
|
|
157
|
+
__description__ = "Clear the CLI terminal"
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
@clear.child("buffer")
|
|
161
|
+
def clear_buffer(self: WatcherCLI, arg=None, **kwargs):
|
|
162
|
+
__description__ = "Clear or keep the CLI buffer"
|
|
163
|
+
|
|
164
|
+
no_buffer = kwargs["no_buffer"]
|
|
165
|
+
|
|
166
|
+
if not no_buffer():
|
|
167
|
+
self.buffer.clear()
|
|
168
|
+
|
|
169
|
+
utils.clearTerminal()
|
|
170
|
+
self.printHeader()
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
@clear_buffer.flag("no-buffer")
|
|
174
|
+
def no_buffer():
|
|
175
|
+
return True
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
# -------------------------
|
|
179
|
+
# help command
|
|
180
|
+
# -------------------------
|
|
181
|
+
|
|
182
|
+
@command("help", requires_arguments=True)
|
|
183
|
+
def help():
|
|
184
|
+
__description__ = "Show help for a command"
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
@help.child("command", requires_arguments=True)
|
|
188
|
+
def help_command(self: WatcherCLI, cmd_name=None, **kwargs):
|
|
189
|
+
__description__ = "Show help for a specific command"
|
|
190
|
+
|
|
191
|
+
if cmd_name is None:
|
|
192
|
+
return self.safePrint("Usage: help command <name>")
|
|
193
|
+
|
|
194
|
+
cmd = COMMANDS.get(cmd_name)
|
|
195
|
+
if not cmd:
|
|
196
|
+
return self.safePrint(f"Unknown command '{cmd_name}'")
|
|
197
|
+
|
|
198
|
+
self.safePrint(generate_help(cmd))
|
|
@@ -4,8 +4,10 @@ pyproject.toml
|
|
|
4
4
|
src/serverwatcher/__init__.py
|
|
5
5
|
src/serverwatcher/__main__.py
|
|
6
6
|
src/serverwatcher/cli.py
|
|
7
|
+
src/serverwatcher/livecli.py
|
|
7
8
|
src/serverwatcher/validator.py
|
|
8
9
|
src/serverwatcher/watcher.py
|
|
10
|
+
src/serverwatcher/watchercli.py
|
|
9
11
|
src/serverwatcher.egg-info/PKG-INFO
|
|
10
12
|
src/serverwatcher.egg-info/SOURCES.txt
|
|
11
13
|
src/serverwatcher.egg-info/dependency_links.txt
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
@command('stats')
|
|
2
|
-
def stats(): pass
|
|
3
|
-
|
|
4
|
-
@stats.child('get')
|
|
5
|
-
def get(stat: str):
|
|
6
|
-
self.watcher.server.refresh()
|
|
7
|
-
|
|
8
|
-
unit = ''
|
|
9
|
-
match stat:
|
|
10
|
-
case 'ram':
|
|
11
|
-
value = self.watcher.server.getRAM(rounding=rounding(), gb=raw())
|
|
12
|
-
name = 'RAM'
|
|
13
|
-
unit = ' GB' if gb else ' MB'
|
|
14
|
-
case 'cpu':
|
|
15
|
-
value = self.watcher.server.getCPU(rounding=rounding())
|
|
16
|
-
name = 'CPU'
|
|
17
|
-
unit = '%'
|
|
18
|
-
case 'uptime':
|
|
19
|
-
value = self.watcher.server.getUptime(formatted=formatted())
|
|
20
|
-
name = 'Uptime'
|
|
21
|
-
unit = '' if fmt else 'ms'
|
|
22
|
-
case 'tps':
|
|
23
|
-
value = self.watcher.server.getTPS(rounding=rounding(), mode=mode())
|
|
24
|
-
name = 'TPS'
|
|
25
|
-
case 'players':
|
|
26
|
-
value = self.watcher.server.getPlayers()
|
|
27
|
-
name = 'Players'
|
|
28
|
-
case 'version':
|
|
29
|
-
value = self.watcher.server.version
|
|
30
|
-
name = 'Minecraft version'
|
|
31
|
-
case 'platform':
|
|
32
|
-
value = self.watcher.server.platform
|
|
33
|
-
name = 'Server platform'
|
|
34
|
-
case _:
|
|
35
|
-
return self.safePrint('Unknown stat')
|
|
36
|
-
|
|
37
|
-
self.bprint(f'{name}: {value}{unit}')
|
|
38
|
-
|
|
39
|
-
@stats.get.param('rounding', type=int, default=2)
|
|
40
|
-
def rounding(value):
|
|
41
|
-
return value
|
|
42
|
-
|
|
43
|
-
@stats.get.param('mode', type=str, default='current')
|
|
44
|
-
def mode(value):
|
|
45
|
-
return value
|
|
46
|
-
|
|
47
|
-
@stats.get.flag('raw')
|
|
48
|
-
def raw():
|
|
49
|
-
return False
|
|
50
|
-
|
|
51
|
-
@stats.get.flag('formatted')
|
|
52
|
-
def formatted():
|
|
53
|
-
return True
|
|
54
|
-
|
|
55
|
-
@stats.get.flag('no-formatted')
|
|
56
|
-
def no_formatted():
|
|
57
|
-
return False
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/defaultconfigs/config.yaml
RENAMED
|
File without changes
|
{serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/defaultconfigs/messages.yaml
RENAMED
|
File without changes
|
{serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher/defaultconfigs/watcher.yaml
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{serverwatcher-6.dev12 → serverwatcher-6.dev14}/src/serverwatcher.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|