serverwatcher 6.dev13__py3-none-any.whl → 6.dev14__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/__main__.py +2 -2
- serverwatcher/livecli.py +284 -0
- serverwatcher/tests/test.py +106 -39
- serverwatcher/watchercli.py +198 -0
- {serverwatcher-6.dev13.dist-info → serverwatcher-6.dev14.dist-info}/METADATA +1 -1
- {serverwatcher-6.dev13.dist-info → serverwatcher-6.dev14.dist-info}/RECORD +9 -7
- {serverwatcher-6.dev13.dist-info → serverwatcher-6.dev14.dist-info}/WHEEL +0 -0
- {serverwatcher-6.dev13.dist-info → serverwatcher-6.dev14.dist-info}/licenses/LICENSE +0 -0
- {serverwatcher-6.dev13.dist-info → serverwatcher-6.dev14.dist-info}/top_level.txt +0 -0
serverwatcher/__main__.py
CHANGED
|
@@ -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
|
|
serverwatcher/livecli.py
ADDED
|
@@ -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)
|
serverwatcher/tests/test.py
CHANGED
|
@@ -1,57 +1,124 @@
|
|
|
1
|
-
@command('
|
|
2
|
-
|
|
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.
|
|
3
4
|
|
|
4
|
-
@
|
|
5
|
-
def
|
|
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
|
+
|
|
6
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)
|
|
7
58
|
|
|
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
59
|
|
|
39
60
|
@stats.get.param('rounding', type=int, default=2)
|
|
40
|
-
def rounding(value):
|
|
61
|
+
def rounding(value: int):
|
|
62
|
+
__description__ = 'placeholder'
|
|
41
63
|
return value
|
|
42
64
|
|
|
43
65
|
@stats.get.param('mode', type=str, default='current')
|
|
44
|
-
def mode(value):
|
|
66
|
+
def mode(value: str):
|
|
45
67
|
return value
|
|
46
68
|
|
|
47
69
|
@stats.get.flag('raw')
|
|
48
70
|
def raw():
|
|
49
|
-
|
|
71
|
+
__description__ = 'placeholder'
|
|
72
|
+
return True # should mean that without flag, is False, with is True
|
|
50
73
|
|
|
51
74
|
@stats.get.flag('formatted')
|
|
52
75
|
def formatted():
|
|
53
|
-
return True
|
|
76
|
+
return True # should mean that without flag, is False, with is True
|
|
54
77
|
|
|
55
78
|
@stats.get.flag('no-formatted')
|
|
56
79
|
def no_formatted():
|
|
57
|
-
return False
|
|
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))
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
serverwatcher/__init__.py,sha256=Q_afN0XU29n48d6Gf0-LT2lLo3RiTWlHRT6rC_fYbjw,486
|
|
2
|
-
serverwatcher/__main__.py,sha256=
|
|
2
|
+
serverwatcher/__main__.py,sha256=6xLRz606w5LIDMdKrw7Z7TBDubX0RzeAIZI80MmwOP4,3083
|
|
3
3
|
serverwatcher/cli.py,sha256=b77rWfjQuqHiRGaVQj-O5EygJGdufMasRBUUqKcasAM,9887
|
|
4
|
+
serverwatcher/livecli.py,sha256=y70-2nhCOqXLUmBpweQSQXFmhvpggZdjBcN7jNYjOrk,7400
|
|
4
5
|
serverwatcher/validator.py,sha256=aD6GHOz1S5VFC93Mj89WO9vZG1bTjhg7QZbdlvQf4wo,1778
|
|
5
6
|
serverwatcher/watcher.py,sha256=ZiocJpeyhKTGOwYYak36MXNEFpLnpdl9v6BnMPGrBmM,13593
|
|
7
|
+
serverwatcher/watchercli.py,sha256=kSLeurUiwkb46Lv43c6V-uq-Z9s31Qcbi5Sp2amLT4Y,4810
|
|
6
8
|
serverwatcher/configclasses/__init__.py,sha256=YS0KhxtAJGdMcaiFstqTRNxIPLnR7s9v3Tkt5h2uKE4,182
|
|
7
9
|
serverwatcher/configclasses/config.py,sha256=1Upor55QFE4UIOqL9ZYa4U5kbda48GG9HWdRXg8mb_Y,2418
|
|
8
10
|
serverwatcher/configclasses/messages.py,sha256=iOkCvwagngW4aeWD5I6hi6cqqDkzEsFJsLHSuqK-P1s,3467
|
|
@@ -10,10 +12,10 @@ serverwatcher/configclasses/watcher.py,sha256=OIAYQZF9hD9e5FbcaYV5N9CAdCZoLgXSqA
|
|
|
10
12
|
serverwatcher/defaultconfigs/config.yaml,sha256=yrJ59Jx0F4TJzE1EdBF5IKPCDqs09XcbKHFfkYGJ1ys,721
|
|
11
13
|
serverwatcher/defaultconfigs/messages.yaml,sha256=jSVEl-RYqPHhe5U5KLl8zfJtUBymvyE74hmzH0RFC_Y,3177
|
|
12
14
|
serverwatcher/defaultconfigs/watcher.yaml,sha256=GlAEKCuzFUSF6wWOqQLFL1CeuPltb1ltfYxFe_FUClw,723
|
|
13
|
-
serverwatcher/tests/test.py,sha256=
|
|
15
|
+
serverwatcher/tests/test.py,sha256=EPJXmIJ4lLfuT-xFuAqrh52O_yYTQdQl17Djzr72v18,3711
|
|
14
16
|
serverwatcher/tests/test2.py,sha256=pDFB3LTZZXdrkofCF9tDGlkUzNXq3AzK225Ww6H2EAw,1702
|
|
15
|
-
serverwatcher-6.
|
|
16
|
-
serverwatcher-6.
|
|
17
|
-
serverwatcher-6.
|
|
18
|
-
serverwatcher-6.
|
|
19
|
-
serverwatcher-6.
|
|
17
|
+
serverwatcher-6.dev14.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
18
|
+
serverwatcher-6.dev14.dist-info/METADATA,sha256=37wTrYkhIEz6TNCjCqXNMRsSvrm1uMADfxSG0mZJgFk,2647
|
|
19
|
+
serverwatcher-6.dev14.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
20
|
+
serverwatcher-6.dev14.dist-info/top_level.txt,sha256=8DJAf8WmmglgtZHkp8aoTriRJ2YPcS4F3DfOIkytASo,14
|
|
21
|
+
serverwatcher-6.dev14.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|