wizlib 3.0.1__py3-none-any.whl → 3.1.1__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.
Potentially problematic release.
This version of wizlib might be problematic. Click here for more details.
- wizlib/app.py +49 -35
- wizlib/command.py +0 -1
- wizlib/parser.py +2 -0
- wizlib/ui/shell_ui.py +0 -1
- {wizlib-3.0.1.dist-info → wizlib-3.1.1.dist-info}/METADATA +1 -1
- {wizlib-3.0.1.dist-info → wizlib-3.1.1.dist-info}/RECORD +7 -8
- wizlib/rlinput.py +0 -71
- {wizlib-3.0.1.dist-info → wizlib-3.1.1.dist-info}/WHEEL +0 -0
wizlib/app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from argparse import ArgumentError
|
|
1
2
|
import sys
|
|
2
3
|
from dataclasses import dataclass
|
|
3
4
|
import os
|
|
@@ -6,7 +7,7 @@ from pathlib import Path
|
|
|
6
7
|
from wizlib.class_family import ClassFamily
|
|
7
8
|
from wizlib.command import WizHelpCommand
|
|
8
9
|
from wizlib.super_wrapper import SuperWrapper
|
|
9
|
-
from wizlib.parser import WizParser
|
|
10
|
+
from wizlib.parser import WizArgumentError, WizParser
|
|
10
11
|
from wizlib.ui import UI
|
|
11
12
|
|
|
12
13
|
|
|
@@ -22,9 +23,15 @@ class WizApp:
|
|
|
22
23
|
"""Root of all WizLib-based CLI applications. Subclass it. Can be
|
|
23
24
|
instantiated and then run multiple commands."""
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
# Name of this app, used in argparse (override)
|
|
26
27
|
name = ''
|
|
27
28
|
|
|
29
|
+
# Base command class, typically defined in command/__init__.py (override)
|
|
30
|
+
base = None
|
|
31
|
+
|
|
32
|
+
# List of Handler classes used by this app (override)
|
|
33
|
+
handlers = []
|
|
34
|
+
|
|
28
35
|
# Set some default types so linting works
|
|
29
36
|
ui: UI
|
|
30
37
|
|
|
@@ -37,13 +44,15 @@ class WizApp:
|
|
|
37
44
|
def start(cls, *args, debug=False):
|
|
38
45
|
"""Call this from a Python entrypoint"""
|
|
39
46
|
try:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
cls.initialize()
|
|
48
|
+
try:
|
|
49
|
+
ns = cls.parser.parse_args(args)
|
|
50
|
+
if (ns.command is None) and (not hasattr(ns, 'help')):
|
|
51
|
+
ns = cls.dparser.parse_args(args)
|
|
52
|
+
except ArgumentError as e:
|
|
53
|
+
ns = cls.dparser.parse_args(args)
|
|
54
|
+
app = cls(**vars(ns))
|
|
55
|
+
app.run(**vars(ns))
|
|
47
56
|
except AppCancellation as cancellation:
|
|
48
57
|
if str(cancellation):
|
|
49
58
|
print(str(cancellation), file=sys.stderr)
|
|
@@ -57,38 +66,43 @@ class WizApp:
|
|
|
57
66
|
sys.exit(1)
|
|
58
67
|
|
|
59
68
|
@classmethod
|
|
60
|
-
def initialize(cls
|
|
61
|
-
"""
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
for
|
|
65
|
-
val = vals[handler.name] if (
|
|
66
|
-
handler.name in vals) else handler.default
|
|
67
|
-
handlers[handler.name] = handler.setup(val)
|
|
68
|
-
return cls(**handlers)
|
|
69
|
-
|
|
70
|
-
def __init__(self, **handlers):
|
|
71
|
-
for name, handler in handlers.items():
|
|
72
|
-
handler.app = self
|
|
73
|
-
setattr(self, name, handler)
|
|
74
|
-
self.parser = WizParser()
|
|
75
|
-
subparsers = self.parser.add_subparsers(dest='command')
|
|
76
|
-
for command in self.base_command.family_members('name'):
|
|
69
|
+
def initialize(cls):
|
|
70
|
+
"""Set up the parser for the app class"""
|
|
71
|
+
cls.parser = WizParser(prog=cls.name)
|
|
72
|
+
subparsers = cls.parser.add_subparsers(dest='command')
|
|
73
|
+
for command in cls.base.family_members('name'):
|
|
77
74
|
key = command.get_member_attr('key')
|
|
78
75
|
aliases = [key] if key else []
|
|
79
76
|
subparser = subparsers.add_parser(command.name, aliases=aliases)
|
|
77
|
+
if command.name == cls.base.default:
|
|
78
|
+
cls.dparser = subparser
|
|
80
79
|
command.add_args(subparser)
|
|
80
|
+
for handler in cls.handlers:
|
|
81
|
+
handler.add_args(cls.parser)
|
|
82
|
+
handler.add_args(cls.dparser)
|
|
83
|
+
|
|
84
|
+
def __init__(self, **vals):
|
|
85
|
+
"""Create the app. Only interested in the handlers from the parsed
|
|
86
|
+
values passed in"""
|
|
87
|
+
for hcls in self.handlers:
|
|
88
|
+
val = vals[hcls.name] if (hcls.name in vals) else hcls.default
|
|
89
|
+
handler = hcls.setup(val)
|
|
90
|
+
handler.app = self
|
|
91
|
+
setattr(self, hcls.name, handler)
|
|
81
92
|
|
|
82
|
-
def run(self,
|
|
83
|
-
|
|
93
|
+
def run(self, **vals):
|
|
94
|
+
"""Perform a command. May be called more than once. Only interested in
|
|
95
|
+
the command itself and its specific arguments from the values passed
|
|
96
|
+
in."""
|
|
84
97
|
if 'help' in vals:
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
98
|
+
ccls = WizHelpCommand
|
|
99
|
+
else:
|
|
100
|
+
c = 'command'
|
|
101
|
+
cname = (vals.pop(c) if c in vals else None) or self.base.default
|
|
102
|
+
ccls = self.base.family_member('name', cname)
|
|
103
|
+
if not ccls:
|
|
104
|
+
raise Exception(f"Unknown command {cname}")
|
|
105
|
+
command = ccls(self, **vals)
|
|
92
106
|
result = command.execute()
|
|
93
107
|
if result:
|
|
94
108
|
print(result, end='')
|
wizlib/command.py
CHANGED
wizlib/parser.py
CHANGED
wizlib/ui/shell_ui.py
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
wizlib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
wizlib/app.py,sha256=
|
|
2
|
+
wizlib/app.py,sha256=PT8iiXGes38PNKtuybTKGFdzTBgc8FuGpEytfrICNps,3795
|
|
3
3
|
wizlib/class_family.py,sha256=tORSVAaPeWTQMcz2DX-MQClj1GQR3vCmkALPXxHa_pk,4506
|
|
4
|
-
wizlib/command.py,sha256=
|
|
4
|
+
wizlib/command.py,sha256=NO1558EYuXxfkpSmX6ljjzae8n8g4w6yytZKTJtigvo,1708
|
|
5
5
|
wizlib/config_handler.py,sha256=hoDavSMiGM_7PAHI8XIwC8nxPWOZDk302ryyjluoLGg,2588
|
|
6
6
|
wizlib/error.py,sha256=ypwdMOYhtgKWd48ccfOX8idmCXmm-Skwx3gkPwqJB3c,46
|
|
7
7
|
wizlib/handler.py,sha256=Oz80aPhDyeY9tdppZ1dvtN-19JU5ydEDVW6jtppVoD4,446
|
|
8
|
-
wizlib/parser.py,sha256=
|
|
9
|
-
wizlib/rlinput.py,sha256=l00Pa3rxNeY6LJgz8Aws_rTKoEchw33fuL8yqHF9_-o,1754
|
|
8
|
+
wizlib/parser.py,sha256=yLHV0fENeApFomCRWa3I6sB1x4lk1ag4vKejWVsic64,1550
|
|
10
9
|
wizlib/stream_handler.py,sha256=i1EgcBrDiYhFK-CI8At7JtUtMCNunJmSkJLSlili-as,663
|
|
11
10
|
wizlib/super_wrapper.py,sha256=F834ytHqA7zegTD1ezk_uxlF9PLygh84wReuiqcI7BI,272
|
|
12
11
|
wizlib/ui/__init__.py,sha256=ve_p_g4aBujh4jIJMgKkJ6cE5PT0aeY5AgRlneDswGg,4241
|
|
13
12
|
wizlib/ui/shell/__init__.py,sha256=sPrYe4bG_Xf7Nwssx_dqXVk9jeyYBFUjh4oLdlSOeRY,943
|
|
14
13
|
wizlib/ui/shell/line_editor.py,sha256=frpsqU5NggcvEz3XeB8YyqgmlExlyx6K1CKPzvARtPk,7419
|
|
15
|
-
wizlib/ui/shell_ui.py,sha256=
|
|
14
|
+
wizlib/ui/shell_ui.py,sha256=f2GErMJg09GPZDS4uK0q8Qq8WEssQFP1dqQyg4ycznI,2029
|
|
16
15
|
wizlib/ui_handler.py,sha256=JoZadtw9DKAtGvHKP3_BJF2NaYqmcQYNdsY4PeRnOjg,634
|
|
17
16
|
wizlib/util.py,sha256=x1SyL3iXot0ET2r8Jjb2ySTd_J-3Uu7J1b_CHvFgzew,156
|
|
18
|
-
wizlib-3.
|
|
19
|
-
wizlib-3.
|
|
20
|
-
wizlib-3.
|
|
17
|
+
wizlib-3.1.1.dist-info/METADATA,sha256=JoxwvHVMdPueP_F-p_5H3qDuj0Jbxg01oYp1tZHMDI8,1780
|
|
18
|
+
wizlib-3.1.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
19
|
+
wizlib-3.1.1.dist-info/RECORD,,
|
wizlib/rlinput.py
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
try: # pragma: nocover
|
|
2
|
-
import gnureadline as readline
|
|
3
|
-
except ImportError: # pragma: nocover
|
|
4
|
-
import readline
|
|
5
|
-
import sys
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
# Use tab for completion
|
|
9
|
-
readline.parse_and_bind('tab: complete')
|
|
10
|
-
|
|
11
|
-
# Only a space separates words
|
|
12
|
-
readline.set_completer_delims(' ')
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
# Readline defaults to complete with local filenames. Definitely not what we
|
|
16
|
-
# want.
|
|
17
|
-
|
|
18
|
-
def null_complete(text, start): # pragma: nocover
|
|
19
|
-
return None
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
readline.set_completer(null_complete)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def rlinput(prompt: str = "", default: str = "",
|
|
26
|
-
options: list = None): # pragma: nocover
|
|
27
|
-
"""
|
|
28
|
-
Get input with preset default and/or tab completion of options
|
|
29
|
-
|
|
30
|
-
Parameters:
|
|
31
|
-
|
|
32
|
-
prompt:str - string to output before requesting input, same as in the
|
|
33
|
-
input() function
|
|
34
|
-
|
|
35
|
-
default:str - value with which to prepopulate the response, can be cleared
|
|
36
|
-
with ctrl-a, ctrl-k
|
|
37
|
-
|
|
38
|
-
options:list - list of choices for tab completion
|
|
39
|
-
"""
|
|
40
|
-
|
|
41
|
-
# Clean out the options
|
|
42
|
-
options = [o.strip() + " " for o in options] if options else []
|
|
43
|
-
|
|
44
|
-
# Create the completer using the options
|
|
45
|
-
def complete(text, state):
|
|
46
|
-
results = [x for x in options if x.startswith(text)] + [None]
|
|
47
|
-
return results[state]
|
|
48
|
-
readline.set_completer(complete)
|
|
49
|
-
|
|
50
|
-
# Insert the default when we launch
|
|
51
|
-
def start():
|
|
52
|
-
readline.insert_text(default)
|
|
53
|
-
readline.set_startup_hook(start)
|
|
54
|
-
|
|
55
|
-
# Actually perform the input
|
|
56
|
-
try:
|
|
57
|
-
value = input(prompt)
|
|
58
|
-
finally:
|
|
59
|
-
readline.set_startup_hook()
|
|
60
|
-
readline.set_completer(null_complete)
|
|
61
|
-
return value.strip()
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def tryit(): # pragma: nocover
|
|
65
|
-
"""Quick and dirty tester function"""
|
|
66
|
-
return rlinput(prompt='>',
|
|
67
|
-
options=['a-b', 'a-c', 'b-a'])
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if __name__ == '__main__':
|
|
71
|
-
tryit()
|
|
File without changes
|