serverwatcher 6.dev13__py3-none-any.whl → 6.dev15__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 +6 -6
- serverwatcher/livecli.py +297 -0
- serverwatcher/tests/test.py +106 -39
- serverwatcher/watchercli.py +153 -0
- {serverwatcher-6.dev13.dist-info → serverwatcher-6.dev15.dist-info}/METADATA +1 -1
- {serverwatcher-6.dev13.dist-info → serverwatcher-6.dev15.dist-info}/RECORD +9 -7
- {serverwatcher-6.dev13.dist-info → serverwatcher-6.dev15.dist-info}/WHEEL +0 -0
- {serverwatcher-6.dev13.dist-info → serverwatcher-6.dev15.dist-info}/licenses/LICENSE +0 -0
- {serverwatcher-6.dev13.dist-info → serverwatcher-6.dev15.dist-info}/top_level.txt +0 -0
serverwatcher/__main__.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
from mapres import rprint, setGlobalMaps, ascii_colors
|
|
2
1
|
import time
|
|
3
2
|
import asyncio
|
|
4
3
|
import threading
|
|
5
|
-
|
|
4
|
+
|
|
5
|
+
from mapres import rprint, setGlobalMaps, ascii_colors
|
|
6
6
|
|
|
7
7
|
from hungerlib.validator import (
|
|
8
8
|
ValidationError,
|
|
@@ -14,12 +14,12 @@ from hungerlib.validator import (
|
|
|
14
14
|
|
|
15
15
|
from .watcher import ServerWatcher
|
|
16
16
|
from .validator import validate_all
|
|
17
|
+
from .watchercli import WatcherCLI
|
|
17
18
|
|
|
18
19
|
|
|
19
|
-
# mapres setup
|
|
20
20
|
setGlobalMaps(ascii_colors)
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
|
|
23
23
|
def pretty_report(report: str) -> str:
|
|
24
24
|
lines = report.splitlines()
|
|
25
25
|
out = []
|
|
@@ -43,7 +43,7 @@ def pretty_report(report: str) -> str:
|
|
|
43
43
|
out.append(line)
|
|
44
44
|
return '\n'.join(out)
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
def main():
|
|
48
48
|
try:
|
|
49
49
|
validate_all()
|
|
@@ -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,297 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
import cmd
|
|
3
|
+
import sys
|
|
4
|
+
import termios
|
|
5
|
+
import tty
|
|
6
|
+
import asyncio
|
|
7
|
+
|
|
8
|
+
# ---------------------------------------------------------------------------
|
|
9
|
+
# DSL core
|
|
10
|
+
# ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
COMMANDS: dict[str, 'CommandSpec'] = {}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class ParamSpec:
|
|
17
|
+
name: str
|
|
18
|
+
type_: type
|
|
19
|
+
default: object
|
|
20
|
+
func: callable
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class FlagSpec:
|
|
25
|
+
name: str
|
|
26
|
+
func: callable
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ChildSpec:
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
name,
|
|
33
|
+
func,
|
|
34
|
+
requires_children: bool = False,
|
|
35
|
+
requires_arguments: bool = False,
|
|
36
|
+
requires_params: bool = False,
|
|
37
|
+
requires_flags: bool = False,
|
|
38
|
+
has_arguments: bool = False,
|
|
39
|
+
):
|
|
40
|
+
if has_arguments and requires_children:
|
|
41
|
+
raise ValueError(
|
|
42
|
+
f'Child \'{name}\' cannot both have arguments and require children'
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
self.name = name
|
|
46
|
+
self.func = func
|
|
47
|
+
self.requires_children = requires_children
|
|
48
|
+
self.requires_arguments = requires_arguments
|
|
49
|
+
self.requires_params = requires_params
|
|
50
|
+
self.requires_flags = requires_flags
|
|
51
|
+
self.has_arguments = has_arguments
|
|
52
|
+
|
|
53
|
+
self.params: dict[str, ParamSpec] = {}
|
|
54
|
+
self.flags: dict[str, FlagSpec] = {}
|
|
55
|
+
self.children: dict[str, 'ChildSpec'] = {}
|
|
56
|
+
|
|
57
|
+
self.description = getattr(func, '__description__', None)
|
|
58
|
+
|
|
59
|
+
def param(self, name, type=str, default=None):
|
|
60
|
+
def deco(func):
|
|
61
|
+
pyname = name.replace('-', '_')
|
|
62
|
+
self.params[pyname] = ParamSpec(name, type, default, func)
|
|
63
|
+
return func
|
|
64
|
+
return deco
|
|
65
|
+
|
|
66
|
+
def flag(self, name):
|
|
67
|
+
def deco(func):
|
|
68
|
+
pyname = name.replace('-', '_')
|
|
69
|
+
self.flags[pyname] = FlagSpec(name, func)
|
|
70
|
+
return func
|
|
71
|
+
return deco
|
|
72
|
+
|
|
73
|
+
def child(self, name, **meta):
|
|
74
|
+
def deco(func):
|
|
75
|
+
child = ChildSpec(name, func, **meta)
|
|
76
|
+
self.children[name] = child
|
|
77
|
+
setattr(self, name, child)
|
|
78
|
+
return child
|
|
79
|
+
return deco
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class CommandSpec:
|
|
83
|
+
def __init__(
|
|
84
|
+
self,
|
|
85
|
+
name,
|
|
86
|
+
func,
|
|
87
|
+
requires_children: bool = False,
|
|
88
|
+
requires_arguments: bool = False,
|
|
89
|
+
requires_params: bool = False,
|
|
90
|
+
requires_flags: bool = False,
|
|
91
|
+
has_arguments: bool = False,
|
|
92
|
+
):
|
|
93
|
+
if has_arguments and requires_children:
|
|
94
|
+
raise ValueError(
|
|
95
|
+
f'Command \'{name}\' cannot both have arguments and require children'
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
self.name = name
|
|
99
|
+
self.func = func
|
|
100
|
+
self.requires_children = requires_children
|
|
101
|
+
self.requires_arguments = requires_arguments
|
|
102
|
+
self.requires_params = requires_params
|
|
103
|
+
self.requires_flags = requires_flags
|
|
104
|
+
self.has_arguments = has_arguments
|
|
105
|
+
|
|
106
|
+
self.children: dict[str, ChildSpec] = {}
|
|
107
|
+
self.description = getattr(func, '__description__', None)
|
|
108
|
+
|
|
109
|
+
def child(self, name, **meta):
|
|
110
|
+
def deco(func):
|
|
111
|
+
child = ChildSpec(name, func, **meta)
|
|
112
|
+
self.children[name] = child
|
|
113
|
+
setattr(self, name, child)
|
|
114
|
+
return child
|
|
115
|
+
return deco
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class CommandDSL:
|
|
119
|
+
def __call__(self, name, **meta):
|
|
120
|
+
def deco(func):
|
|
121
|
+
spec = CommandSpec(name, func, **meta)
|
|
122
|
+
COMMANDS[name] = spec
|
|
123
|
+
return spec
|
|
124
|
+
return deco
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
command = CommandDSL()
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
# ---------------------------------------------------------------------------
|
|
131
|
+
# Help generation
|
|
132
|
+
# ---------------------------------------------------------------------------
|
|
133
|
+
|
|
134
|
+
def generate_help(cmd: CommandSpec) -> str:
|
|
135
|
+
lines: list[str] = []
|
|
136
|
+
lines.append(f'{cmd.name}: {cmd.description or 'No description'}')
|
|
137
|
+
|
|
138
|
+
if cmd.children:
|
|
139
|
+
lines.append('')
|
|
140
|
+
lines.append('Children:')
|
|
141
|
+
for cname, child in cmd.children.items():
|
|
142
|
+
lines.append(f' {cname}: {child.description or 'No description'}')
|
|
143
|
+
|
|
144
|
+
if child.params:
|
|
145
|
+
lines.append(' Params:')
|
|
146
|
+
for pname, pspec in child.params.items():
|
|
147
|
+
lines.append(
|
|
148
|
+
f' {pname} (CLI: {pspec.name}, type={pspec.type_.__name__}, default={pspec.default})'
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
if child.flags:
|
|
152
|
+
lines.append(' Flags:')
|
|
153
|
+
for fname, fspec in child.flags.items():
|
|
154
|
+
lines.append(f' --{fspec.name}')
|
|
155
|
+
|
|
156
|
+
return '\n'.join(lines)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
# ---------------------------------------------------------------------------
|
|
160
|
+
# Parsing + dispatch
|
|
161
|
+
# ---------------------------------------------------------------------------
|
|
162
|
+
|
|
163
|
+
@dataclass
|
|
164
|
+
class ParsedArgs:
|
|
165
|
+
subcommand: str | None
|
|
166
|
+
positional: list[str]
|
|
167
|
+
flags: dict[str, bool]
|
|
168
|
+
params: dict[str, str]
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def parse_line(raw: str) -> ParsedArgs:
|
|
172
|
+
raw = raw.strip()
|
|
173
|
+
if not raw:
|
|
174
|
+
return ParsedArgs(None, [], {}, {})
|
|
175
|
+
|
|
176
|
+
parts = raw.split()
|
|
177
|
+
sub = parts[0]
|
|
178
|
+
positional: list[str] = []
|
|
179
|
+
flags: dict[str, bool] = {}
|
|
180
|
+
params: dict[str, str] = {}
|
|
181
|
+
|
|
182
|
+
for token in parts[1:]:
|
|
183
|
+
if token.startswith('--'):
|
|
184
|
+
flags[token[2:]] = True
|
|
185
|
+
elif ':' in token:
|
|
186
|
+
key, value = token.split(':', 1)
|
|
187
|
+
params[key.lower()] = value
|
|
188
|
+
else:
|
|
189
|
+
positional.append(token)
|
|
190
|
+
|
|
191
|
+
return ParsedArgs(sub, positional, flags, params)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def dispatch(cli, line: str):
|
|
195
|
+
parsed = parse_line(line)
|
|
196
|
+
if not parsed.subcommand:
|
|
197
|
+
return
|
|
198
|
+
|
|
199
|
+
cmd = COMMANDS.get(parsed.subcommand)
|
|
200
|
+
if cmd is None:
|
|
201
|
+
return cli.safePrint(f'Unknown command \'{parsed.subcommand}\'')
|
|
202
|
+
|
|
203
|
+
if cmd.requires_children and not cmd.children:
|
|
204
|
+
return cli.safePrint(f'Command \'{cmd.name}\' requires children')
|
|
205
|
+
|
|
206
|
+
cmd.func()
|
|
207
|
+
|
|
208
|
+
if not parsed.positional:
|
|
209
|
+
if cmd.requires_arguments:
|
|
210
|
+
return cli.safePrint(f'Command \'{cmd.name}\' requires an argument')
|
|
211
|
+
return cli.safePrint('Missing subcommand')
|
|
212
|
+
|
|
213
|
+
child_name = parsed.positional[0]
|
|
214
|
+
child = cmd.children.get(child_name)
|
|
215
|
+
if child is None:
|
|
216
|
+
return cli.safePrint(f'Unknown subcommand \'{child_name}\'')
|
|
217
|
+
|
|
218
|
+
arg = parsed.positional[1] if len(parsed.positional) > 1 else None
|
|
219
|
+
|
|
220
|
+
if child.requires_arguments and arg is None:
|
|
221
|
+
return cli.safePrint(f'Subcommand \'{child.name}\' requires an argument')
|
|
222
|
+
|
|
223
|
+
# Inject param/flag functions into child.func globals
|
|
224
|
+
g = child.func.__globals__
|
|
225
|
+
|
|
226
|
+
# Params: use provided value or default, then call original func(value)
|
|
227
|
+
for pname, pspec in child.params.items():
|
|
228
|
+
if pname in parsed.params:
|
|
229
|
+
raw_value = parsed.params[pname]
|
|
230
|
+
try:
|
|
231
|
+
converted = pspec.type_(raw_value)
|
|
232
|
+
except Exception:
|
|
233
|
+
converted = pspec.default
|
|
234
|
+
else:
|
|
235
|
+
converted = pspec.default
|
|
236
|
+
|
|
237
|
+
def make_param_func(func=pspec.func, value=converted):
|
|
238
|
+
def wrapper():
|
|
239
|
+
return func(value)
|
|
240
|
+
return wrapper
|
|
241
|
+
|
|
242
|
+
g[pname] = make_param_func()
|
|
243
|
+
|
|
244
|
+
# Flags: presence → True, absence → False
|
|
245
|
+
for fname, fspec in child.flags.items():
|
|
246
|
+
present = fname in parsed.flags
|
|
247
|
+
|
|
248
|
+
def make_flag_func(func=fspec.func, present=present):
|
|
249
|
+
def wrapper():
|
|
250
|
+
return present
|
|
251
|
+
return wrapper
|
|
252
|
+
|
|
253
|
+
g[fname] = make_flag_func()
|
|
254
|
+
|
|
255
|
+
return child.func(cli, arg)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
# ---------------------------------------------------------------------------
|
|
259
|
+
# Base LiveCLI
|
|
260
|
+
# ---------------------------------------------------------------------------
|
|
261
|
+
|
|
262
|
+
class LiveCLI(cmd.Cmd):
|
|
263
|
+
prompt = ''
|
|
264
|
+
|
|
265
|
+
def safePrint(self, msg: object = '', end: str = '\n') -> None:
|
|
266
|
+
print(str(msg), end=end)
|
|
267
|
+
|
|
268
|
+
def bprint(self, msg: object) -> None:
|
|
269
|
+
self.safePrint(msg)
|
|
270
|
+
|
|
271
|
+
def read_line_raw(self) -> str:
|
|
272
|
+
fd = sys.stdin.fileno()
|
|
273
|
+
old = termios.tcgetattr(fd)
|
|
274
|
+
try:
|
|
275
|
+
tty.setraw(fd)
|
|
276
|
+
chars: list[str] = []
|
|
277
|
+
while True:
|
|
278
|
+
ch = sys.stdin.read(1)
|
|
279
|
+
if ch in ('\r', '\n'):
|
|
280
|
+
break
|
|
281
|
+
chars.append(ch)
|
|
282
|
+
return ''.join(chars)
|
|
283
|
+
finally:
|
|
284
|
+
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
|
285
|
+
|
|
286
|
+
async def run(self):
|
|
287
|
+
while True:
|
|
288
|
+
line = await asyncio.to_thread(self.read_line_raw)
|
|
289
|
+
line = line.strip()
|
|
290
|
+
if not line:
|
|
291
|
+
continue
|
|
292
|
+
stop = await asyncio.to_thread(self.onecmd, line)
|
|
293
|
+
if stop:
|
|
294
|
+
break
|
|
295
|
+
|
|
296
|
+
def onecmd(self, line: str):
|
|
297
|
+
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,153 @@
|
|
|
1
|
+
from hungerlib import utils
|
|
2
|
+
from mapres import res, ascii_colors, setGlobalMaps
|
|
3
|
+
from .watcher import ServerWatcher
|
|
4
|
+
from .livecli import LiveCLI, command
|
|
5
|
+
|
|
6
|
+
setGlobalMaps(ascii_colors)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class WatcherCLI(LiveCLI):
|
|
10
|
+
def __init__(self, watcher: ServerWatcher):
|
|
11
|
+
super().__init__()
|
|
12
|
+
self.watcher = watcher
|
|
13
|
+
self.buffer = utils.Buffer(enabled=True)
|
|
14
|
+
self.outputMode = 'both'
|
|
15
|
+
|
|
16
|
+
def printHeader(self) -> None:
|
|
17
|
+
self.safePrint(res('<yellow>-----------------------------------'))
|
|
18
|
+
self.safePrint(res('<yellow>-------- <aqua>ServerWatcher CLI <yellow>--------'))
|
|
19
|
+
self.safePrint(res('<yellow>-----------------------------------'))
|
|
20
|
+
self.safePrint('\n')
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# ---------------------------------------------------------------------------
|
|
24
|
+
# stats command
|
|
25
|
+
# ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
@command('stats', requires_children=True)
|
|
28
|
+
def stats():
|
|
29
|
+
__description__ = 'Retrieve and print server statistics'
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@stats.child('get', requires_arguments=False)
|
|
33
|
+
def stats_get(self, arg=None):
|
|
34
|
+
__description__ = 'Retrieve and print all or specific server statistics'
|
|
35
|
+
|
|
36
|
+
self.watcher.server.refresh()
|
|
37
|
+
|
|
38
|
+
if arg is not None:
|
|
39
|
+
gb = not raw()
|
|
40
|
+
if formatted():
|
|
41
|
+
fmt = True
|
|
42
|
+
elif no_formatted():
|
|
43
|
+
fmt = False
|
|
44
|
+
else:
|
|
45
|
+
fmt = True
|
|
46
|
+
|
|
47
|
+
unit = ''
|
|
48
|
+
match arg:
|
|
49
|
+
case 'ram':
|
|
50
|
+
value = self.watcher.server.getRAM(rounding=rounding(), gb=gb)
|
|
51
|
+
name = 'RAM'
|
|
52
|
+
unit = ' GB' if gb else ' MB'
|
|
53
|
+
case 'cpu':
|
|
54
|
+
value = self.watcher.server.getCPU(rounding=rounding())
|
|
55
|
+
name = 'CPU'
|
|
56
|
+
unit = '%'
|
|
57
|
+
case 'uptime':
|
|
58
|
+
value = self.watcher.server.getUptime(formatted=fmt)
|
|
59
|
+
name = 'Uptime'
|
|
60
|
+
unit = '' if fmt else 'ms'
|
|
61
|
+
case 'tps':
|
|
62
|
+
value = self.watcher.server.getTPS(rounding=rounding(), mode=mode())
|
|
63
|
+
name = 'TPS'
|
|
64
|
+
case 'players':
|
|
65
|
+
value = self.watcher.server.getPlayers()
|
|
66
|
+
name = 'Players'
|
|
67
|
+
case 'version':
|
|
68
|
+
value = self.watcher.server.version
|
|
69
|
+
name = 'Minecraft version'
|
|
70
|
+
case 'platform':
|
|
71
|
+
value = self.watcher.server.platform
|
|
72
|
+
name = 'Server platform'
|
|
73
|
+
case _:
|
|
74
|
+
return self.safePrint('Unknown stat')
|
|
75
|
+
|
|
76
|
+
self.bprint(f'{name}: {value}{unit}')
|
|
77
|
+
else:
|
|
78
|
+
return # later: print all stats for `stats get`
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@stats.get.param('rounding', type=int, default=2)
|
|
82
|
+
def rounding(value: int):
|
|
83
|
+
__description__ = 'placeholder'
|
|
84
|
+
return value
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@stats.get.param('mode', type=str, default='current')
|
|
88
|
+
def mode(value: str):
|
|
89
|
+
return value
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@stats.get.flag('raw')
|
|
93
|
+
def raw():
|
|
94
|
+
__description__ = 'placeholder'
|
|
95
|
+
return True # DSL overrides with presence/absence
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@stats.get.flag('formatted')
|
|
99
|
+
def formatted():
|
|
100
|
+
return True # DSL overrides with presence/absence
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@stats.get.flag('no-formatted')
|
|
104
|
+
def no_formatted():
|
|
105
|
+
return False # DSL overrides with presence/absence
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# ---------------------------------------------------------------------------
|
|
109
|
+
# view command
|
|
110
|
+
# ---------------------------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
@command('view', requires_children=True)
|
|
113
|
+
def view():
|
|
114
|
+
__description__ = 'Switch terminal view'
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@view.child('cli')
|
|
118
|
+
def view_cli(self):
|
|
119
|
+
__description__ = 'Switch to CLI view'
|
|
120
|
+
|
|
121
|
+
self.watcher.router.disableOriginOutput()
|
|
122
|
+
self.outputMode = 'cli'
|
|
123
|
+
self.buffer.enabled = True
|
|
124
|
+
utils.clearTerminal()
|
|
125
|
+
self.printHeader()
|
|
126
|
+
for msg in self.buffer.captured:
|
|
127
|
+
self.safePrint(msg)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@view.child('watcher')
|
|
131
|
+
def view_watcher(self):
|
|
132
|
+
__description__ = 'Switch to Watcher view'
|
|
133
|
+
|
|
134
|
+
self.watcher.router.enableOriginOutput()
|
|
135
|
+
self.outputMode = 'both'
|
|
136
|
+
utils.clearTerminal()
|
|
137
|
+
self.printHeader()
|
|
138
|
+
for msg in self.watcher.router.buffer.captured:
|
|
139
|
+
self.safePrint(msg)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
# ---------------------------------------------------------------------------
|
|
143
|
+
# clear command
|
|
144
|
+
# ---------------------------------------------------------------------------
|
|
145
|
+
|
|
146
|
+
@command('clear')
|
|
147
|
+
def clear():
|
|
148
|
+
__description__ = 'Clear the CLI terminal'
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
@clear.flag('no-buffer')
|
|
152
|
+
def no_buffer():
|
|
153
|
+
return False # DSL overrides with presence/absence
|
|
@@ -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=5geaZQKgLSP1pxnpQPgFy9zAwyyPyTInlbcFxb9OiEw,3016
|
|
3
3
|
serverwatcher/cli.py,sha256=b77rWfjQuqHiRGaVQj-O5EygJGdufMasRBUUqKcasAM,9887
|
|
4
|
+
serverwatcher/livecli.py,sha256=ncACuZw5U6pzPjJevUttzyIcYrYcWFWSNzFsB2gApIw,8564
|
|
4
5
|
serverwatcher/validator.py,sha256=aD6GHOz1S5VFC93Mj89WO9vZG1bTjhg7QZbdlvQf4wo,1778
|
|
5
6
|
serverwatcher/watcher.py,sha256=ZiocJpeyhKTGOwYYak36MXNEFpLnpdl9v6BnMPGrBmM,13593
|
|
7
|
+
serverwatcher/watchercli.py,sha256=xJGtwT2x67P0ql-v2uIYnbYfzPuPlf-Nx_oE1XN6qkQ,4394
|
|
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.dev15.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
18
|
+
serverwatcher-6.dev15.dist-info/METADATA,sha256=kGDyPA8TBY8dwi5EN18Cqk0GSI22orLvs6JmWr1ALt8,2647
|
|
19
|
+
serverwatcher-6.dev15.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
20
|
+
serverwatcher-6.dev15.dist-info/top_level.txt,sha256=8DJAf8WmmglgtZHkp8aoTriRJ2YPcS4F3DfOIkytASo,14
|
|
21
|
+
serverwatcher-6.dev15.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|