serverwatcher 6.dev25__py3-none-any.whl → 6.dev27__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 +285 -297
- serverwatcher/tests/test.py +72 -47
- serverwatcher/watchercli.py +74 -88
- {serverwatcher-6.dev25.dist-info → serverwatcher-6.dev27.dist-info}/METADATA +1 -1
- {serverwatcher-6.dev25.dist-info → serverwatcher-6.dev27.dist-info}/RECORD +8 -8
- {serverwatcher-6.dev25.dist-info → serverwatcher-6.dev27.dist-info}/WHEEL +0 -0
- {serverwatcher-6.dev25.dist-info → serverwatcher-6.dev27.dist-info}/licenses/LICENSE +0 -0
- {serverwatcher-6.dev25.dist-info → serverwatcher-6.dev27.dist-info}/top_level.txt +0 -0
serverwatcher/livecli.py
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
# livecli.py
|
|
2
|
+
|
|
1
3
|
from dataclasses import dataclass
|
|
2
4
|
import cmd
|
|
3
5
|
import sys
|
|
4
6
|
import termios
|
|
5
7
|
import tty
|
|
6
8
|
import asyncio
|
|
9
|
+
import inspect
|
|
7
10
|
|
|
8
|
-
COMMANDS
|
|
9
|
-
|
|
11
|
+
COMMANDS = {}
|
|
10
12
|
|
|
11
13
|
@dataclass
|
|
12
14
|
class ParamSpec:
|
|
@@ -14,361 +16,349 @@ class ParamSpec:
|
|
|
14
16
|
type_: type
|
|
15
17
|
default: object
|
|
16
18
|
func: callable
|
|
17
|
-
|
|
19
|
+
desc: str | None
|
|
18
20
|
|
|
19
21
|
@dataclass
|
|
20
22
|
class FlagSpec:
|
|
21
23
|
name: str
|
|
22
24
|
func: callable
|
|
25
|
+
desc: str | None
|
|
23
26
|
|
|
27
|
+
@dataclass
|
|
28
|
+
class ArgMeta:
|
|
29
|
+
name: str
|
|
30
|
+
params: list[str]
|
|
31
|
+
flags: list[str]
|
|
32
|
+
required_params: set[str]
|
|
33
|
+
required_flags: set[str]
|
|
24
34
|
|
|
25
35
|
class ChildSpec:
|
|
26
|
-
def __init__(
|
|
27
|
-
self
|
|
28
|
-
name,
|
|
29
|
-
func,
|
|
30
|
-
requires_children=False,
|
|
31
|
-
requires_arguments=False,
|
|
32
|
-
requires_params=False,
|
|
33
|
-
requires_flags=False,
|
|
34
|
-
has_arguments=False,
|
|
35
|
-
):
|
|
36
|
-
if has_arguments and requires_children:
|
|
37
|
-
raise ValueError(f'Child {name} cannot have arguments and children')
|
|
38
|
-
|
|
36
|
+
def __init__(self, parent, name, func):
|
|
37
|
+
self.parent = parent
|
|
39
38
|
self.name = name
|
|
40
39
|
self.func = func
|
|
41
|
-
self.
|
|
42
|
-
self.
|
|
43
|
-
self.
|
|
44
|
-
self.
|
|
45
|
-
self.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
40
|
+
self.params = {}
|
|
41
|
+
self.flags = {}
|
|
42
|
+
self.args_meta = {}
|
|
43
|
+
self.desc = self._doc(func)
|
|
44
|
+
self._parse_args_meta()
|
|
45
|
+
self._infer_arg_requirement()
|
|
46
|
+
|
|
47
|
+
def _doc(self, f):
|
|
48
|
+
d = f.__doc__
|
|
49
|
+
return d.strip().splitlines()[0].strip() if d else None
|
|
50
|
+
|
|
51
|
+
def _parse_args_meta(self):
|
|
52
|
+
raw = getattr(self.func, '__args__', None)
|
|
53
|
+
if not raw:
|
|
54
|
+
return
|
|
55
|
+
for line in raw.splitlines():
|
|
56
|
+
s = line.strip()
|
|
57
|
+
if not s:
|
|
58
|
+
continue
|
|
59
|
+
if ':' in s:
|
|
60
|
+
arg, rest = s.split(':', 1)
|
|
61
|
+
arg = arg.strip()
|
|
62
|
+
tokens = [t.strip() for t in rest.split(',') if t.strip()]
|
|
63
|
+
else:
|
|
64
|
+
arg = s
|
|
65
|
+
tokens = []
|
|
66
|
+
params = []
|
|
67
|
+
flags = []
|
|
68
|
+
req_p = set()
|
|
69
|
+
req_f = set()
|
|
70
|
+
for t in tokens:
|
|
71
|
+
required = True
|
|
72
|
+
if t.startswith('[') and t.endswith(']'):
|
|
73
|
+
required = False
|
|
74
|
+
t = t[1:-1].strip()
|
|
75
|
+
if t.startswith('--'):
|
|
76
|
+
f = t[2:]
|
|
77
|
+
flags.append(f)
|
|
78
|
+
if required:
|
|
79
|
+
req_f.add(f)
|
|
80
|
+
else:
|
|
81
|
+
params.append(t)
|
|
82
|
+
if required:
|
|
83
|
+
req_p.add(t)
|
|
84
|
+
self.args_meta[arg] = ArgMeta(arg, params, flags, req_p, req_f)
|
|
85
|
+
|
|
86
|
+
def _infer_arg_requirement(self):
|
|
87
|
+
sig = inspect.signature(self.func)
|
|
88
|
+
ps = list(sig.parameters.values())
|
|
89
|
+
if len(ps) == 1:
|
|
90
|
+
self.requires_arg = False
|
|
91
|
+
self.optional_arg = False
|
|
92
|
+
elif len(ps) == 2:
|
|
93
|
+
p = ps[1]
|
|
94
|
+
if p.default is inspect._empty:
|
|
95
|
+
self.requires_arg = True
|
|
96
|
+
self.optional_arg = False
|
|
97
|
+
else:
|
|
98
|
+
self.requires_arg = False
|
|
99
|
+
self.optional_arg = True
|
|
100
|
+
else:
|
|
101
|
+
self.requires_arg = False
|
|
102
|
+
self.optional_arg = False
|
|
50
103
|
|
|
51
|
-
self.description = getattr(func, '__description__', None)
|
|
52
|
-
|
|
53
104
|
def param(self, name, type=str, default=None):
|
|
54
|
-
def deco(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
105
|
+
def deco(f):
|
|
106
|
+
py = name.replace('-', '_')
|
|
107
|
+
d = f.__doc__.strip().splitlines()[0].strip() if f.__doc__ else None
|
|
108
|
+
self.params[py] = ParamSpec(name, type, default, f, d)
|
|
109
|
+
return f
|
|
58
110
|
return deco
|
|
59
111
|
|
|
60
112
|
def flag(self, name):
|
|
61
|
-
def deco(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
113
|
+
def deco(f):
|
|
114
|
+
py = name.replace('-', '_')
|
|
115
|
+
d = f.__doc__.strip().splitlines()[0].strip() if f.__doc__ else None
|
|
116
|
+
self.flags[py] = FlagSpec(name, f, d)
|
|
117
|
+
return f
|
|
65
118
|
return deco
|
|
66
119
|
|
|
67
|
-
|
|
68
|
-
|
|
69
120
|
class CommandSpec:
|
|
70
|
-
def __init__(
|
|
71
|
-
self,
|
|
72
|
-
name,
|
|
73
|
-
func,
|
|
74
|
-
requires_children=False,
|
|
75
|
-
requires_arguments=False,
|
|
76
|
-
requires_params=False,
|
|
77
|
-
requires_flags=False,
|
|
78
|
-
has_arguments=False,
|
|
79
|
-
):
|
|
80
|
-
if has_arguments and requires_children:
|
|
81
|
-
raise ValueError(f'Command {name} cannot have arguments and children')
|
|
82
|
-
|
|
121
|
+
def __init__(self, name, func):
|
|
83
122
|
self.name = name
|
|
84
123
|
self.func = func
|
|
85
|
-
self.
|
|
86
|
-
self.
|
|
87
|
-
self.
|
|
88
|
-
self.
|
|
89
|
-
self.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
124
|
+
self.children = {}
|
|
125
|
+
self.params = {}
|
|
126
|
+
self.flags = {}
|
|
127
|
+
self.desc = self._doc(func)
|
|
128
|
+
self.is_namespace = self._infer_namespace()
|
|
129
|
+
|
|
130
|
+
def _doc(self, f):
|
|
131
|
+
d = f.__doc__
|
|
132
|
+
return d.strip().splitlines()[0].strip() if d else None
|
|
133
|
+
|
|
134
|
+
def _infer_namespace(self):
|
|
135
|
+
src = inspect.getsource(self.func)
|
|
136
|
+
body = src.split('\n', 1)[1]
|
|
137
|
+
stripped = [l.strip() for l in body.splitlines() if l.strip()]
|
|
138
|
+
return stripped == [] or stripped == ['pass']
|
|
96
139
|
|
|
97
140
|
def param(self, name, type=str, default=None):
|
|
98
|
-
def deco(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
141
|
+
def deco(f):
|
|
142
|
+
py = name.replace('-', '_')
|
|
143
|
+
d = f.__doc__.strip().splitlines()[0].strip() if f.__doc__ else None
|
|
144
|
+
self.params[py] = ParamSpec(name, type, default, f, d)
|
|
145
|
+
return f
|
|
102
146
|
return deco
|
|
103
147
|
|
|
104
148
|
def flag(self, name):
|
|
105
|
-
def deco(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
149
|
+
def deco(f):
|
|
150
|
+
py = name.replace('-', '_')
|
|
151
|
+
d = f.__doc__.strip().splitlines()[0].strip() if f.__doc__ else None
|
|
152
|
+
self.flags[py] = FlagSpec(name, f, d)
|
|
153
|
+
return f
|
|
109
154
|
return deco
|
|
110
155
|
|
|
111
|
-
def child(self, name
|
|
112
|
-
def deco(
|
|
113
|
-
|
|
114
|
-
self.children[name] =
|
|
115
|
-
setattr(self, name,
|
|
116
|
-
return
|
|
156
|
+
def child(self, name):
|
|
157
|
+
def deco(f):
|
|
158
|
+
c = ChildSpec(self, name, f)
|
|
159
|
+
self.children[name] = c
|
|
160
|
+
setattr(self, name, c)
|
|
161
|
+
return c
|
|
117
162
|
return deco
|
|
118
163
|
|
|
119
|
-
|
|
120
164
|
class CommandDSL:
|
|
121
|
-
def __call__(self, name
|
|
122
|
-
def deco(
|
|
123
|
-
spec = CommandSpec(name,
|
|
165
|
+
def __call__(self, name):
|
|
166
|
+
def deco(f):
|
|
167
|
+
spec = CommandSpec(name, f)
|
|
124
168
|
COMMANDS[name] = spec
|
|
125
169
|
return spec
|
|
126
170
|
return deco
|
|
127
171
|
|
|
128
|
-
|
|
129
172
|
command = CommandDSL()
|
|
130
173
|
|
|
131
|
-
|
|
132
174
|
@dataclass
|
|
133
175
|
class ParsedArgs:
|
|
134
|
-
|
|
135
|
-
|
|
176
|
+
sub: str | None
|
|
177
|
+
pos: list[str]
|
|
136
178
|
flags: dict[str, bool]
|
|
137
179
|
params: dict[str, str]
|
|
138
180
|
|
|
139
|
-
|
|
140
|
-
def parse_line(raw: str) -> ParsedArgs:
|
|
181
|
+
def parse_line(raw):
|
|
141
182
|
raw = raw.strip()
|
|
142
183
|
if not raw:
|
|
143
184
|
return ParsedArgs(None, [], {}, {})
|
|
144
|
-
|
|
145
185
|
parts = raw.split()
|
|
146
186
|
sub = parts[0]
|
|
147
|
-
|
|
187
|
+
pos = []
|
|
148
188
|
flags = {}
|
|
149
189
|
params = {}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
params[key.lower()] = value
|
|
190
|
+
for t in parts[1:]:
|
|
191
|
+
if t.startswith('--'):
|
|
192
|
+
flags[t[2:]] = True
|
|
193
|
+
elif ':' in t:
|
|
194
|
+
k, v = t.split(':', 1)
|
|
195
|
+
params[k.lower()] = v
|
|
157
196
|
else:
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
lines.append(
|
|
206
|
-
lines.append(
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
lines.append('
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
lines.append('
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
return '
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
def dispatch(cli, line: str):
|
|
264
|
-
parsed = parse_line(line)
|
|
265
|
-
if not parsed.subcommand:
|
|
266
|
-
return
|
|
267
|
-
|
|
268
|
-
cmd_spec = COMMANDS.get(parsed.subcommand)
|
|
269
|
-
if cmd_spec is None:
|
|
270
|
-
return cli.safePrint(f'Unknown command {parsed.subcommand}')
|
|
271
|
-
|
|
272
|
-
if 'help' in parsed.flags and not parsed.positional:
|
|
273
|
-
return cli.safePrint(generate_help(cmd_spec))
|
|
274
|
-
|
|
275
|
-
cmd_spec.func(cli)
|
|
197
|
+
pos.append(t)
|
|
198
|
+
return ParsedArgs(sub, pos, flags, params)
|
|
199
|
+
|
|
200
|
+
def help_command(cmd):
|
|
201
|
+
lines = []
|
|
202
|
+
d = cmd.desc or 'No description'
|
|
203
|
+
lines.append(f'{cmd.name}: {d}')
|
|
204
|
+
if cmd.children:
|
|
205
|
+
if cmd.is_namespace and len(cmd.children) == 1:
|
|
206
|
+
lines.append(f'Usage: {cmd.name} <child>')
|
|
207
|
+
else:
|
|
208
|
+
lines.append(f'Usage: {cmd.name} [child]')
|
|
209
|
+
lines.append('')
|
|
210
|
+
lines.append(' Children:')
|
|
211
|
+
for n, c in cmd.children.items():
|
|
212
|
+
cd = c.desc or 'No description'
|
|
213
|
+
lines.append(f' {n}: {cd}')
|
|
214
|
+
else:
|
|
215
|
+
lines.append(f'Usage: {cmd.name}')
|
|
216
|
+
return '\n'.join(lines)
|
|
217
|
+
|
|
218
|
+
def help_child(child):
|
|
219
|
+
lines = []
|
|
220
|
+
d = child.desc or 'No description'
|
|
221
|
+
p = child.parent.name
|
|
222
|
+
lines.append(f'{child.name}: {d}')
|
|
223
|
+
u = f'Usage: {p} {child.name}'
|
|
224
|
+
if child.requires_arg:
|
|
225
|
+
u += ' <arg>'
|
|
226
|
+
elif child.optional_arg:
|
|
227
|
+
u += ' [arg]'
|
|
228
|
+
if child.params:
|
|
229
|
+
u += ' [params]'
|
|
230
|
+
if child.flags:
|
|
231
|
+
u += ' [--flags]'
|
|
232
|
+
lines.append(u)
|
|
233
|
+
if child.args_meta:
|
|
234
|
+
lines.append('')
|
|
235
|
+
lines.append(' Args:')
|
|
236
|
+
for a, m in child.args_meta.items():
|
|
237
|
+
parts = [a]
|
|
238
|
+
for pn in m.params:
|
|
239
|
+
parts.append(f'[{pn}]')
|
|
240
|
+
for fn in m.flags:
|
|
241
|
+
parts.append(f'[--{fn}]')
|
|
242
|
+
lines.append(' ' + ' '.join(parts))
|
|
243
|
+
if child.params:
|
|
244
|
+
lines.append('')
|
|
245
|
+
lines.append(' Params:')
|
|
246
|
+
for n, ps in child.params.items():
|
|
247
|
+
t = ps.type_.__name__
|
|
248
|
+
d = ps.desc or 'No description'
|
|
249
|
+
lines.append(f' {n} (type={t}, default={ps.default}): {d}')
|
|
250
|
+
if child.flags:
|
|
251
|
+
lines.append('')
|
|
252
|
+
lines.append(' Flags:')
|
|
253
|
+
for n, fs in child.flags.items():
|
|
254
|
+
d = fs.desc or 'No description'
|
|
255
|
+
lines.append(f' --{n}: {d}')
|
|
256
|
+
return '\n'.join(lines)
|
|
257
|
+
|
|
258
|
+
def help_arg(child, meta):
|
|
259
|
+
lines = []
|
|
260
|
+
p = child.parent.name
|
|
261
|
+
a = meta.name
|
|
262
|
+
lines.append(f'{a}: Retrieve and print the {a} of the server')
|
|
263
|
+
u = f'Usage: {p} {child.name} {a}'
|
|
264
|
+
req = []
|
|
265
|
+
opt = []
|
|
266
|
+
for pn in meta.params:
|
|
267
|
+
ps = child.params.get(pn)
|
|
268
|
+
t = ps.type_.__name__ if ps else 'str'
|
|
269
|
+
if pn in meta.required_params:
|
|
270
|
+
req.append(f'<{pn}:{t}>')
|
|
271
|
+
else:
|
|
272
|
+
opt.append(f'[{pn}:{t}]')
|
|
273
|
+
if req:
|
|
274
|
+
u += ' ' + ' '.join(req)
|
|
275
|
+
if opt:
|
|
276
|
+
u += ' ' + ' '.join(opt)
|
|
277
|
+
if meta.flags:
|
|
278
|
+
u += ' [--flags]'
|
|
279
|
+
lines.append(u)
|
|
280
|
+
if meta.params:
|
|
281
|
+
lines.append('')
|
|
282
|
+
lines.append(' Params:')
|
|
283
|
+
for pn in meta.params:
|
|
284
|
+
ps = child.params.get(pn)
|
|
285
|
+
if not ps:
|
|
286
|
+
continue
|
|
287
|
+
t = ps.type_.__name__
|
|
288
|
+
d = ps.desc or 'No description'
|
|
289
|
+
lines.append(f' {pn} (type={t}, default={ps.default}): {d}')
|
|
290
|
+
if meta.flags:
|
|
291
|
+
lines.append('')
|
|
292
|
+
lines.append(' Flags:')
|
|
293
|
+
for fn in meta.flags:
|
|
294
|
+
fs = child.flags.get(fn)
|
|
295
|
+
if not fs:
|
|
296
|
+
continue
|
|
297
|
+
d = fs.desc or 'No description'
|
|
298
|
+
lines.append(f' --{fn}: {d}')
|
|
299
|
+
return '\n'.join(lines)
|
|
276
300
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
301
|
+
def dispatch(cli, line):
|
|
302
|
+
p = parse_line(line)
|
|
303
|
+
if not p.sub:
|
|
280
304
|
return
|
|
281
|
-
|
|
282
|
-
if not
|
|
283
|
-
return cli.safePrint('
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
if
|
|
305
|
+
cmd = COMMANDS.get(p.sub)
|
|
306
|
+
if not cmd:
|
|
307
|
+
return cli.safePrint(f'Unknown command {p.sub}')
|
|
308
|
+
if 'help' in p.flags and not p.pos:
|
|
309
|
+
return cli.safePrint(help_command(cmd))
|
|
310
|
+
if cmd.children:
|
|
311
|
+
if not p.pos:
|
|
312
|
+
return cli.safePrint('Missing subcommand')
|
|
313
|
+
else:
|
|
314
|
+
return cmd.func(cli)
|
|
315
|
+
cname = p.pos[0]
|
|
316
|
+
child = cmd.children.get(cname)
|
|
317
|
+
if not child:
|
|
318
|
+
return cli.safePrint(f'Unknown subcommand {cname}')
|
|
319
|
+
if 'help' in p.flags and len(p.pos) == 1:
|
|
320
|
+
return cli.safePrint(help_child(child))
|
|
321
|
+
if 'help' in p.flags and len(p.pos) >= 2:
|
|
322
|
+
an = p.pos[1]
|
|
323
|
+
meta = child.args_meta.get(an)
|
|
324
|
+
if not meta:
|
|
325
|
+
return cli.safePrint(f'Unknown argument {an}')
|
|
326
|
+
return cli.safePrint(help_arg(child, meta))
|
|
327
|
+
arg = p.pos[1] if len(p.pos) > 1 else None
|
|
328
|
+
if child.requires_arg and arg is None:
|
|
296
329
|
return cli.safePrint(f'Subcommand {child.name} requires an argument')
|
|
297
|
-
|
|
298
330
|
g = child.func.__globals__
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
if pname in parsed.params:
|
|
302
|
-
raw_value = parsed.params[pname]
|
|
331
|
+
for n, ps in child.params.items():
|
|
332
|
+
if n in p.params:
|
|
303
333
|
try:
|
|
304
|
-
|
|
305
|
-
except
|
|
306
|
-
|
|
334
|
+
v = ps.type_(p.params[n])
|
|
335
|
+
except:
|
|
336
|
+
v = ps.default
|
|
307
337
|
else:
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
g[fname] = make_flag_func()
|
|
326
|
-
|
|
327
|
-
for pname, pspec in child.params.items():
|
|
328
|
-
if pname in parsed.params:
|
|
329
|
-
raw_value = parsed.params[pname]
|
|
330
|
-
try:
|
|
331
|
-
converted = pspec.type_(raw_value)
|
|
332
|
-
except Exception:
|
|
333
|
-
converted = pspec.default
|
|
334
|
-
else:
|
|
335
|
-
converted = pspec.default
|
|
336
|
-
|
|
337
|
-
def make_param_func(func=pspec.func, value=converted):
|
|
338
|
-
def wrapper():
|
|
339
|
-
return func(value)
|
|
340
|
-
return wrapper
|
|
341
|
-
|
|
342
|
-
g[pname] = make_param_func()
|
|
343
|
-
|
|
344
|
-
for fname, fspec in child.flags.items():
|
|
345
|
-
present = fname in parsed.flags
|
|
346
|
-
|
|
347
|
-
def make_flag_func(func=fspec.func, present=present):
|
|
348
|
-
def wrapper():
|
|
349
|
-
return present
|
|
350
|
-
return wrapper
|
|
351
|
-
|
|
352
|
-
g[fname] = make_flag_func()
|
|
353
|
-
|
|
354
|
-
func = child.func
|
|
355
|
-
argc = func.__code__.co_argcount
|
|
356
|
-
|
|
357
|
-
if argc >= 2:
|
|
358
|
-
return func(cli, arg)
|
|
359
|
-
else:
|
|
360
|
-
return func(cli)
|
|
361
|
-
|
|
338
|
+
v = ps.default
|
|
339
|
+
def wrap(f=ps.func, val=v):
|
|
340
|
+
def w():
|
|
341
|
+
return f(val)
|
|
342
|
+
return w
|
|
343
|
+
g[n] = wrap()
|
|
344
|
+
for n, fs in child.flags.items():
|
|
345
|
+
present = n in p.flags
|
|
346
|
+
def wrap(f=fs.func, pr=present):
|
|
347
|
+
def w():
|
|
348
|
+
return pr
|
|
349
|
+
return w
|
|
350
|
+
g[n] = wrap()
|
|
351
|
+
f = child.func
|
|
352
|
+
if f.__code__.co_argcount >= 2:
|
|
353
|
+
return f(cli, arg)
|
|
354
|
+
return f(cli)
|
|
362
355
|
|
|
363
356
|
class LiveCLI(cmd.Cmd):
|
|
364
357
|
prompt = ''
|
|
365
|
-
|
|
366
358
|
def safePrint(self, msg='', end='\n'):
|
|
367
359
|
print(str(msg), end=end)
|
|
368
|
-
|
|
369
360
|
def bprint(self, msg):
|
|
370
361
|
self.safePrint(msg)
|
|
371
|
-
|
|
372
362
|
def read_line_raw(self):
|
|
373
363
|
fd = sys.stdin.fileno()
|
|
374
364
|
old = termios.tcgetattr(fd)
|
|
@@ -383,7 +373,6 @@ class LiveCLI(cmd.Cmd):
|
|
|
383
373
|
return ''.join(chars)
|
|
384
374
|
finally:
|
|
385
375
|
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
|
386
|
-
|
|
387
376
|
async def run(self):
|
|
388
377
|
while True:
|
|
389
378
|
line = await asyncio.to_thread(self.read_line_raw)
|
|
@@ -393,6 +382,5 @@ class LiveCLI(cmd.Cmd):
|
|
|
393
382
|
stop = await asyncio.to_thread(self.onecmd, line)
|
|
394
383
|
if stop:
|
|
395
384
|
break
|
|
396
|
-
|
|
397
385
|
def onecmd(self, line):
|
|
398
386
|
return dispatch(self, line)
|
serverwatcher/tests/test.py
CHANGED
|
@@ -1,52 +1,77 @@
|
|
|
1
|
-
@
|
|
2
|
-
def
|
|
1
|
+
@command('stats') # removed requires_children=True, the DSL should interpret no logic as requiring children
|
|
2
|
+
def stats(self):
|
|
3
3
|
'''
|
|
4
|
-
|
|
5
|
-
ram [rounding] [--raw]
|
|
6
|
-
cpu [rounding]
|
|
7
|
-
uptime [--raw]
|
|
8
|
-
tps [rounding] [mode]
|
|
9
|
-
players
|
|
10
|
-
version
|
|
11
|
-
platform
|
|
4
|
+
Retrieve and print server statistics
|
|
12
5
|
'''
|
|
13
|
-
stats_get.__description__ = 'Retrieve and print all or specific server statistics'
|
|
14
6
|
|
|
7
|
+
|
|
8
|
+
@stats.child('get') # removed requires_arguments=False, the DSL should interpret logic for both as supporting both args and noargs. that and having arg=None. If it didn't support arguments it would just be self right? And if it required arguments it would have arg, not arg=None right?
|
|
9
|
+
def stats_get(self, arg=None):
|
|
10
|
+
'''
|
|
11
|
+
Retrieve and print all or specific server statistics
|
|
12
|
+
'''
|
|
13
|
+
# the below will render as not having indents correct? or not?
|
|
14
|
+
__args__ = '''
|
|
15
|
+
ram: [rounding] [--raw]
|
|
16
|
+
cpu: [rounding]
|
|
17
|
+
uptime: [rounding] [mode]
|
|
18
|
+
players
|
|
19
|
+
version
|
|
20
|
+
platform
|
|
21
|
+
'''
|
|
15
22
|
self.watcher.server.refresh()
|
|
16
23
|
|
|
17
|
-
if arg is
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
24
|
+
if arg is None:
|
|
25
|
+
return # will add support later for showing all stats, but this acts as a placeholder for the DLS to know it supports optional arguments.
|
|
26
|
+
|
|
27
|
+
gb = not raw()
|
|
28
|
+
fmt = not raw()
|
|
29
|
+
|
|
30
|
+
unit = ''
|
|
31
|
+
match arg:
|
|
32
|
+
case 'ram':
|
|
33
|
+
value = self.watcher.server.getRAM(rounding=rounding(), gb=gb)
|
|
34
|
+
name = 'RAM'
|
|
35
|
+
unit = ' GB' if gb else ' MB'
|
|
36
|
+
case 'cpu':
|
|
37
|
+
value = self.watcher.server.getCPU(rounding=rounding())
|
|
38
|
+
name = 'CPU'
|
|
39
|
+
unit = '%'
|
|
40
|
+
case 'uptime':
|
|
41
|
+
value = self.watcher.server.getUptime(formatted=fmt)
|
|
42
|
+
name = 'Uptime'
|
|
43
|
+
unit = '' if fmt else 'ms'
|
|
44
|
+
case 'tps':
|
|
45
|
+
value = self.watcher.server.getTPS(rounding=rounding(), mode=mode())
|
|
46
|
+
name = 'TPS'
|
|
47
|
+
case 'players':
|
|
48
|
+
value = self.watcher.server.getPlayers()
|
|
49
|
+
name = 'Players'
|
|
50
|
+
case 'version':
|
|
51
|
+
value = self.watcher.server.version
|
|
52
|
+
name = 'Minecraft version'
|
|
53
|
+
case 'platform':
|
|
54
|
+
value = self.watcher.server.platform
|
|
55
|
+
name = 'Server platform'
|
|
56
|
+
case _:
|
|
57
|
+
return self.safePrint('Unknown stat')
|
|
58
|
+
|
|
59
|
+
self.bprint(f'{name}: {value}{unit}')
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@stats.get.param('rounding', type=int, default=2)
|
|
63
|
+
def rounding(value):
|
|
64
|
+
'''The decimal place to round to''' # yes, descriptions need to be supported for flags and params too.
|
|
65
|
+
return value
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@stats.get.param('mode', type=str, default='current')
|
|
69
|
+
def mode(value):
|
|
70
|
+
'''The mode for TPS. Accepted: current, 1m, 5m, tick_time'''
|
|
71
|
+
return value
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@stats.get.flag('raw')
|
|
75
|
+
def raw():
|
|
76
|
+
'''Return the raw value instead of the calculated or formatted string'''
|
|
77
|
+
return True
|
serverwatcher/watchercli.py
CHANGED
|
@@ -5,20 +5,16 @@ from .livecli import LiveCLI, command
|
|
|
5
5
|
|
|
6
6
|
setGlobalMaps(ascii_colors)
|
|
7
7
|
|
|
8
|
-
|
|
9
8
|
class BufferingStdout:
|
|
10
|
-
def __init__(self, buffer,
|
|
9
|
+
def __init__(self, buffer, real):
|
|
11
10
|
self.buffer = buffer
|
|
12
|
-
self.
|
|
13
|
-
|
|
11
|
+
self.real = real
|
|
14
12
|
def write(self, text):
|
|
15
13
|
if self.buffer.enabled and text.strip():
|
|
16
14
|
self.buffer.captured.append(text.rstrip('\n'))
|
|
17
|
-
self.
|
|
18
|
-
|
|
15
|
+
self.real.write(text)
|
|
19
16
|
def flush(self):
|
|
20
|
-
self.
|
|
21
|
-
|
|
17
|
+
self.real.flush()
|
|
22
18
|
|
|
23
19
|
class WatcherCLI(LiveCLI):
|
|
24
20
|
def __init__(self, watcher: ServerWatcher):
|
|
@@ -26,137 +22,127 @@ class WatcherCLI(LiveCLI):
|
|
|
26
22
|
self.watcher = watcher
|
|
27
23
|
self.buffer = utils.Buffer(enabled=True)
|
|
28
24
|
self.outputMode = 'both'
|
|
29
|
-
|
|
30
25
|
def printHeader(self):
|
|
31
26
|
self.safePrint(res('<yellow>-----------------------------------'))
|
|
32
27
|
self.safePrint(res('<yellow>-------- <aqua>ServerWatcher CLI <yellow>--------'))
|
|
33
28
|
self.safePrint(res('<yellow>-----------------------------------'))
|
|
34
29
|
self.safePrint('')
|
|
35
30
|
|
|
36
|
-
|
|
37
|
-
@command('stats', requires_children=True)
|
|
31
|
+
@command('stats')
|
|
38
32
|
def stats(self):
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
'''
|
|
34
|
+
Retrieve and print server statistics
|
|
35
|
+
'''
|
|
36
|
+
pass
|
|
41
37
|
|
|
42
|
-
@stats.child('get'
|
|
38
|
+
@stats.child('get')
|
|
43
39
|
def stats_get(self, arg=None):
|
|
44
40
|
'''
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
41
|
+
Retrieve and print all or specific server statistics
|
|
42
|
+
'''
|
|
43
|
+
__args__ = '''
|
|
44
|
+
ram: rounding, --raw
|
|
45
|
+
cpu: rounding
|
|
46
|
+
uptime: rounding, mode
|
|
47
|
+
players
|
|
48
|
+
version
|
|
49
|
+
platform
|
|
53
50
|
'''
|
|
54
|
-
__description__ = 'Retrieve and print all or specific server statistics'
|
|
55
51
|
self.watcher.server.refresh()
|
|
56
|
-
|
|
57
|
-
if arg is not None:
|
|
58
|
-
gb = raw()
|
|
59
|
-
fmt = not raw()
|
|
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
|
-
else:
|
|
52
|
+
if arg is None:
|
|
92
53
|
return
|
|
93
|
-
|
|
54
|
+
gb = not raw()
|
|
55
|
+
fmt = not raw()
|
|
56
|
+
unit = ''
|
|
57
|
+
match arg:
|
|
58
|
+
case 'ram':
|
|
59
|
+
value = self.watcher.server.getRAM(rounding=rounding(), gb=gb)
|
|
60
|
+
name = 'RAM'
|
|
61
|
+
unit = ' GB' if gb else ' MB'
|
|
62
|
+
case 'cpu':
|
|
63
|
+
value = self.watcher.server.getCPU(rounding=rounding())
|
|
64
|
+
name = 'CPU'
|
|
65
|
+
unit = '%'
|
|
66
|
+
case 'uptime':
|
|
67
|
+
value = self.watcher.server.getUptime(formatted=fmt)
|
|
68
|
+
name = 'Uptime'
|
|
69
|
+
unit = '' if fmt else 'ms'
|
|
70
|
+
case 'tps':
|
|
71
|
+
value = self.watcher.server.getTPS(rounding=rounding(), mode=mode())
|
|
72
|
+
name = 'TPS'
|
|
73
|
+
case 'players':
|
|
74
|
+
value = self.watcher.server.getPlayers()
|
|
75
|
+
name = 'Players'
|
|
76
|
+
case 'version':
|
|
77
|
+
value = self.watcher.server.version
|
|
78
|
+
name = 'Minecraft version'
|
|
79
|
+
case 'platform':
|
|
80
|
+
value = self.watcher.server.platform
|
|
81
|
+
name = 'Server platform'
|
|
82
|
+
case _:
|
|
83
|
+
return self.safePrint('Unknown stat')
|
|
84
|
+
self.bprint(f'{name}: {value}{unit}')
|
|
94
85
|
|
|
95
86
|
@stats.get.param('rounding', type=int, default=2)
|
|
96
87
|
def rounding(value):
|
|
88
|
+
'''The decimal place to round to'''
|
|
97
89
|
return value
|
|
98
90
|
|
|
99
|
-
|
|
100
91
|
@stats.get.param('mode', type=str, default='current')
|
|
101
92
|
def mode(value):
|
|
93
|
+
'''The mode for TPS. Accepted: current, 1m, 5m, tick_time'''
|
|
102
94
|
return value
|
|
103
95
|
|
|
104
|
-
|
|
105
96
|
@stats.get.flag('raw')
|
|
106
97
|
def raw():
|
|
98
|
+
'''Return the raw value instead of the formatted string'''
|
|
107
99
|
return True
|
|
108
100
|
|
|
109
|
-
|
|
110
|
-
@command('view', requires_children=True)
|
|
101
|
+
@command('view')
|
|
111
102
|
def view(self):
|
|
112
|
-
|
|
113
|
-
|
|
103
|
+
'''
|
|
104
|
+
Switch terminal view
|
|
105
|
+
'''
|
|
106
|
+
pass
|
|
114
107
|
|
|
115
108
|
@view.child('cli')
|
|
116
109
|
def view_cli(self):
|
|
117
|
-
|
|
118
|
-
|
|
110
|
+
'''
|
|
111
|
+
Switch to CLI view
|
|
112
|
+
'''
|
|
119
113
|
self.watcher.router.disableOriginOutput()
|
|
120
114
|
self.outputMode = 'cli'
|
|
121
115
|
self.buffer.enabled = True
|
|
122
|
-
|
|
123
116
|
utils.clearTerminal()
|
|
124
117
|
self.safePrint('', end='')
|
|
125
|
-
|
|
126
118
|
self.printHeader()
|
|
127
|
-
|
|
128
119
|
for msg in self.buffer.captured:
|
|
129
120
|
self.safePrint(msg)
|
|
130
121
|
|
|
131
|
-
|
|
132
122
|
@view.child('watcher')
|
|
133
123
|
def view_watcher(self):
|
|
134
|
-
|
|
135
|
-
|
|
124
|
+
'''
|
|
125
|
+
Switch to Watcher view
|
|
126
|
+
'''
|
|
136
127
|
self.watcher.router.enableOriginOutput()
|
|
137
128
|
self.outputMode = 'both'
|
|
138
|
-
|
|
139
129
|
utils.clearTerminal()
|
|
140
130
|
self.safePrint('', end='')
|
|
141
|
-
|
|
142
131
|
for msg in self.watcher.router.buffer.captured:
|
|
143
132
|
self.safePrint(msg)
|
|
144
133
|
|
|
145
|
-
|
|
146
134
|
@command('clear')
|
|
147
135
|
def clear(self):
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if clear_buffer:
|
|
136
|
+
'''
|
|
137
|
+
Clear the CLI terminal
|
|
138
|
+
'''
|
|
139
|
+
no_buf = no_buffer()
|
|
140
|
+
if not no_buf:
|
|
154
141
|
self.buffer.clear()
|
|
155
|
-
|
|
156
142
|
utils.clearTerminal()
|
|
157
143
|
self.printHeader()
|
|
158
144
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return
|
|
145
|
+
@clear.flag('no-buffer')
|
|
146
|
+
def no_buffer():
|
|
147
|
+
'''Do not clear buffer'''
|
|
148
|
+
return True
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
serverwatcher/__init__.py,sha256=Q_afN0XU29n48d6Gf0-LT2lLo3RiTWlHRT6rC_fYbjw,486
|
|
2
2
|
serverwatcher/__main__.py,sha256=5geaZQKgLSP1pxnpQPgFy9zAwyyPyTInlbcFxb9OiEw,3016
|
|
3
|
-
serverwatcher/livecli.py,sha256=
|
|
3
|
+
serverwatcher/livecli.py,sha256=Mjq3vPFRkvngZD_17MAkgz2NNJCw4IUa0JJIJse091o,11233
|
|
4
4
|
serverwatcher/validator.py,sha256=aD6GHOz1S5VFC93Mj89WO9vZG1bTjhg7QZbdlvQf4wo,1778
|
|
5
5
|
serverwatcher/watcher.py,sha256=ZiocJpeyhKTGOwYYak36MXNEFpLnpdl9v6BnMPGrBmM,13593
|
|
6
|
-
serverwatcher/watchercli.py,sha256=
|
|
6
|
+
serverwatcher/watchercli.py,sha256=81zXQVkT7T6VdTEfHGsQle5nY1OXplP-d_Iu2TLgcao,3933
|
|
7
7
|
serverwatcher/configclasses/__init__.py,sha256=YS0KhxtAJGdMcaiFstqTRNxIPLnR7s9v3Tkt5h2uKE4,182
|
|
8
8
|
serverwatcher/configclasses/config.py,sha256=1Upor55QFE4UIOqL9ZYa4U5kbda48GG9HWdRXg8mb_Y,2418
|
|
9
9
|
serverwatcher/configclasses/messages.py,sha256=iOkCvwagngW4aeWD5I6hi6cqqDkzEsFJsLHSuqK-P1s,3467
|
|
@@ -11,10 +11,10 @@ serverwatcher/configclasses/watcher.py,sha256=OIAYQZF9hD9e5FbcaYV5N9CAdCZoLgXSqA
|
|
|
11
11
|
serverwatcher/defaultconfigs/config.yaml,sha256=yrJ59Jx0F4TJzE1EdBF5IKPCDqs09XcbKHFfkYGJ1ys,721
|
|
12
12
|
serverwatcher/defaultconfigs/messages.yaml,sha256=jSVEl-RYqPHhe5U5KLl8zfJtUBymvyE74hmzH0RFC_Y,3177
|
|
13
13
|
serverwatcher/defaultconfigs/watcher.yaml,sha256=GlAEKCuzFUSF6wWOqQLFL1CeuPltb1ltfYxFe_FUClw,723
|
|
14
|
-
serverwatcher/tests/test.py,sha256
|
|
14
|
+
serverwatcher/tests/test.py,sha256=CqGBdfktVIyXz6wMoa6TBbEPlPvp_lA_VSzBuxwxrfs,2551
|
|
15
15
|
serverwatcher/tests/test2.py,sha256=DcvYiidXTe3SSOpiSPQ585YBN4PXrDMs0a3ZW_TbBsg,9564
|
|
16
|
-
serverwatcher-6.
|
|
17
|
-
serverwatcher-6.
|
|
18
|
-
serverwatcher-6.
|
|
19
|
-
serverwatcher-6.
|
|
20
|
-
serverwatcher-6.
|
|
16
|
+
serverwatcher-6.dev27.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
17
|
+
serverwatcher-6.dev27.dist-info/METADATA,sha256=S4ma1JjgBDsc3MtmbFkrxTSKa9D7scNJKsdf3VCpq2g,2647
|
|
18
|
+
serverwatcher-6.dev27.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
19
|
+
serverwatcher-6.dev27.dist-info/top_level.txt,sha256=8DJAf8WmmglgtZHkp8aoTriRJ2YPcS4F3DfOIkytASo,14
|
|
20
|
+
serverwatcher-6.dev27.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|