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:
Guillaume Raffy 2026-06-17 10:32:07 +02:00
parent 0e7309d3b6
commit 6bc44363de
1 changed files with 45 additions and 13 deletions

View File

@ -4,6 +4,7 @@ from typing import Optional, List
import argparse
import re
import subprocess
import abc
class RgbColor():
@ -40,6 +41,20 @@ class SshTarget():
target_as_str = f'{self.user_id}@{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()
# {
@ -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)
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():
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')
@ -277,10 +309,12 @@ def main():
# AS_ROOT='true'
# fi
color_saturation = 0.3
color_value = 0.2
proc = subprocess.run(['uname'], check=True, stdout=subprocess.PIPE)
os_name = proc.stdout.decode('utf8').strip()
terminal_launcher: Optional[ITerminalLauncher] = None
if os_name == 'Darwin':
raise NotImplementedError()
# 'Darwin')
@ -292,22 +326,20 @@ def main():
# /usr/bin/ssh "$@"
# ;;
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)
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)
assert match, f'unexpected color as string: {bg_color_as_str}'
bg_color = RgbColor(match['red'], match['green'], match['blue'])
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)
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()
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}'
bg_color = RgbColor(match['red'], match['green'], match['blue'])
logging.debug('bg_color = %s', str(bg_color)) # eg (35, 43, 51)
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()