From e1ca2c8ca0a85c6b2cc9ec5e0f360a996d19ad41 Mon Sep 17 00:00:00 2001 From: fisk Date: Wed, 21 Jun 2017 17:30:28 +0100 Subject: [PATCH] Defer terminal detection to the adapter itself --- adapter/__init__.py | 19 +++++++++---------- adapter/base.py | 16 ++++++++++++++++ adapter/implementations/ITerm.py | 13 +++++++++---- adapter/implementations/NullAdapter.py | 8 ++++++-- adapter/implementations/Terminology.py | 4 ++++ adapter/implementations/Tilix.py | 4 ++++ 6 files changed, 48 insertions(+), 16 deletions(-) diff --git a/adapter/__init__.py b/adapter/__init__.py index eaa8a40..6729897 100644 --- a/adapter/__init__.py +++ b/adapter/__init__.py @@ -1,10 +1,14 @@ -import os - from adapter.implementations.ITerm import ITerm from adapter.implementations.NullAdapter import NullAdapter from adapter.implementations.Terminology import Terminology from adapter.implementations.Tilix import Tilix +available_terminals = [ + Terminology, + Tilix, + ITerm +] + def identify(): """ @@ -12,13 +16,8 @@ def identify(): :return: A terminal adapter interface or a NullAdapter. :rtype: TerminalAdapterInterface """ - if os.environ.get("TERMINOLOGY") == '1': - return Terminology() - - if "TILIX_ID" in os.environ: - return Tilix() - - if os.environ.get("ITERM_PROFILE"): - return ITerm() + for terminal in available_terminals: + if terminal.is_available(): + return terminal() return NullAdapter() diff --git a/adapter/base.py b/adapter/base.py index 0b92bac..d12b2a9 100644 --- a/adapter/base.py +++ b/adapter/base.py @@ -1,6 +1,22 @@ class TerminalAdapterInterface(object): + @staticmethod + def is_available(): + """ + :return: True if the environment implies we are using this terminal. + :rtype bool + """ + raise NotImplementedError() + def set_pokemon(self, pokemon): + """ + Set the background image of the terminal. + :param pokemon: Information about a Pokémon. + :type pokemon: dict + """ raise NotImplementedError() def clear(self): + """ + Clear the terminal's background image. + """ raise NotImplementedError() diff --git a/adapter/implementations/ITerm.py b/adapter/implementations/ITerm.py index 3599a24..bad8a8b 100644 --- a/adapter/implementations/ITerm.py +++ b/adapter/implementations/ITerm.py @@ -1,9 +1,14 @@ +import os import subprocess from adapter.base import TerminalAdapterInterface class ITerm(TerminalAdapterInterface): + @staticmethod + def is_available(): + return os.environ.get("ITERM_PROFILE") + def __generate_osascript(self, path): # Create the content for script that will change the terminal background image. content = "tell application \"iTerm\"\n" @@ -19,10 +24,10 @@ class ITerm(TerminalAdapterInterface): p.communicate() p.stdin.close() - def clear(self): - stdin = self.__generate_osascript("") - self.__run_osascript(str.encode(stdin)) - def set_pokemon(self, pokemon): stdin = self.__generate_osascript(pokemon.get_path()) self.__run_osascript(str.encode(stdin)) + + def clear(self): + stdin = self.__generate_osascript("") + self.__run_osascript(str.encode(stdin)) diff --git a/adapter/implementations/NullAdapter.py b/adapter/implementations/NullAdapter.py index 83f099a..6b8fa6e 100644 --- a/adapter/implementations/NullAdapter.py +++ b/adapter/implementations/NullAdapter.py @@ -4,8 +4,12 @@ from adapter.base import TerminalAdapterInterface class NullAdapter(TerminalAdapterInterface): err = "This terminal emulator is not supported." - def clear(self): - print(self.err) + @staticmethod + def is_available(): + return True def set_pokemon(self, pokemon): print(self.err) + + def clear(self): + print(self.err) diff --git a/adapter/implementations/Terminology.py b/adapter/implementations/Terminology.py index 9c9fba5..e322300 100644 --- a/adapter/implementations/Terminology.py +++ b/adapter/implementations/Terminology.py @@ -4,6 +4,10 @@ from adapter.base import TerminalAdapterInterface class Terminology(TerminalAdapterInterface): + @staticmethod + def is_available(): + return os.environ.get("TERMINOLOGY") == '1' + def set_pokemon(self, pokemon): os.system('tybg "{}"'.format(pokemon.get_path())) diff --git a/adapter/implementations/Tilix.py b/adapter/implementations/Tilix.py index 350ddc8..7c723b0 100644 --- a/adapter/implementations/Tilix.py +++ b/adapter/implementations/Tilix.py @@ -7,6 +7,10 @@ class Tilix(TerminalAdapterInterface): setting_key = "com.gexperts.Tilix.Settings" setting_field = "background-image" + @staticmethod + def is_available(): + return "TILIX_ID" in os.environ + def set_pokemon(self, pokemon): command = 'gsettings set {} {} "{}"' os.system(command.format(self.setting_key,