mirror of
https://github.com/LazoCoder/Pokemon-Terminal
synced 2025-02-17 05:18:31 +00:00
Implements adapter pattern to wallpappers
This commit is contained in:
parent
f83821a683
commit
b035b4f212
5 changed files with 117 additions and 35 deletions
|
@ -1,37 +1,8 @@
|
|||
# Used for creating, running and analyzing applescript and bash scripts.
|
||||
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
|
||||
from pokemonterminal.adapter import identify
|
||||
|
||||
|
||||
osa_script_fmt = """tell application "System Events"
|
||||
\ttell current desktop
|
||||
\t\tset picture to "{}"
|
||||
\tend tell
|
||||
end tell"""
|
||||
|
||||
|
||||
def __run_osascript(stream):
|
||||
p = subprocess.Popen(['osascript'], stdout=subprocess.PIPE,
|
||||
stdin=subprocess.PIPE)
|
||||
p.stdin.write(stream)
|
||||
p.communicate()
|
||||
p.stdin.close()
|
||||
|
||||
|
||||
def __linux_create_wallpaper_script(image_file_path):
|
||||
# If its gnome... aka GDMSESSION=gnome-xorg, etc.
|
||||
if "gnome" in os.environ.get("GDMSESSION"):
|
||||
fmt = 'gsettings set org.gnome.desktop.background ' +\
|
||||
'picture-uri "file://{}"'
|
||||
return fmt.format(image_file_path)
|
||||
# elif condition of KDE...
|
||||
else:
|
||||
print("Window manager not supported ")
|
||||
exit(1)
|
||||
from .wallpaper import get_current_adapters
|
||||
|
||||
|
||||
def clear_terminal():
|
||||
|
@ -53,8 +24,27 @@ def change_wallpaper(image_file_path):
|
|||
if not isinstance(image_file_path, str):
|
||||
print("A image path must be passed to the change wallpapper function.")
|
||||
return
|
||||
if sys.platform == "darwin":
|
||||
script = osa_script_fmt.format(image_file_path)
|
||||
__run_osascript(str.encode(script))
|
||||
elif sys.platform == "linux":
|
||||
os.system(__linux_create_wallpaper_script(image_file_path))
|
||||
providers = get_current_adapters()
|
||||
if len(providers) > 1:
|
||||
# All this if is really not supposed to happen at all whatsoever
|
||||
# really what kind of person has 2 simultaneous D.E???
|
||||
print("Multiple providers found select the appropriate one:")
|
||||
[print(str(x)) for x in providers]
|
||||
print("If some of these make no sense or are irrelevant please file" +
|
||||
"an issue in https://github.com/LazoCoder/Pokemon-Terminal")
|
||||
print("=> ", end='')
|
||||
inp = None
|
||||
while inp is None:
|
||||
try:
|
||||
inp = int(input())
|
||||
if inp >= len(providers):
|
||||
raise ValueError()
|
||||
except ValueError as _:
|
||||
print("Invalid number, try again!")
|
||||
target = providers[inp]
|
||||
elif len(providers) <= 0:
|
||||
print("Your desktop environment isn't supported by this time.")
|
||||
sys.exit()
|
||||
else:
|
||||
target = providers[0]
|
||||
target.change_wallpaper(image_file_path)
|
||||
|
|
29
pokemonterminal/wallpaper/__init__.py
Normal file
29
pokemonterminal/wallpaper/__init__.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
import os
|
||||
import importlib
|
||||
import inspect
|
||||
from .adapters import WallpaperProvider
|
||||
|
||||
|
||||
def _is_adapter(member) -> bool:
|
||||
return (inspect.isclass(member)
|
||||
and issubclass(member, WallpaperProvider)
|
||||
and member != WallpaperProvider)
|
||||
|
||||
|
||||
def _get_adapter_classes() -> [WallpaperProvider]:
|
||||
dirname = os.path.join(os.path.dirname(
|
||||
os.path.abspath(__file__)), 'adapters')
|
||||
adapter_classes = []
|
||||
for file_name in sorted(os.listdir(dirname)):
|
||||
root, ext = os.path.splitext(file_name)
|
||||
if ext.lower() == '.py' and not root.startswith('__'):
|
||||
module = importlib.import_module(
|
||||
'.' + root, 'pokemonterminal.wallpaper.adapters')
|
||||
for _, c in inspect.getmembers(module, _is_adapter):
|
||||
adapter_classes.append(c)
|
||||
return adapter_classes
|
||||
|
||||
|
||||
def get_current_adapters() -> [WallpaperProvider]:
|
||||
arr = _get_adapter_classes()
|
||||
return [x for x in arr if x.is_compatible()]
|
21
pokemonterminal/wallpaper/adapters/__init__.py
Normal file
21
pokemonterminal/wallpaper/adapters/__init__.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
class WallpaperProvider:
|
||||
"""
|
||||
Interface representing all the different desktop environments supported
|
||||
by pokemon-terminal if you want to implement a DE, create a module in this
|
||||
folder that implements this interface, reflection will do the rest.
|
||||
"""
|
||||
|
||||
def change_wallpaper(path: str):
|
||||
"""
|
||||
This sets the wallpaper of the corresponding D.E of this adapter.
|
||||
:param path The full path of the required pokemon image
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def is_compatible() -> bool:
|
||||
"""
|
||||
checks for compatibility
|
||||
:return a boolean saying whether or not the current adaptor is
|
||||
compatible with the running D.E
|
||||
"""
|
||||
raise NotImplementedError()
|
28
pokemonterminal/wallpaper/adapters/darwin.py
Normal file
28
pokemonterminal/wallpaper/adapters/darwin.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
from . import WallpaperProvider as _WProv
|
||||
import subprocess as __sp
|
||||
import sys as __sys
|
||||
|
||||
|
||||
class DarwinProvider(_WProv):
|
||||
__osa_script_fmt = """tell application "System Events"
|
||||
\ttell current desktop
|
||||
\t\tset picture to "{}"
|
||||
\tend tell
|
||||
end tell"""
|
||||
|
||||
def __run_osascript(stream):
|
||||
p = __sp.Popen(['osascript'], stdout=__sp.PIPE,
|
||||
stdin=__sp.PIPE)
|
||||
p.stdin.write(stream)
|
||||
p.communicate()
|
||||
p.stdin.close()
|
||||
|
||||
def change_wallpaper(path: str) -> None:
|
||||
script = DarwinProvider.__osa_script_fmt.format(path)
|
||||
DarwinProvider.__run_osascript(str.encode(script))
|
||||
|
||||
def is_compatible() -> bool:
|
||||
return __sys.platform == "darwin"
|
||||
|
||||
def __str__():
|
||||
return "MacOS Desktop Environment"
|
14
pokemonterminal/wallpaper/adapters/gnome.py
Normal file
14
pokemonterminal/wallpaper/adapters/gnome.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
from . import WallpaperProvider as _WProv
|
||||
import os as _os
|
||||
|
||||
|
||||
class GnomeProvider(_WProv):
|
||||
def change_wallpaper(path: str) -> None:
|
||||
_os.system('gsettings set org.gnome.desktop.background ' +
|
||||
f'picture-uri "file://{path}"')
|
||||
|
||||
def is_compatible() -> bool:
|
||||
return "gnome" in _os.environ.get("GDMSESSION")
|
||||
|
||||
def __str__():
|
||||
return "GNOME Shell Desktop"
|
Loading…
Add table
Reference in a new issue