0.7.1 style fixes

Fixes potential bugs with printing things. Just really cleans up the style printing.
This commit is contained in:
2025-10-20 09:38:31 -07:00
parent 81ea11e37d
commit 226dc5a6ee
3 changed files with 30 additions and 29 deletions

View File

@ -1 +1 @@
__version__ = "0.7.0"
__version__ = "0.7.1"

View File

@ -31,8 +31,9 @@ from .fabtools import HostSet
from .tools import (load_bare_server_list, load_config,
load_sshconfig_server_list)
oprint = print
from .style import print, STYLES
_print = print
from .style import print_style as print
from .style import STYLES
class MultiballCommandException(BaseException):
pass
@ -117,19 +118,19 @@ class Multiball:
self.ssh_config = Path(sc).expanduser()
newhosts = load_sshconfig_server_list(
Path(sc).expanduser())
print(f"<message>Loaded {len(newhosts)} host(s) from {sc}</message>")
print(f"Loaded {len(newhosts)} host(s) from {sc}")
self.allhosts.update(newhosts)
if bareconfigs := config.get('hostlists'):
for sc in bareconfigs.split(','):
newhosts = load_bare_server_list(Path(sc).expanduser())
print(f"<message>Loaded {len(newhosts)} host(s) from {sc}</message>")
print(f"Loaded {len(newhosts)} host(s) from {sc}")
self.allhosts.update(newhosts)
else:
self.ssh_config = Path("~/.ssh/ssh_config").expanduser()
print(
f"<error>Warning: no [config] section in {configFile}. Using fallbacks.</error>")
f"Warning: no [config] section in {configFile}. Using fallbacks.", style="error")
self.allhosts = load_sshconfig_server_list(self.ssh_config)
# setup state and environment
@ -165,17 +166,17 @@ class Multiball:
"""
if args.lower().startswith("off"):
self.safety = False
print("<unsafe>** Safety OFF. Now unsafe.</unsafe>")
print("** Safety OFF. Now unsafe!", style="unsafe")
else:
self.safety = True
print("<safe>** Safety ON.</safe>")
print("** Safety ON.", style="safe")
def command_echo(self, command, args):
"""
Echo string
fixme put string interpolation here so that state variables can be printed
"""
print(f"<message>{args}</message>")
print(f"{args}")
def command_environment(self, command, args):
"""
@ -211,7 +212,7 @@ class Multiball:
if result and (result[0] in 'Yys'):
return True
else:
print("<message>(no)</message>")
print("(no)")
raise MBCommandAbort
def command_arguments(self, command, args):
@ -223,9 +224,9 @@ class Multiball:
"""
if not self._is_scripting or not self._script_frame_arguments:
if not self._is_scripting:
print("<alert>does nothing outside of script</alert>")
print("does nothing outside of script", style="alert")
else:
print("<error>arguments required</error>")
print("arguments required", style="error")
raise MBCommandAbort
return
@ -236,9 +237,9 @@ class Multiball:
"""
if not self._is_scripting or self.safety:
if not self._is_scripting:
print("<alert>does nothing outside of script</alert>")
print("does nothing outside of script", style="alert")
else:
print("<error>cannot run in safety mode</error>")
print("cannot run in safety mode", style="error")
raise MBCommandAbort
return
@ -251,7 +252,7 @@ class Multiball:
# FIXME: We need to make aliases store multiple entries, and save should take the whole alias not just
# the last command.
if (not self.last_run):
print("<alert>Nothing to log.</alert>")
print("Nothing to log.", style="alert")
return
tstamp = datetime.datetime.now().strftime("%Y-%m-%d-%H%M%Z")
outfilename = f"{tstamp}.multiball.log"
@ -260,7 +261,7 @@ class Multiball:
if (not outfilename.endswith(".log")):
outfilename = outfilename + ".log"
print(f"<alert>Writing {outfilename} for command `{self.last_run['cmd']}`.</alert>")
print(f"Writing {outfilename} for command `{self.last_run['cmd']}`.", style="alert")
with (open(outfilename, 'w')) as out:
out.writelines([f"command: {self.last_run['cmd']}\n",
"-------------------------------------------\n"
@ -271,13 +272,13 @@ class Multiball:
def _print_targetlist(self, tlist=None, keyword='Targeting'):
if not tlist:
tlist = self.targethosts
print(f"<message>{keyword} {len(self.targethosts)} hosts:</message>")
print(f"{keyword} {len(self.targethosts)} hosts:")
print(' '.join(tlist))
def _print_help(self):
for item in COMMANDS.values():
m = ', '.join(item[0]) + ' - ' + item[1]
print(f"<bright>{m}</bright>")
print(f"{m}", style="bright")
print("Other entries run command on targets.")
def _run_command(self, command: str):
@ -389,10 +390,10 @@ class Multiball:
except MBCommandRun:
# remote command!
if self.safety:
print(f"<bright>would run `{command}` on targethosts</bright>")
print("<alert>Safety is ON. `/safety off` to turn off.</alert>")
print(f"would run `{command}` on targethosts", style="bright")
print("Safety is ON. `/safety off` to turn off.", style="alert")
else:
print(f"<bright>running `{command}` on targethosts</bright>")
print(f"running `{command}` on targethosts", style="bright")
self._run_remote_command(command)
def cmd_prompt(self):
@ -447,7 +448,7 @@ def multiball(argv):
print('Need at least one --command')
return 1
print(f"<alert>Multiball {__version__}.</alert>")
print(f"Multiball {__version__}.", style="alert")
mb = Multiball()
if (args.all):
@ -467,7 +468,7 @@ def multiball(argv):
return 0
## Interactive mode
print(f"<alert>Welcome to Multiball {__version__}. type /help for help.</alert>")
print(f"Welcome to Multiball {__version__}. type /help for help.", style="alert")
mb = Multiball()
while (True):
# loop on prompt
@ -484,7 +485,7 @@ def main():
sys.exit(0)
except Exception as inst:
# fixme log exception
print(f"<error>Exception! {inst}</error")
print(f"Exception! {inst}", style="error")
print(traceback.format_exc())
sys.exit(-1)

View File

@ -17,11 +17,11 @@ STYLES = Style.from_dict({
"bright": "#ff0 bg:#000",
})
def print_style(*args, **kwargs):
style="message"
if ("style" in kwargs):
style=kwargs["style"]
def print(message):
message = message.replace("&", "&amp;")
print_formatted_text(HTML(f"<default>{message}</default>"), style=STYLES)
message = " ".join([str(a) for a in args])
def print_style(message, style):
print_formatted_text(FormattedText([(f"class:{style}", message)]), style=STYLES)