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