replbase 0.0.31__tar.gz → 0.0.33__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {replbase-0.0.31 → replbase-0.0.33}/PKG-INFO +1 -1
- {replbase-0.0.31 → replbase-0.0.33}/pyproject.toml +1 -1
- {replbase-0.0.31 → replbase-0.0.33}/replbase/repl_base.py +58 -0
- {replbase-0.0.31 → replbase-0.0.33}/LICENSE +0 -0
- {replbase-0.0.31 → replbase-0.0.33}/README.md +0 -0
- {replbase-0.0.31 → replbase-0.0.33}/replbase/__init__.py +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
|
|
|
4
4
|
|
|
5
5
|
[tool.poetry]
|
|
6
6
|
name = "replbase"
|
|
7
|
-
version = "0.0.
|
|
7
|
+
version = "0.0.33"
|
|
8
8
|
description = "\"Combination of other REPL tools into a reusable class that generates a REPL\""
|
|
9
9
|
authors = [ "Joseph Bochinski <stirgejr@gmail.com>",]
|
|
10
10
|
license = "MIT"
|
|
@@ -16,6 +16,7 @@ from __future__ import annotations
|
|
|
16
16
|
|
|
17
17
|
import argparse
|
|
18
18
|
import grp
|
|
19
|
+
import inspect
|
|
19
20
|
import math
|
|
20
21
|
import os
|
|
21
22
|
import pwd
|
|
@@ -418,6 +419,59 @@ class ReplBase:
|
|
|
418
419
|
self.commands[cmd_name] = new_cmd
|
|
419
420
|
return new_cmd
|
|
420
421
|
|
|
422
|
+
def gen_command(self, cmd_name: str) -> ReplCommand:
|
|
423
|
+
if not hasattr(self, cmd_name):
|
|
424
|
+
return
|
|
425
|
+
|
|
426
|
+
cmd: Callable = getattr(self, cmd_name)
|
|
427
|
+
anno = cmd.__annotations__
|
|
428
|
+
sig = inspect.signature(cmd)
|
|
429
|
+
parms = sig.parameters
|
|
430
|
+
help_text = cmd.__doc__
|
|
431
|
+
repl_cmd = self.add_command(
|
|
432
|
+
cmd.__name__,
|
|
433
|
+
cmd,
|
|
434
|
+
help_txt=help_text,
|
|
435
|
+
use_parser=True,
|
|
436
|
+
description=help_text,
|
|
437
|
+
)
|
|
438
|
+
|
|
439
|
+
if not parms:
|
|
440
|
+
return repl_cmd
|
|
441
|
+
|
|
442
|
+
for arg, parm in parms.items():
|
|
443
|
+
arg_type = anno.get(arg, str)
|
|
444
|
+
def_val = parm.default
|
|
445
|
+
|
|
446
|
+
optional = def_val != parm.empty
|
|
447
|
+
flag_name = arg.replace("_", "-")
|
|
448
|
+
|
|
449
|
+
if optional:
|
|
450
|
+
flag_name = "--" + flag_name
|
|
451
|
+
|
|
452
|
+
cmd_init = {}
|
|
453
|
+
|
|
454
|
+
if arg_type == bool:
|
|
455
|
+
cmd_init["action"] = (
|
|
456
|
+
"store_false" if def_val is True else "store_true"
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
if arg_type != str:
|
|
460
|
+
cmd_init["type"] = arg_type
|
|
461
|
+
|
|
462
|
+
if isinstance(arg_type, type) and isinstance(def_val, arg_type):
|
|
463
|
+
cmd_init["default"] = def_val
|
|
464
|
+
|
|
465
|
+
if isinstance(arg_type, list):
|
|
466
|
+
cmd_init["nargs"] = "+"
|
|
467
|
+
|
|
468
|
+
if cmd_init:
|
|
469
|
+
repl_cmd.parser.add_argument(flag_name, **cmd_init)
|
|
470
|
+
else:
|
|
471
|
+
repl_cmd.parser.add_argument(flag_name)
|
|
472
|
+
|
|
473
|
+
return repl_cmd
|
|
474
|
+
|
|
421
475
|
def setup_cmds(self, *cmd_names: list[str]) -> None:
|
|
422
476
|
"""Automatically configure commands based on the provided names
|
|
423
477
|
The ReplCommand objects are populated based on function meta
|
|
@@ -437,6 +491,10 @@ class ReplBase:
|
|
|
437
491
|
name = func.__name__
|
|
438
492
|
self.add_command(name, func, help_txt=help_text)
|
|
439
493
|
|
|
494
|
+
def setup_cmds_2(self, *cmd_names: list[str]):
|
|
495
|
+
for name in cmd_names:
|
|
496
|
+
self.gen_command(name)
|
|
497
|
+
|
|
440
498
|
def warn(self, msg: str) -> None:
|
|
441
499
|
"""Print a message to the REPL preformatted as a warning"""
|
|
442
500
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|