Merge pull request #47 from Fiskie/adapters

Use adapters for each supported terminal
This commit is contained in:
Lazo 2017-06-21 18:40:31 -04:00 committed by GitHub
commit d6add97c04
16 changed files with 149 additions and 167 deletions

24
.gitignore vendored
View file

@ -1,4 +1,3 @@
# Created by https://www.gitignore.io/api/osx,python,pycharm
### OSX ###
@ -121,28 +120,7 @@ ENV/
### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
# Sensitive or high-churn files:
.idea/dataSources/
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml
# Gradle:
.idea/gradle.xml
.idea/libraries
# Mongo Explorer plugin:
.idea/mongoSettings.xml
.idea/
## File-based project format:
*.iws

View file

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>

11
.idea/misc.xml generated
View file

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.5.2 (/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5)" project-jdk-type="Python SDK" />
<component name="PyConsoleOptionsProvider">
<option name="myPythonConsoleState">
<console-settings sdk-home="/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5">
<option name="mySdkHome" value="/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5" />
</console-settings>
</option>
</component>
</project>

8
.idea/modules.xml generated
View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Pokemon-Terminal-Themes.iml" filepath="$PROJECT_DIR$/.idea/Pokemon-Terminal-Themes.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated
View file

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View file

@ -1,5 +0,0 @@
tell application "iTerm"
tell current session of current window
set background image to "/Users/Laki/Documents/GitHub/Pokemon-Terminal/./Images/Generation I - Kanto/151.png"
end tell
end tell

View file

@ -1,2 +0,0 @@
#!/bin/bash
osascript /Users/Laki/Documents/GitHub/Pokemon-Terminal/./Scripts/background.scpt

View file

@ -1,5 +0,0 @@
tell application "System Events"
tell current desktop
set picture to "/Users/Laki/Documents/GitHub/Pokemon-Terminal/./Images/Extra/deoxys-attack.png"
end tell
end tell

23
adapter/__init__.py Normal file
View file

@ -0,0 +1,23 @@
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():
"""
Identify the terminal we are using based on env vars.
:return: A terminal adapter interface or a NullAdapter.
:rtype: TerminalAdapterInterface
"""
for terminal in available_terminals:
if terminal.is_available():
return terminal()
return NullAdapter()

22
adapter/base.py Normal file
View file

@ -0,0 +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()

View file

@ -0,0 +1,33 @@
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"
content += "\ttell current session of current window\n"
content += "\t\tset background image to \"" + path + "\"\n"
content += "\tend tell\n"
content += "end tell"
return content
def __run_osascript(self, stream):
p = subprocess.Popen(['osascript'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write(stream)
p.communicate()
p.stdin.close()
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))

View file

@ -0,0 +1,15 @@
from adapter.base import TerminalAdapterInterface
class NullAdapter(TerminalAdapterInterface):
err = "This terminal emulator is not supported."
@staticmethod
def is_available():
return True
def set_pokemon(self, pokemon):
print(self.err)
def clear(self):
print(self.err)

View file

@ -0,0 +1,15 @@
import os
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()))
def clear(self):
os.system("tybg")

View file

@ -0,0 +1,23 @@
import os
from adapter.base import TerminalAdapterInterface
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,
self.setting_field,
pokemon.get_path()))
def clear(self):
command = 'gsettings set {} {}'
os.system(command.format(self.setting_key,
self.setting_field))

View file

View file

@ -2,18 +2,19 @@
import os
import sys
import subprocess
cwd = os.path.dirname(os.path.realpath(__file__))
from adapter import identify
def __terminal_script(path):
# Create the content for script that will change the terminal background image.
content = "tell application \"iTerm\"\n"
content += "\ttell current session of current window\n"
content += "\t\tset background image to \"" + path + "\"\n"
content += "\tend tell\n"
content += "end tell"
return content
def clear_terminal():
adapter = identify()
adapter.clear()
def change_terminal(pokemon):
adapter = identify()
adapter.set_pokemon(pokemon)
def __wallpaper_script(pokemon):
@ -26,101 +27,21 @@ def __wallpaper_script(pokemon):
return content
def __iterm2_create_terminal_script(pokemon):
# Create and save the script for changing the terminal background image.
content = __terminal_script(pokemon.get_path())
file = open(cwd + "/./Scripts/background.scpt", "wb")
file.write(bytes(content, 'UTF-8'))
file.close()
def __iterm2_clear_script():
# Create and save the script for clearing the terminal background image.
content = __terminal_script("")
file = open(cwd + "/./Scripts/background.scpt", "wb")
file.write(bytes(content, 'UTF-8'))
file.close()
def __darwin_create_wallpaper_script(pokemon):
# Create and save the script for changing the wallpaper.
content = __wallpaper_script(pokemon)
file = open(cwd + "/./Scripts/wallpaper.scpt", "wb")
file.write(bytes(content, 'UTF-8'))
file.close()
def __darwin_create_terminal_bash():
# Create and save the run.sh that will execute the AppleScript if the correct run.sh doesn't already exist.
content = "#!/bin/bash\n" + "osascript " + cwd + "/./Scripts/background.scpt"
if open(cwd + "/./Scripts/run.sh", 'r').read() == content:
return
file = open(cwd + "/./Scripts/run.sh", 'wb')
file.write(bytes(content, 'UTF-8'))
file.close()
# Create and save the run.sh that will execute the AppleScript if the correct run.sh
# doesn't already exist.
def __darwin_create_wallpaper_bash():
content = "#!/bin/bash\n" + "osascript " + cwd + "/./Scripts/wallpaper.scpt"
if open(cwd + "/./Scripts/run.sh", 'r').read() == content:
return
file = open(cwd + "/./Scripts/run.sh", 'wb')
file.write(bytes(content, 'UTF-8'))
file.close()
def change_terminal(pokemon):
if sys.platform == "darwin":
# Create, save and run the bash script to change the terminal background.
__iterm2_create_terminal_script(pokemon)
__darwin_create_terminal_bash()
os.system(cwd + "/./Scripts/run.sh")
if sys.platform == "linux":
os.system(__linux_create_terminal(pokemon))
def __linux_create_terminal(pokemon):
if os.environ.get("TERMINOLOGY") == '1':
return "tybg \"" + pokemon.get_path() + "\""
elif "TILIX_ID" in os.environ:
return "gsettings set com.gexperts.Tilix.Settings background-image " + \
"\""+ pokemon.get_path()+"\""
else:
print("Terminal emulator not supported")
exit(1)
def __linux_clear_terminal():
if os.environ.get("TERMINOLOGY") == '1':
return "tybg"
elif "TILIX_ID" in os.environ:
return "gsettings reset com.gexperts.Tilix.Settings background-image"
else:
print("Terminal emulator not supported")
exit(1)
def __run_osascript(stream):
p = subprocess.Popen(['osascript'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write(stream)
p.communicate()
p.stdin.close()
def change_wallpaper(pokemon):
if sys.platform == "darwin":
# Create, save and run the bash script to change the wallpaper.
__darwin_create_wallpaper_script(pokemon)
__darwin_create_wallpaper_bash()
os.system(cwd + "/./Scripts/run.sh")
script = __wallpaper_script(pokemon)
__run_osascript(str.encode(script))
if sys.platform == "linux":
os.system(__linux_create_wallpaper_script(pokemon))
def clear_terminal():
if sys.platform == "darwin":
__iterm2_clear_script()
__darwin_create_terminal_bash()
os.system(cwd + "/./Scripts/run.sh")
if sys.platform == "linux":
os.system(__linux_clear_terminal())
def __linux_create_wallpaper_script(pokemon):
# If its gnome... aka GDMSESSION=gnome-xorg, etc.
if os.environ.get("GDMSESSION").find("gnome") >= 0: