serverwatcher 6.dev20__py3-none-any.whl → 6.dev21__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/livecli.py +112 -40
- serverwatcher/tests/test.py +16 -88
- serverwatcher/tests/test2.py +314 -48
- serverwatcher/watchercli.py +42 -7
- {serverwatcher-6.dev20.dist-info → serverwatcher-6.dev21.dist-info}/METADATA +1 -1
- {serverwatcher-6.dev20.dist-info → serverwatcher-6.dev21.dist-info}/RECORD +9 -10
- serverwatcher/cli.py +0 -398
- {serverwatcher-6.dev20.dist-info → serverwatcher-6.dev21.dist-info}/WHEEL +0 -0
- {serverwatcher-6.dev20.dist-info → serverwatcher-6.dev21.dist-info}/licenses/LICENSE +0 -0
- {serverwatcher-6.dev20.dist-info → serverwatcher-6.dev21.dist-info}/top_level.txt +0 -0
serverwatcher/livecli.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
# livecli.py
|
|
2
|
-
|
|
3
1
|
from dataclasses import dataclass
|
|
4
2
|
import cmd
|
|
5
3
|
import sys
|
|
@@ -138,6 +136,95 @@ class CommandDSL:
|
|
|
138
136
|
command = CommandDSL()
|
|
139
137
|
|
|
140
138
|
|
|
139
|
+
@dataclass
|
|
140
|
+
class ParsedArgs:
|
|
141
|
+
subcommand: str | None
|
|
142
|
+
positional: list[str]
|
|
143
|
+
flags: dict[str, bool]
|
|
144
|
+
params: dict[str, str]
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def parse_line(raw: str) -> ParsedArgs:
|
|
148
|
+
raw = raw.strip()
|
|
149
|
+
if not raw:
|
|
150
|
+
return ParsedArgs(None, [], {}, {})
|
|
151
|
+
|
|
152
|
+
parts = raw.split()
|
|
153
|
+
sub = parts[0]
|
|
154
|
+
positional = []
|
|
155
|
+
flags = {}
|
|
156
|
+
params = {}
|
|
157
|
+
|
|
158
|
+
for token in parts[1:]:
|
|
159
|
+
if token.startswith('--'):
|
|
160
|
+
flags[token[2:]] = True
|
|
161
|
+
elif ':' in token:
|
|
162
|
+
key, value = token.split(':', 1)
|
|
163
|
+
params[key.lower()] = value
|
|
164
|
+
else:
|
|
165
|
+
positional.append(token)
|
|
166
|
+
|
|
167
|
+
return ParsedArgs(sub, positional, flags, params)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def parse_docstring_args(doc: str | None) -> dict[str, list[tuple[str, bool]]]:
|
|
171
|
+
"""
|
|
172
|
+
Returns {arg_name: [(item_name, required_bool), ...]}.
|
|
173
|
+
Syntax:
|
|
174
|
+
|
|
175
|
+
args:
|
|
176
|
+
ram [rounding] [--raw]
|
|
177
|
+
cpu [rounding]
|
|
178
|
+
uptime [--raw]
|
|
179
|
+
tps [rounding] [mode]
|
|
180
|
+
players
|
|
181
|
+
version
|
|
182
|
+
platform
|
|
183
|
+
"""
|
|
184
|
+
result: dict[str, list[tuple[str, bool]]] = {}
|
|
185
|
+
if not doc:
|
|
186
|
+
return result
|
|
187
|
+
|
|
188
|
+
lines = doc.splitlines()
|
|
189
|
+
in_args = False
|
|
190
|
+
|
|
191
|
+
for line in lines:
|
|
192
|
+
stripped = line.strip()
|
|
193
|
+
if not stripped:
|
|
194
|
+
continue
|
|
195
|
+
|
|
196
|
+
if stripped.startswith('args:'):
|
|
197
|
+
in_args = True
|
|
198
|
+
continue
|
|
199
|
+
|
|
200
|
+
if not in_args:
|
|
201
|
+
continue
|
|
202
|
+
|
|
203
|
+
# stop if we hit something that looks like non-args section
|
|
204
|
+
if ':' not in stripped and ' ' not in stripped and stripped != 'args:':
|
|
205
|
+
# still allow bare "players" etc
|
|
206
|
+
parts = stripped.split()
|
|
207
|
+
else:
|
|
208
|
+
parts = stripped.split()
|
|
209
|
+
|
|
210
|
+
if not parts:
|
|
211
|
+
continue
|
|
212
|
+
|
|
213
|
+
name = parts[0]
|
|
214
|
+
items: list[tuple[str, bool]] = []
|
|
215
|
+
|
|
216
|
+
for token in parts[1:]:
|
|
217
|
+
required = True
|
|
218
|
+
if token.startswith('[') and token.endswith(']'):
|
|
219
|
+
required = False
|
|
220
|
+
token = token[1:-1]
|
|
221
|
+
items.append((token, required))
|
|
222
|
+
|
|
223
|
+
result[name] = items
|
|
224
|
+
|
|
225
|
+
return result
|
|
226
|
+
|
|
227
|
+
|
|
141
228
|
def generate_help(obj):
|
|
142
229
|
if isinstance(obj, CommandSpec):
|
|
143
230
|
lines = []
|
|
@@ -169,6 +256,20 @@ def generate_help(obj):
|
|
|
169
256
|
lines.append(f'{obj.name}: {obj.description or "No description"}')
|
|
170
257
|
lines.append(f'Usage: {obj.name} [arg] [--flags] [params]')
|
|
171
258
|
|
|
259
|
+
# docstring-based args
|
|
260
|
+
args_meta = parse_docstring_args(obj.func.__doc__)
|
|
261
|
+
if args_meta:
|
|
262
|
+
lines.append('')
|
|
263
|
+
lines.append('Args:')
|
|
264
|
+
for arg_name, items in args_meta.items():
|
|
265
|
+
line = f' {arg_name}'
|
|
266
|
+
for item_name, required in items:
|
|
267
|
+
if required:
|
|
268
|
+
line += f' {item_name}'
|
|
269
|
+
else:
|
|
270
|
+
line += f' [{item_name}]'
|
|
271
|
+
lines.append(line)
|
|
272
|
+
|
|
172
273
|
if obj.flags:
|
|
173
274
|
lines.append('')
|
|
174
275
|
lines.append('Flags:')
|
|
@@ -186,61 +287,30 @@ def generate_help(obj):
|
|
|
186
287
|
return 'No help available'
|
|
187
288
|
|
|
188
289
|
|
|
189
|
-
@dataclass
|
|
190
|
-
class ParsedArgs:
|
|
191
|
-
subcommand: str | None
|
|
192
|
-
positional: list[str]
|
|
193
|
-
flags: dict[str, bool]
|
|
194
|
-
params: dict[str, str]
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
def parse_line(raw: str) -> ParsedArgs:
|
|
198
|
-
raw = raw.strip()
|
|
199
|
-
if not raw:
|
|
200
|
-
return ParsedArgs(None, [], {}, {})
|
|
201
|
-
|
|
202
|
-
parts = raw.split()
|
|
203
|
-
sub = parts[0]
|
|
204
|
-
positional = []
|
|
205
|
-
flags = {}
|
|
206
|
-
params = {}
|
|
207
|
-
|
|
208
|
-
for token in parts[1:]:
|
|
209
|
-
if token.startswith('--'):
|
|
210
|
-
flags[token[2:]] = True
|
|
211
|
-
elif ':' in token:
|
|
212
|
-
key, value = token.split(':', 1)
|
|
213
|
-
params[key.lower()] = value
|
|
214
|
-
else:
|
|
215
|
-
positional.append(token)
|
|
216
|
-
|
|
217
|
-
return ParsedArgs(sub, positional, flags, params)
|
|
218
|
-
|
|
219
|
-
|
|
220
290
|
def dispatch(cli, line: str):
|
|
221
291
|
parsed = parse_line(line)
|
|
222
292
|
if not parsed.subcommand:
|
|
223
293
|
return
|
|
224
294
|
|
|
225
|
-
|
|
226
|
-
if
|
|
295
|
+
cmd_spec = COMMANDS.get(parsed.subcommand)
|
|
296
|
+
if cmd_spec is None:
|
|
227
297
|
return cli.safePrint(f'Unknown command {parsed.subcommand}')
|
|
228
298
|
|
|
229
299
|
if 'help' in parsed.flags and not parsed.positional:
|
|
230
|
-
return cli.safePrint(generate_help(
|
|
300
|
+
return cli.safePrint(generate_help(cmd_spec))
|
|
231
301
|
|
|
232
|
-
|
|
302
|
+
cmd_spec.func()
|
|
233
303
|
|
|
234
|
-
if not
|
|
304
|
+
if not cmd_spec.children:
|
|
235
305
|
if 'help' in parsed.flags:
|
|
236
|
-
return cli.safePrint(generate_help(
|
|
306
|
+
return cli.safePrint(generate_help(cmd_spec))
|
|
237
307
|
return
|
|
238
308
|
|
|
239
309
|
if not parsed.positional:
|
|
240
310
|
return cli.safePrint('Missing subcommand')
|
|
241
311
|
|
|
242
312
|
child_name = parsed.positional[0]
|
|
243
|
-
child =
|
|
313
|
+
child = cmd_spec.children.get(child_name)
|
|
244
314
|
if child is None:
|
|
245
315
|
return cli.safePrint(f'Unknown subcommand {child_name}')
|
|
246
316
|
|
|
@@ -254,6 +324,7 @@ def dispatch(cli, line: str):
|
|
|
254
324
|
|
|
255
325
|
g = child.func.__globals__
|
|
256
326
|
|
|
327
|
+
# params
|
|
257
328
|
for pname, pspec in child.params.items():
|
|
258
329
|
if pname in parsed.params:
|
|
259
330
|
raw_value = parsed.params[pname]
|
|
@@ -271,6 +342,7 @@ def dispatch(cli, line: str):
|
|
|
271
342
|
|
|
272
343
|
g[pname] = make_param_func()
|
|
273
344
|
|
|
345
|
+
# flags
|
|
274
346
|
for fname, fspec in child.flags.items():
|
|
275
347
|
present = fname in parsed.flags
|
|
276
348
|
|
serverwatcher/tests/test.py
CHANGED
|
@@ -1,27 +1,22 @@
|
|
|
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
1
|
@stats.child('get', requires_arguments=False)
|
|
11
2
|
def stats_get(self, arg=None):
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
'''
|
|
4
|
+
args:
|
|
5
|
+
ram [rounding] [--raw]
|
|
6
|
+
cpu [rounding]
|
|
7
|
+
uptime [--raw]
|
|
8
|
+
tps [rounding] [mode]
|
|
9
|
+
players
|
|
10
|
+
version
|
|
11
|
+
platform
|
|
12
|
+
'''
|
|
13
|
+
stats_get.__description__ = 'Retrieve and print all or specific server statistics'
|
|
14
|
+
|
|
14
15
|
self.watcher.server.refresh()
|
|
15
|
-
|
|
16
|
+
|
|
16
17
|
if arg is not None:
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if formatted():
|
|
20
|
-
fmt = True
|
|
21
|
-
elif no_formatted():
|
|
22
|
-
fmt = False
|
|
23
|
-
else:
|
|
24
|
-
fmt = True
|
|
18
|
+
gb = raw()
|
|
19
|
+
fmt = not raw()
|
|
25
20
|
|
|
26
21
|
unit = ''
|
|
27
22
|
match arg:
|
|
@@ -54,71 +49,4 @@ def stats_get(self, arg=None):
|
|
|
54
49
|
|
|
55
50
|
self.bprint(f'{name}: {value}{unit}')
|
|
56
51
|
else:
|
|
57
|
-
return
|
|
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
|
|
52
|
+
return
|
serverwatcher/tests/test2.py
CHANGED
|
@@ -1,57 +1,323 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import asyncio
|
|
2
|
+
import argparse
|
|
3
|
+
import cmd
|
|
4
|
+
import sys
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
import termios
|
|
7
|
+
import tty
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return value
|
|
9
|
+
from hungerlib import utils
|
|
10
|
+
from mapres import res, maps, setGlobalMaps
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
def mode(value):
|
|
12
|
-
return value
|
|
12
|
+
from .watcher import ServerWatcher
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
def raw():
|
|
16
|
-
return False
|
|
14
|
+
setGlobalMaps(maps.ascii_colors)
|
|
17
15
|
|
|
18
|
-
@flag('formatted')
|
|
19
|
-
def formatted():
|
|
20
|
-
return True
|
|
21
16
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
class BufferingStdout:
|
|
18
|
+
def __init__(self, buffer, real_stdout):
|
|
19
|
+
self.buffer = buffer
|
|
20
|
+
self.real_stdout = real_stdout
|
|
21
|
+
|
|
22
|
+
def write(self, text):
|
|
23
|
+
# capture only non-empty lines
|
|
24
|
+
if self.buffer.enabled and text.strip():
|
|
25
|
+
self.buffer.captured.append(text.rstrip("\n"))
|
|
26
|
+
self.real_stdout.write(text)
|
|
27
|
+
|
|
28
|
+
def flush(self):
|
|
29
|
+
self.real_stdout.flush()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# we keep this only for the positional stat name
|
|
33
|
+
stats_parser = argparse.ArgumentParser(add_help=False)
|
|
34
|
+
stats_parser.add_argument(
|
|
35
|
+
"stat",
|
|
36
|
+
choices=["cpu", "ram", "uptime", "tps", "players"],
|
|
37
|
+
help="Statistic to retrieve"
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
schedule_parser = argparse.ArgumentParser()
|
|
41
|
+
schedule_parser.add_argument(
|
|
42
|
+
"minutes",
|
|
43
|
+
type=int,
|
|
44
|
+
help="Minutes until restart"
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class WatcherCLI(cmd.Cmd):
|
|
49
|
+
prompt = ""
|
|
50
|
+
|
|
51
|
+
def __init__(self, watcher: ServerWatcher):
|
|
52
|
+
self.watcher = watcher
|
|
53
|
+
|
|
54
|
+
# buffer for CLI output
|
|
55
|
+
self.buffer = utils.Buffer(enabled=True)
|
|
56
|
+
|
|
57
|
+
# real stdout (terminal)
|
|
58
|
+
self.real_stdout = sys.stdout
|
|
59
|
+
|
|
60
|
+
# CLI stdout goes through BufferingStdout → captured in self.buffer
|
|
61
|
+
self.cli_stdout = BufferingStdout(self.buffer, self.real_stdout)
|
|
62
|
+
|
|
63
|
+
# permanently override cmd.Cmd stdout
|
|
64
|
+
super().__init__(stdout=self.cli_stdout)
|
|
65
|
+
|
|
66
|
+
# output mode: both | cli | silent
|
|
67
|
+
self.outputMode = "both"
|
|
68
|
+
|
|
69
|
+
# disable rawinput in order to control input manually
|
|
70
|
+
self.use_rawinput = False
|
|
71
|
+
|
|
72
|
+
def read_line_raw(self):
|
|
73
|
+
fd = sys.stdin.fileno()
|
|
74
|
+
old = termios.tcgetattr(fd)
|
|
75
|
+
|
|
76
|
+
try:
|
|
77
|
+
tty.setraw(fd)
|
|
78
|
+
chars = []
|
|
79
|
+
while True:
|
|
80
|
+
ch = sys.stdin.read(1)
|
|
81
|
+
if ch in ("\r", "\n"):
|
|
82
|
+
break
|
|
83
|
+
chars.append(ch)
|
|
84
|
+
|
|
85
|
+
return "".join(chars)
|
|
86
|
+
finally:
|
|
87
|
+
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
|
88
|
+
|
|
89
|
+
async def run(self):
|
|
90
|
+
while True:
|
|
91
|
+
# read input without echo (Pterodactyl will echo anyway)
|
|
92
|
+
line = await asyncio.to_thread(self.read_line_raw)
|
|
93
|
+
line = line.strip()
|
|
94
|
+
|
|
95
|
+
if not line:
|
|
96
|
+
continue
|
|
97
|
+
|
|
98
|
+
# capture command (Pterodactyl already printed it)
|
|
99
|
+
if self.buffer.enabled and not line.startswith("view"):
|
|
100
|
+
self.buffer.captured.append(line)
|
|
101
|
+
|
|
102
|
+
# run command (self.stdout is already overridden)
|
|
103
|
+
stop = await asyncio.to_thread(self.onecmd, line)
|
|
104
|
+
|
|
105
|
+
if stop:
|
|
106
|
+
break
|
|
107
|
+
|
|
108
|
+
def safePrint(self, msg="", end="\n"):
|
|
109
|
+
self.real_stdout.write(str(msg) + end)
|
|
110
|
+
self.real_stdout.flush()
|
|
111
|
+
|
|
112
|
+
def printHeader(self):
|
|
113
|
+
self.safePrint(res("<yellow>-----------------------------------"))
|
|
114
|
+
self.safePrint(res("<yellow>-------- <aqua>ServerWatcher CLI <yellow>--------"))
|
|
115
|
+
self.safePrint(res("<yellow>-----------------------------------"))
|
|
116
|
+
self.safePrint("\n")
|
|
117
|
+
|
|
118
|
+
# ---------------------------------------------------------
|
|
119
|
+
# VIEW COMMANDS
|
|
120
|
+
# ---------------------------------------------------------
|
|
121
|
+
def do_view(self, arg):
|
|
122
|
+
"""
|
|
123
|
+
view cli
|
|
124
|
+
view watcher
|
|
125
|
+
"""
|
|
126
|
+
if arg == "cli":
|
|
127
|
+
self._view_cli()
|
|
128
|
+
elif arg == "watcher":
|
|
129
|
+
self._view_watcher()
|
|
130
|
+
else:
|
|
131
|
+
self.safePrint("Usage: view <cli|watcher>")
|
|
132
|
+
|
|
133
|
+
def _view_cli(self):
|
|
134
|
+
self.watcher.router.disableOriginOutput()
|
|
135
|
+
self.outputMode = "cli"
|
|
136
|
+
|
|
137
|
+
# CLI capture always enabled
|
|
138
|
+
self.buffer.enabled = True
|
|
139
|
+
|
|
140
|
+
utils.clearTerminal()
|
|
141
|
+
self.safePrint("", end="")
|
|
142
|
+
|
|
143
|
+
# header is always printed fresh, not stored in buffer
|
|
144
|
+
self.printHeader()
|
|
145
|
+
|
|
146
|
+
# replay captured CLI output
|
|
147
|
+
for msg in self.buffer.captured:
|
|
148
|
+
self.safePrint(msg)
|
|
25
149
|
|
|
150
|
+
def _view_watcher(self):
|
|
151
|
+
self.watcher.router.enableOriginOutput()
|
|
152
|
+
self.outputMode = "both"
|
|
153
|
+
|
|
154
|
+
# do NOT touch self.buffer.enabled here
|
|
155
|
+
|
|
156
|
+
utils.clearTerminal()
|
|
157
|
+
self.safePrint("", end="")
|
|
158
|
+
|
|
159
|
+
# replay watcher output
|
|
160
|
+
for msg in self.watcher.router.buffer.captured:
|
|
161
|
+
self.safePrint(msg)
|
|
162
|
+
|
|
163
|
+
# ---------------------------------------------------------
|
|
164
|
+
# STATS COMMANDS
|
|
165
|
+
# ---------------------------------------------------------
|
|
166
|
+
def do_stats(self, arg):
|
|
167
|
+
"""
|
|
168
|
+
stats get <cpu|ram|uptime|tps|players> [gb:true|false] [rounding:<int>]
|
|
169
|
+
Example:
|
|
170
|
+
stats get ram
|
|
171
|
+
stats get ram gb:false
|
|
172
|
+
stats get ram gb:false rounding:1
|
|
173
|
+
"""
|
|
174
|
+
parts = arg.split()
|
|
175
|
+
if len(parts) == 0 or parts[0] != "get":
|
|
176
|
+
self.safePrint("Usage: stats get <cpu|ram|uptime|tps|players> [gb:true|false] [rounding:<int>]")
|
|
177
|
+
return
|
|
178
|
+
|
|
179
|
+
# strip the leading "get"
|
|
180
|
+
tokens = parts[1:]
|
|
181
|
+
if not tokens:
|
|
182
|
+
self.safePrint("Usage: stats get <cpu|ram|uptime|tps|players> [gb:true|false] [rounding:<int>]")
|
|
183
|
+
return
|
|
184
|
+
|
|
185
|
+
# first token is the stat name, validated by stats_parser
|
|
186
|
+
try:
|
|
187
|
+
args = stats_parser.parse_args([tokens[0]])
|
|
188
|
+
stat = args.stat
|
|
189
|
+
except SystemExit:
|
|
190
|
+
self.safePrint("Usage: stats get <cpu|ram|uptime|tps|players> [gb:<bool>] [rounding:<int>] [formatted:<bool>]")
|
|
191
|
+
return
|
|
192
|
+
|
|
193
|
+
# defaults
|
|
194
|
+
gb = True
|
|
195
|
+
rounding = 2
|
|
196
|
+
formatted = True
|
|
197
|
+
|
|
198
|
+
# parse optional key:value tokens
|
|
199
|
+
for token in tokens[1:]:
|
|
200
|
+
if ":" not in token:
|
|
201
|
+
continue
|
|
202
|
+
key, value = token.split(":", 1)
|
|
203
|
+
key = key.strip().lower()
|
|
204
|
+
value = value.strip().lower()
|
|
205
|
+
|
|
206
|
+
if key == "gb":
|
|
207
|
+
gb = value == "true"
|
|
208
|
+
elif key == "rounding":
|
|
209
|
+
try:
|
|
210
|
+
rounding = int(value)
|
|
211
|
+
except ValueError:
|
|
212
|
+
self.safePrint("rounding must be an integer")
|
|
213
|
+
return
|
|
214
|
+
elif key == 'formatted':
|
|
215
|
+
formatted = value == 'true'
|
|
216
|
+
|
|
217
|
+
self._stats_get(stat, gb=gb, rounding=rounding, formatted=formatted)
|
|
218
|
+
|
|
219
|
+
def _stats_get(self, stat: str, gb: bool=True, rounding: int=2, formatted: bool=True):
|
|
26
220
|
self.watcher.server.refresh()
|
|
27
221
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
222
|
+
if stat == "ram":
|
|
223
|
+
value = self.watcher.server.getRAM(rounding=rounding, gb=gb)
|
|
224
|
+
stat_name = 'RAM'
|
|
225
|
+
unit = ' GB' if gb else ' MB'
|
|
226
|
+
elif stat == "cpu":
|
|
227
|
+
value = self.watcher.server.getCPU(rounding=rounding)
|
|
228
|
+
stat_name = 'CPU'
|
|
229
|
+
unit = '%'
|
|
230
|
+
elif stat == "uptime":
|
|
231
|
+
value = self.watcher.server.getUptime(formatted=formatted)
|
|
232
|
+
stat_name = 'Uptime'
|
|
233
|
+
unit = '' if formatted else 'ms'
|
|
234
|
+
elif stat == "tps":
|
|
235
|
+
value = self.watcher.server.getTPS()
|
|
236
|
+
stat_name = 'TPS'
|
|
237
|
+
unit = ''
|
|
238
|
+
elif stat == "players":
|
|
239
|
+
value = self.watcher.server.getPlayers()
|
|
240
|
+
stat_name = 'Players'
|
|
241
|
+
unit = ''
|
|
242
|
+
else:
|
|
243
|
+
self.safePrint("Unknown stat")
|
|
244
|
+
return
|
|
245
|
+
|
|
246
|
+
self.bprint(f'{stat_name}: {value}{unit}')
|
|
247
|
+
|
|
248
|
+
# ---------------------------------------------------------
|
|
249
|
+
# CLEAR COMMAND
|
|
250
|
+
# ---------------------------------------------------------
|
|
251
|
+
def do_clear(self, arg):
|
|
252
|
+
"""
|
|
253
|
+
clear buffer:true
|
|
254
|
+
clear buffer:false
|
|
255
|
+
"""
|
|
256
|
+
parts = arg.split()
|
|
257
|
+
|
|
258
|
+
# default: buffer:false
|
|
259
|
+
clear_buffer = False
|
|
260
|
+
|
|
261
|
+
# parse key:value pairs
|
|
262
|
+
for token in parts:
|
|
263
|
+
if ":" not in token:
|
|
264
|
+
continue
|
|
265
|
+
key, value = token.split(":", 1)
|
|
266
|
+
key = key.strip().lower()
|
|
267
|
+
value = value.strip().lower()
|
|
268
|
+
|
|
269
|
+
if key == "buffer":
|
|
270
|
+
clear_buffer = (value == "true")
|
|
271
|
+
|
|
272
|
+
# clear CLI buffer only
|
|
273
|
+
if clear_buffer:
|
|
274
|
+
self.buffer.clear()
|
|
275
|
+
|
|
276
|
+
# clear terminal (but NOT watcher buffer)
|
|
277
|
+
utils.clearTerminal()
|
|
278
|
+
|
|
279
|
+
# print header (not captured)
|
|
280
|
+
self.printHeader()
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
# ---------------------------------------------------------
|
|
284
|
+
# WATCHER COMMANDS
|
|
285
|
+
# ---------------------------------------------------------
|
|
286
|
+
def do_watcher(self, arg):
|
|
287
|
+
"""
|
|
288
|
+
watcher restart
|
|
289
|
+
watcher schedule <minutes>
|
|
290
|
+
watcher shutdown
|
|
291
|
+
"""
|
|
292
|
+
parts = arg.split()
|
|
293
|
+
|
|
294
|
+
if len(parts) == 0:
|
|
295
|
+
self.safePrint("Usage: watcher <restart|schedule|shutdown>")
|
|
296
|
+
return
|
|
297
|
+
|
|
298
|
+
sub = parts[0]
|
|
299
|
+
|
|
300
|
+
if sub == "restart":
|
|
301
|
+
self.watcher.restart_and_wait()
|
|
302
|
+
|
|
303
|
+
elif sub == "schedule":
|
|
304
|
+
try:
|
|
305
|
+
args = schedule_parser.parse_args(parts[1:])
|
|
306
|
+
self.watcher.schedule_restart(args.minutes)
|
|
307
|
+
except SystemExit:
|
|
308
|
+
self.safePrint("Usage: watcher schedule <minutes>")
|
|
309
|
+
|
|
310
|
+
elif sub == "shutdown":
|
|
311
|
+
self.watcher.shutdown()
|
|
312
|
+
return True
|
|
313
|
+
|
|
314
|
+
else:
|
|
315
|
+
self.safePrint("Usage: watcher <restart|schedule|shutdown>")
|
|
316
|
+
|
|
317
|
+
# ---------------------------------------------------------
|
|
318
|
+
# BUFFER PRINT
|
|
319
|
+
# ---------------------------------------------------------
|
|
320
|
+
def bprint(self, text):
|
|
321
|
+
if self.buffer.enabled:
|
|
322
|
+
self.buffer.captured.append(str(text))
|
|
323
|
+
self.safePrint(str(text))
|
serverwatcher/watchercli.py
CHANGED
|
@@ -8,6 +8,20 @@ from .livecli import LiveCLI, command
|
|
|
8
8
|
setGlobalMaps(ascii_colors)
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
class BufferingStdout:
|
|
12
|
+
def __init__(self, buffer, real_stdout):
|
|
13
|
+
self.buffer = buffer
|
|
14
|
+
self.real_stdout = real_stdout
|
|
15
|
+
|
|
16
|
+
def write(self, text):
|
|
17
|
+
if self.buffer.enabled and text.strip():
|
|
18
|
+
self.buffer.captured.append(text.rstrip("\n"))
|
|
19
|
+
self.real_stdout.write(text)
|
|
20
|
+
|
|
21
|
+
def flush(self):
|
|
22
|
+
self.real_stdout.flush()
|
|
23
|
+
|
|
24
|
+
|
|
11
25
|
class WatcherCLI(LiveCLI):
|
|
12
26
|
def __init__(self, watcher: ServerWatcher):
|
|
13
27
|
super().__init__()
|
|
@@ -29,6 +43,16 @@ def stats():
|
|
|
29
43
|
|
|
30
44
|
@stats.child('get', requires_arguments=False)
|
|
31
45
|
def stats_get(self, arg=None):
|
|
46
|
+
'''
|
|
47
|
+
args:
|
|
48
|
+
ram [rounding] [--raw]
|
|
49
|
+
cpu [rounding]
|
|
50
|
+
uptime [--raw]
|
|
51
|
+
tps [rounding] [mode]
|
|
52
|
+
players
|
|
53
|
+
version
|
|
54
|
+
platform
|
|
55
|
+
'''
|
|
32
56
|
stats_get.__description__ = 'Retrieve and print all or specific server statistics'
|
|
33
57
|
|
|
34
58
|
self.watcher.server.refresh()
|
|
@@ -117,15 +141,26 @@ def view_watcher(self):
|
|
|
117
141
|
|
|
118
142
|
|
|
119
143
|
@command('clear')
|
|
120
|
-
def clear():
|
|
144
|
+
def clear(self):
|
|
121
145
|
clear.__description__ = 'Clear the CLI terminal'
|
|
122
146
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
147
|
+
# default: buffer:false
|
|
148
|
+
clear_buffer = False
|
|
149
|
+
|
|
150
|
+
# parse key:value pairs from params (buffer:true|false)
|
|
151
|
+
# our DSL already parses params as key:value tokens
|
|
152
|
+
# here we just look at globals injected by dispatch if needed
|
|
153
|
+
# but simpler: rely on a param
|
|
154
|
+
buf = buffer()
|
|
155
|
+
clear_buffer = bool(buf)
|
|
156
|
+
|
|
157
|
+
if clear_buffer:
|
|
158
|
+
self.buffer.clear()
|
|
159
|
+
|
|
126
160
|
utils.clearTerminal()
|
|
161
|
+
self.printHeader()
|
|
127
162
|
|
|
128
163
|
|
|
129
|
-
@clear.
|
|
130
|
-
def
|
|
131
|
-
return
|
|
164
|
+
@clear.param('buffer', type=bool, default=False)
|
|
165
|
+
def buffer(value):
|
|
166
|
+
return value
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
serverwatcher/__init__.py,sha256=Q_afN0XU29n48d6Gf0-LT2lLo3RiTWlHRT6rC_fYbjw,486
|
|
2
2
|
serverwatcher/__main__.py,sha256=5geaZQKgLSP1pxnpQPgFy9zAwyyPyTInlbcFxb9OiEw,3016
|
|
3
|
-
serverwatcher/
|
|
4
|
-
serverwatcher/livecli.py,sha256=GC-OBSL-EkGRFyuHLx8wjgsDh6d7y2OEr_SIQxGX-WU,8815
|
|
3
|
+
serverwatcher/livecli.py,sha256=PvbwEF_zUxhHI2yNsszbRrzTD9mvywFyFtMB4m7VKis,10764
|
|
5
4
|
serverwatcher/validator.py,sha256=aD6GHOz1S5VFC93Mj89WO9vZG1bTjhg7QZbdlvQf4wo,1778
|
|
6
5
|
serverwatcher/watcher.py,sha256=ZiocJpeyhKTGOwYYak36MXNEFpLnpdl9v6BnMPGrBmM,13593
|
|
7
|
-
serverwatcher/watchercli.py,sha256=
|
|
6
|
+
serverwatcher/watchercli.py,sha256=DkqqeY9FTeZb5zorGCU0Y4j7v00Y1jW5BpX0U5FUgaw,4398
|
|
8
7
|
serverwatcher/configclasses/__init__.py,sha256=YS0KhxtAJGdMcaiFstqTRNxIPLnR7s9v3Tkt5h2uKE4,182
|
|
9
8
|
serverwatcher/configclasses/config.py,sha256=1Upor55QFE4UIOqL9ZYa4U5kbda48GG9HWdRXg8mb_Y,2418
|
|
10
9
|
serverwatcher/configclasses/messages.py,sha256=iOkCvwagngW4aeWD5I6hi6cqqDkzEsFJsLHSuqK-P1s,3467
|
|
@@ -12,10 +11,10 @@ serverwatcher/configclasses/watcher.py,sha256=OIAYQZF9hD9e5FbcaYV5N9CAdCZoLgXSqA
|
|
|
12
11
|
serverwatcher/defaultconfigs/config.yaml,sha256=yrJ59Jx0F4TJzE1EdBF5IKPCDqs09XcbKHFfkYGJ1ys,721
|
|
13
12
|
serverwatcher/defaultconfigs/messages.yaml,sha256=jSVEl-RYqPHhe5U5KLl8zfJtUBymvyE74hmzH0RFC_Y,3177
|
|
14
13
|
serverwatcher/defaultconfigs/watcher.yaml,sha256=GlAEKCuzFUSF6wWOqQLFL1CeuPltb1ltfYxFe_FUClw,723
|
|
15
|
-
serverwatcher/tests/test.py,sha256
|
|
16
|
-
serverwatcher/tests/test2.py,sha256=
|
|
17
|
-
serverwatcher-6.
|
|
18
|
-
serverwatcher-6.
|
|
19
|
-
serverwatcher-6.
|
|
20
|
-
serverwatcher-6.
|
|
21
|
-
serverwatcher-6.
|
|
14
|
+
serverwatcher/tests/test.py,sha256=-ZYvAQ1vtAUydiEBK27MgBGW0fHzAuc3Iu63O-Fz4Y0,1636
|
|
15
|
+
serverwatcher/tests/test2.py,sha256=DcvYiidXTe3SSOpiSPQ585YBN4PXrDMs0a3ZW_TbBsg,9564
|
|
16
|
+
serverwatcher-6.dev21.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
17
|
+
serverwatcher-6.dev21.dist-info/METADATA,sha256=E-31hpgb_0FXDB9epQQFPP3ytE7CegKeOGG60TAgwyA,2647
|
|
18
|
+
serverwatcher-6.dev21.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
19
|
+
serverwatcher-6.dev21.dist-info/top_level.txt,sha256=8DJAf8WmmglgtZHkp8aoTriRJ2YPcS4F3DfOIkytASo,14
|
|
20
|
+
serverwatcher-6.dev21.dist-info/RECORD,,
|
serverwatcher/cli.py
DELETED
|
@@ -1,398 +0,0 @@
|
|
|
1
|
-
import asyncio
|
|
2
|
-
import cmd
|
|
3
|
-
import sys
|
|
4
|
-
import termios
|
|
5
|
-
import tty
|
|
6
|
-
from dataclasses import dataclass
|
|
7
|
-
|
|
8
|
-
from hungerlib import utils
|
|
9
|
-
from mapres import res, maps, setGlobalMaps
|
|
10
|
-
from .watcher import ServerWatcher
|
|
11
|
-
|
|
12
|
-
setGlobalMaps(maps.ascii_colors)
|
|
13
|
-
|
|
14
|
-
# parsed arguments
|
|
15
|
-
@dataclass
|
|
16
|
-
class ParsedArgs:
|
|
17
|
-
subcommand: str | None
|
|
18
|
-
positional: list[str]
|
|
19
|
-
flags: dict[str, bool]
|
|
20
|
-
params: dict[str, str]
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def parse_line(raw: str) -> ParsedArgs:
|
|
24
|
-
raw = raw.strip()
|
|
25
|
-
if not raw:
|
|
26
|
-
return ParsedArgs(None, [], {}, {})
|
|
27
|
-
|
|
28
|
-
parts = raw.split()
|
|
29
|
-
sub = parts[0]
|
|
30
|
-
positional: list[str] = []
|
|
31
|
-
flags: dict[str, bool] = {}
|
|
32
|
-
params: dict[str, str] = {}
|
|
33
|
-
|
|
34
|
-
for token in parts[1:]:
|
|
35
|
-
if token.startswith('--'):
|
|
36
|
-
flags[token[2:]] = True
|
|
37
|
-
elif ':' in token:
|
|
38
|
-
key, value = token.split(':', 1)
|
|
39
|
-
params[key.lower()] = value
|
|
40
|
-
else:
|
|
41
|
-
positional.append(token)
|
|
42
|
-
|
|
43
|
-
return ParsedArgs(sub, positional, flags, params)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
# command registry
|
|
47
|
-
COMMANDS = {}
|
|
48
|
-
|
|
49
|
-
class ParamSpec:
|
|
50
|
-
def __init__(self, name, type_, default):
|
|
51
|
-
self.name = name
|
|
52
|
-
self.type = type_
|
|
53
|
-
self.default = default
|
|
54
|
-
|
|
55
|
-
class FlagSpec:
|
|
56
|
-
def __init__(self, name):
|
|
57
|
-
self.name = name
|
|
58
|
-
|
|
59
|
-
class ChildSpec:
|
|
60
|
-
def __init__(self, name, func):
|
|
61
|
-
self.name = name
|
|
62
|
-
self.func = func
|
|
63
|
-
self.params = {}
|
|
64
|
-
self.flags = {}
|
|
65
|
-
|
|
66
|
-
def param(self, name, type=str, default=None):
|
|
67
|
-
def deco(func):
|
|
68
|
-
pyname = name.replace('-', '_')
|
|
69
|
-
self.params[pyname] = ParamSpec(name, type, default)
|
|
70
|
-
return func
|
|
71
|
-
return deco
|
|
72
|
-
|
|
73
|
-
def flag(self, name):
|
|
74
|
-
def deco(func):
|
|
75
|
-
pyname = name.replace('-', '_')
|
|
76
|
-
self.flags[pyname] = FlagSpec(name)
|
|
77
|
-
return func
|
|
78
|
-
return deco
|
|
79
|
-
|
|
80
|
-
class CommandSpec:
|
|
81
|
-
def __init__(self, name, func):
|
|
82
|
-
self.name = name
|
|
83
|
-
self.func = func
|
|
84
|
-
self.children = {}
|
|
85
|
-
|
|
86
|
-
def child(self, name):
|
|
87
|
-
def deco(func):
|
|
88
|
-
child = ChildSpec(name, func)
|
|
89
|
-
self.children[name] = child
|
|
90
|
-
# expose child as attribute so stats.get, clear.buffer, watcher.schedule work
|
|
91
|
-
setattr(self, name, child)
|
|
92
|
-
return child
|
|
93
|
-
return deco
|
|
94
|
-
|
|
95
|
-
class CommandDSL:
|
|
96
|
-
def __call__(self, name):
|
|
97
|
-
def deco(func):
|
|
98
|
-
spec = CommandSpec(name, func)
|
|
99
|
-
COMMANDS[name] = spec
|
|
100
|
-
return spec
|
|
101
|
-
return deco
|
|
102
|
-
|
|
103
|
-
command = CommandDSL()
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
# base CLI helpers
|
|
107
|
-
class CLIBase:
|
|
108
|
-
def safePrint(self, msg: object = '', end: str = '\n') -> None:
|
|
109
|
-
print(str(msg), end=end)
|
|
110
|
-
|
|
111
|
-
def bprint(self, msg: object) -> None:
|
|
112
|
-
self.safePrint(msg)
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
# dispatcher
|
|
116
|
-
def dispatch(cli: CLIBase, line: str):
|
|
117
|
-
parsed = parse_line(line)
|
|
118
|
-
if not parsed.subcommand:
|
|
119
|
-
return
|
|
120
|
-
|
|
121
|
-
cmd = COMMANDS.get(parsed.subcommand)
|
|
122
|
-
if cmd is None:
|
|
123
|
-
return cli.safePrint(f'Unknown command {parsed.subcommand}')
|
|
124
|
-
|
|
125
|
-
cmd.func()
|
|
126
|
-
|
|
127
|
-
if not parsed.positional:
|
|
128
|
-
return cli.safePrint('Missing subcommand')
|
|
129
|
-
|
|
130
|
-
child_name = parsed.positional[0]
|
|
131
|
-
child = cmd.children.get(child_name)
|
|
132
|
-
if child is None:
|
|
133
|
-
return cli.safePrint(f'Unknown subcommand {child_name}')
|
|
134
|
-
|
|
135
|
-
stat = parsed.positional[1] if len(parsed.positional) > 1 else None
|
|
136
|
-
|
|
137
|
-
wrappers: dict[str, callable] = {}
|
|
138
|
-
|
|
139
|
-
for pname, pspec in child.params.items():
|
|
140
|
-
if pname in parsed.params:
|
|
141
|
-
raw = parsed.params[pname]
|
|
142
|
-
|
|
143
|
-
def make_param(raw=raw, pspec=pspec):
|
|
144
|
-
try:
|
|
145
|
-
return pspec.type(raw)
|
|
146
|
-
except Exception:
|
|
147
|
-
return pspec.default
|
|
148
|
-
|
|
149
|
-
wrappers[pname] = make_param
|
|
150
|
-
else:
|
|
151
|
-
def make_default(pspec=pspec):
|
|
152
|
-
return pspec.default
|
|
153
|
-
|
|
154
|
-
wrappers[pname] = make_default
|
|
155
|
-
|
|
156
|
-
for fname, _fspec in child.flags.items():
|
|
157
|
-
present = fname in parsed.flags
|
|
158
|
-
|
|
159
|
-
def make_flag(present=present):
|
|
160
|
-
return present
|
|
161
|
-
|
|
162
|
-
wrappers[fname] = make_flag
|
|
163
|
-
|
|
164
|
-
return child.func(cli, stat, **wrappers)
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
# buffering stdout
|
|
168
|
-
class BufferingStdout:
|
|
169
|
-
def __init__(self, buffer, real_stdout):
|
|
170
|
-
self.buffer = buffer
|
|
171
|
-
self.real_stdout = real_stdout
|
|
172
|
-
|
|
173
|
-
def write(self, text: str) -> None:
|
|
174
|
-
if self.buffer.enabled and text.strip():
|
|
175
|
-
self.buffer.captured.append(text.rstrip('\n'))
|
|
176
|
-
self.real_stdout.write(text)
|
|
177
|
-
|
|
178
|
-
def flush(self) -> None:
|
|
179
|
-
self.real_stdout.flush()
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
# main CLI
|
|
183
|
-
class WatcherCLI(cmd.Cmd, CLIBase):
|
|
184
|
-
prompt = ''
|
|
185
|
-
|
|
186
|
-
def __init__(self, watcher: ServerWatcher):
|
|
187
|
-
self.watcher = watcher
|
|
188
|
-
self.buffer = utils.Buffer(enabled=True)
|
|
189
|
-
self.real_stdout = sys.stdout
|
|
190
|
-
self.cli_stdout = BufferingStdout(self.buffer, self.real_stdout)
|
|
191
|
-
super().__init__(stdout=self.cli_stdout)
|
|
192
|
-
self.outputMode = 'both'
|
|
193
|
-
self.use_rawinput = False
|
|
194
|
-
|
|
195
|
-
def read_line_raw(self) -> str:
|
|
196
|
-
fd = sys.stdin.fileno()
|
|
197
|
-
old = termios.tcgetattr(fd)
|
|
198
|
-
try:
|
|
199
|
-
tty.setraw(fd)
|
|
200
|
-
chars: list[str] = []
|
|
201
|
-
while True:
|
|
202
|
-
ch = sys.stdin.read(1)
|
|
203
|
-
if ch in ('\r', '\n'):
|
|
204
|
-
break
|
|
205
|
-
chars.append(ch)
|
|
206
|
-
return ''.join(chars)
|
|
207
|
-
finally:
|
|
208
|
-
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
|
209
|
-
|
|
210
|
-
async def run(self):
|
|
211
|
-
while True:
|
|
212
|
-
try:
|
|
213
|
-
line = await asyncio.to_thread(self.read_line_raw)
|
|
214
|
-
line = line.strip()
|
|
215
|
-
if not line:
|
|
216
|
-
continue
|
|
217
|
-
if self.buffer.enabled and not line.startswith('view'):
|
|
218
|
-
self.buffer.captured.append(line)
|
|
219
|
-
stop = await asyncio.to_thread(self.onecmd, line)
|
|
220
|
-
if stop:
|
|
221
|
-
break
|
|
222
|
-
except KeyboardInterrupt:
|
|
223
|
-
if self.watcher.config.handle_keyboard_interrupt:
|
|
224
|
-
self.watcher.shutdown()
|
|
225
|
-
return
|
|
226
|
-
else:
|
|
227
|
-
raise
|
|
228
|
-
|
|
229
|
-
def onecmd(self, line: str):
|
|
230
|
-
return dispatch(self, line)
|
|
231
|
-
|
|
232
|
-
def printHeader(self) -> None:
|
|
233
|
-
self.safePrint(res('<yellow>-----------------------------------'))
|
|
234
|
-
self.safePrint(res('<yellow>-------- <aqua>ServerWatcher CLI <yellow>--------'))
|
|
235
|
-
self.safePrint(res('<yellow>-----------------------------------'))
|
|
236
|
-
self.safePrint('\n')
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
# stats command
|
|
240
|
-
@command('stats')
|
|
241
|
-
def stats():
|
|
242
|
-
pass
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
@stats.child('get')
|
|
246
|
-
def stats_get(self, stat: str, **kwargs):
|
|
247
|
-
rounding = kwargs['rounding']
|
|
248
|
-
mode = kwargs['mode']
|
|
249
|
-
raw = kwargs['raw']
|
|
250
|
-
formatted = kwargs['formatted']
|
|
251
|
-
no_formatted = kwargs['no_formatted']
|
|
252
|
-
|
|
253
|
-
self.watcher.server.refresh()
|
|
254
|
-
|
|
255
|
-
gb = not raw()
|
|
256
|
-
if formatted():
|
|
257
|
-
fmt = True
|
|
258
|
-
elif no_formatted():
|
|
259
|
-
fmt = False
|
|
260
|
-
else:
|
|
261
|
-
fmt = True
|
|
262
|
-
|
|
263
|
-
unit = ''
|
|
264
|
-
match stat:
|
|
265
|
-
case 'ram':
|
|
266
|
-
value = self.watcher.server.getRAM(rounding=rounding(), gb=gb)
|
|
267
|
-
name = 'RAM'
|
|
268
|
-
unit = ' GB' if gb else ' MB'
|
|
269
|
-
case 'cpu':
|
|
270
|
-
value = self.watcher.server.getCPU(rounding=rounding())
|
|
271
|
-
name = 'CPU'
|
|
272
|
-
unit = '%'
|
|
273
|
-
case 'uptime':
|
|
274
|
-
value = self.watcher.server.getUptime(formatted=fmt)
|
|
275
|
-
name = 'Uptime'
|
|
276
|
-
unit = '' if fmt else 'ms'
|
|
277
|
-
case 'tps':
|
|
278
|
-
value = self.watcher.server.getTPS(rounding=rounding(), mode=mode())
|
|
279
|
-
name = 'TPS'
|
|
280
|
-
case 'players':
|
|
281
|
-
value = self.watcher.server.getPlayers()
|
|
282
|
-
name = 'Players'
|
|
283
|
-
case 'version':
|
|
284
|
-
value = self.watcher.server.version
|
|
285
|
-
name = 'Minecraft version'
|
|
286
|
-
case 'platform':
|
|
287
|
-
value = self.watcher.server.platform
|
|
288
|
-
name = 'Server platform'
|
|
289
|
-
case _:
|
|
290
|
-
return self.safePrint('Unknown stat')
|
|
291
|
-
|
|
292
|
-
self.bprint(f'{name}: {value}{unit}')
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
@stats.get.param('rounding', type=int, default=2)
|
|
296
|
-
def rounding(value: int):
|
|
297
|
-
return value
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
@stats.get.param('mode', type=str, default='current')
|
|
301
|
-
def mode(value: str):
|
|
302
|
-
return value
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
@stats.get.flag('raw')
|
|
306
|
-
def raw():
|
|
307
|
-
return False
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
@stats.get.flag('formatted')
|
|
311
|
-
def formatted():
|
|
312
|
-
return True
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
@stats.get.flag('no-formatted')
|
|
316
|
-
def no_formatted():
|
|
317
|
-
return False
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
# view command
|
|
321
|
-
@command('view')
|
|
322
|
-
def view():
|
|
323
|
-
pass
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
@view.child('cli')
|
|
327
|
-
def view_cli(self, _arg: str, **kwargs):
|
|
328
|
-
self.watcher.router.disableOriginOutput()
|
|
329
|
-
self.outputMode = 'cli'
|
|
330
|
-
self.buffer.enabled = True
|
|
331
|
-
utils.clearTerminal()
|
|
332
|
-
self.printHeader()
|
|
333
|
-
for msg in self.buffer.captured:
|
|
334
|
-
self.safePrint(msg)
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
@view.child('watcher')
|
|
338
|
-
def view_watcher(self, _arg: str, **kwargs):
|
|
339
|
-
self.watcher.router.enableOriginOutput()
|
|
340
|
-
self.outputMode = 'both'
|
|
341
|
-
utils.clearTerminal()
|
|
342
|
-
self.printHeader()
|
|
343
|
-
for msg in self.watcher.router.buffer.captured:
|
|
344
|
-
self.safePrint(msg)
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
# clear command
|
|
348
|
-
@command('clear')
|
|
349
|
-
def clear():
|
|
350
|
-
pass
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
@clear.child('buffer')
|
|
354
|
-
def clear_buffer(self, _arg: str, **kwargs):
|
|
355
|
-
buffer = kwargs['buffer']
|
|
356
|
-
|
|
357
|
-
if buffer():
|
|
358
|
-
self.buffer.clear()
|
|
359
|
-
|
|
360
|
-
utils.clearTerminal()
|
|
361
|
-
self.printHeader()
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
@clear.buffer.param('buffer', type=str, default='true')
|
|
365
|
-
def buffer(value: str):
|
|
366
|
-
return value.lower() == 'true'
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
# watcher command
|
|
370
|
-
@command('watcher')
|
|
371
|
-
def watcher():
|
|
372
|
-
pass
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
@watcher.child('restart')
|
|
376
|
-
def watcher_restart(self, _arg: str, **kwargs):
|
|
377
|
-
self.watcher.restart_and_wait()
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
@watcher.child('schedule')
|
|
381
|
-
def watcher_schedule(self, _arg: str, **kwargs):
|
|
382
|
-
minutes = kwargs['minutes']
|
|
383
|
-
|
|
384
|
-
val = minutes()
|
|
385
|
-
if val is None:
|
|
386
|
-
return self.safePrint('Usage: watcher schedule minutes')
|
|
387
|
-
self.watcher.schedule_restart(val)
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
@watcher.child('shutdown')
|
|
391
|
-
def watcher_shutdown(self, _arg: str, **kwargs):
|
|
392
|
-
self.watcher.shutdown()
|
|
393
|
-
return True
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
@watcher.schedule.param('minutes', type=int, default=None)
|
|
397
|
-
def minutes(value: int):
|
|
398
|
-
return value
|
|
File without changes
|
|
File without changes
|
|
File without changes
|