refactored cssh (added ITerminalLauncher)
this actract class better organises the code, and will allow a clear separation between macosx functions and gnome related functions work related to [https://bugzilla.ipr.univ-rennes.fr/show_bug.cgi?id=4405]
This commit is contained in:
parent
0e7309d3b6
commit
6bc44363de
|
|
@ -4,6 +4,7 @@ from typing import Optional, List
|
||||||
import argparse
|
import argparse
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import abc
|
||||||
|
|
||||||
|
|
||||||
class RgbColor():
|
class RgbColor():
|
||||||
|
|
@ -40,6 +41,20 @@ class SshTarget():
|
||||||
target_as_str = f'{self.user_id}@{target_as_str}'
|
target_as_str = f'{self.user_id}@{target_as_str}'
|
||||||
return target_as_str
|
return target_as_str
|
||||||
|
|
||||||
|
|
||||||
|
TerminalLabel: str # label used as the title and the color profile of the terminal
|
||||||
|
|
||||||
|
class ITerminalLauncher(abc.ABC):
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def launch_terminal(self, terminal_label: TerminalLabel, bg_color: RgbColor, command: List[str]):
|
||||||
|
'''
|
||||||
|
terminal_label: eg 'alambix50.ipr.univ-rennes.fr'
|
||||||
|
command: the command to execute in the terminal eg ['/usr/bin/ssh', 'root@alambix50.ipr.univ-rennes.fr']
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# '''
|
# '''
|
||||||
# function getHostRealFqdn()
|
# function getHostRealFqdn()
|
||||||
# {
|
# {
|
||||||
|
|
@ -262,6 +277,23 @@ class GnomeTerminal():
|
||||||
subprocess.run(['dconf', 'write', f"{GnomeTerminal.DCONF_PROFILES_PATH}/{profile_uuid}/background-color", f'({bg_color.red}, {bg_color.green}, {bg_color.blue})'], check=True, stdout=subprocess.PIPE)
|
subprocess.run(['dconf', 'write', f"{GnomeTerminal.DCONF_PROFILES_PATH}/{profile_uuid}/background-color", f'({bg_color.red}, {bg_color.green}, {bg_color.blue})'], check=True, stdout=subprocess.PIPE)
|
||||||
|
|
||||||
|
|
||||||
|
class GnomeTerminalLauncher(ITerminalLauncher):
|
||||||
|
|
||||||
|
def launch_terminal(self, terminal_label: TerminalLabel, bg_color: RgbColor, command: List[str]):
|
||||||
|
'''
|
||||||
|
command: the command to execute in the terminal eg ['/usr/bin/ssh', f'{str(ssh_target)}']
|
||||||
|
'''
|
||||||
|
|
||||||
|
GnomeTerminal.set_terminal_profile_bg_color(terminal_label, bg_color)
|
||||||
|
|
||||||
|
profile_uuid = GnomeTerminal.get_terminal_profile_uuid(terminal_label)
|
||||||
|
|
||||||
|
launch_term_command = ['gnome-terminal', f'--window-with-profile={profile_uuid}', f'--title={terminal_label}', '--'] + command
|
||||||
|
logging.debug('launch_term_command = %s', launch_term_command)
|
||||||
|
subprocess.run(launch_term_command, check=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
arg_parser = argparse.ArgumentParser('cssh (colored ssh) opens a terminal window with a background color that depends on the fully qualified domain name of the target machine')
|
arg_parser = argparse.ArgumentParser('cssh (colored ssh) opens a terminal window with a background color that depends on the fully qualified domain name of the target machine')
|
||||||
|
|
@ -277,10 +309,12 @@ def main():
|
||||||
# AS_ROOT='true'
|
# AS_ROOT='true'
|
||||||
# fi
|
# fi
|
||||||
|
|
||||||
|
|
||||||
color_saturation = 0.3
|
color_saturation = 0.3
|
||||||
color_value = 0.2
|
color_value = 0.2
|
||||||
proc = subprocess.run(['uname'], check=True, stdout=subprocess.PIPE)
|
proc = subprocess.run(['uname'], check=True, stdout=subprocess.PIPE)
|
||||||
os_name = proc.stdout.decode('utf8').strip()
|
os_name = proc.stdout.decode('utf8').strip()
|
||||||
|
terminal_launcher: Optional[ITerminalLauncher] = None
|
||||||
if os_name == 'Darwin':
|
if os_name == 'Darwin':
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
# 'Darwin')
|
# 'Darwin')
|
||||||
|
|
@ -292,22 +326,20 @@ def main():
|
||||||
# /usr/bin/ssh "$@"
|
# /usr/bin/ssh "$@"
|
||||||
# ;;
|
# ;;
|
||||||
elif os_name == 'Linux':
|
elif os_name == 'Linux':
|
||||||
proc = subprocess.run(['make_color.py', ssh_target.host_id, str(color_value), str(color_saturation), 'linux'], check=True, stdout=subprocess.PIPE)
|
|
||||||
|
terminal_launcher = GnomeTerminalLauncher()
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(f'unhandled os : "{os_name}"')
|
||||||
|
|
||||||
|
proc = subprocess.run(['make_color.py', ssh_target.host_id, str(color_value), str(color_saturation), os_name.lower()], check=True, stdout=subprocess.PIPE)
|
||||||
bg_color_as_str = proc.stdout.decode('utf8').strip()
|
bg_color_as_str = proc.stdout.decode('utf8').strip()
|
||||||
match = re.match(r'\((?P<red>[0-9]+), (?P<green>[0-9]+), (?P<blue>[0-9]+)\)', bg_color_as_str)
|
match = re.match(r'\((?P<red>[0-9]+), (?P<green>[0-9]+), (?P<blue>[0-9]+)\)', bg_color_as_str)
|
||||||
assert match, f'unexpected color as string: {bg_color_as_str}'
|
assert match, f'unexpected color as string: {bg_color_as_str}'
|
||||||
bg_color = RgbColor(match['red'], match['green'], match['blue'])
|
bg_color = RgbColor(match['red'], match['green'], match['blue'])
|
||||||
logging.debug('bg_color = %s', str(bg_color)) # eg (35, 43, 51)
|
logging.debug('bg_color = %s', str(bg_color)) # eg (35, 43, 51)
|
||||||
|
|
||||||
GnomeTerminal.set_terminal_profile_bg_color(ssh_target.host_id, bg_color)
|
|
||||||
|
|
||||||
profile_uuid = GnomeTerminal.get_terminal_profile_uuid(ssh_target.host_id)
|
|
||||||
|
|
||||||
launch_term_command = ['gnome-terminal', f'--window-with-profile={profile_uuid}', f'--title={ssh_target.host_id}', '--', '/usr/bin/ssh', f'{str(ssh_target)}']
|
|
||||||
logging.debug('launch_term_command = %s', launch_term_command)
|
|
||||||
subprocess.run(launch_term_command, check=True)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(f'unhandled os : "{os_name}"')
|
|
||||||
|
|
||||||
|
command = ['/usr/bin/ssh', f'{str(ssh_target)}']
|
||||||
|
terminal_launcher.launch_terminal(terminal_label=ssh_target.host_id, bg_color=bg_color, command=command)
|
||||||
|
|
||||||
main()
|
main()
|
||||||
Loading…
Reference in New Issue