mirror of
https://github.com/LazoCoder/Pokemon-Terminal
synced 2024-11-23 04:13:20 +00:00
Refactor slideshow, add Windows wallpaper changer, and improve consistency in adapter code
This commit is contained in:
parent
3e378f770c
commit
e0934b61dd
11 changed files with 81 additions and 82 deletions
|
@ -1,13 +1,13 @@
|
|||
"""
|
||||
Pokemon Terminal Themes.
|
||||
|
||||
493 unique Pokemon,
|
||||
from Kanto, Johto, Hoenn and Sinnoh.
|
||||
719 unique Pokemon.
|
||||
from Kanto, Johto, Hoenn, Sinnoh, Unova, and Kalos.
|
||||
|
||||
Change the Terminal Backgroun & Desktop Wallpaper.
|
||||
Supports ITerm2, Terminology & Tilix.
|
||||
Change the Terminal Background & Desktop Wallpaper.
|
||||
Supports iTerm2, Terminology, Tilix and ConEmu.
|
||||
"""
|
||||
|
||||
|
||||
__version__ = "0.0.1"
|
||||
__version__ = "1.1.0"
|
||||
__author__ = "LazoCoder"
|
||||
|
|
|
@ -4,56 +4,14 @@
|
|||
import os
|
||||
import random
|
||||
import sys
|
||||
import time
|
||||
from multiprocessing import Process
|
||||
from pathlib import Path
|
||||
|
||||
from . import scripter
|
||||
from . import scripter, slideshow
|
||||
from pokemonterminal.command_flags import parser, is_slideshow
|
||||
from pokemonterminal.database import Database
|
||||
from pokemonterminal.filters import Filter
|
||||
|
||||
PIPE_PATH = Path.home() / (".pokemon-terminal-pipe" + str(os.getppid()))
|
||||
PIPE_EXISTS = os.path.exists(PIPE_PATH)
|
||||
|
||||
|
||||
def daemon(time_stamp, pkmn_list):
|
||||
# TODO: Implement messaging, like status and current pokemon
|
||||
if not PIPE_EXISTS:
|
||||
os.mkfifo(PIPE_PATH)
|
||||
pip = open(PIPE_PATH, 'r')
|
||||
while True:
|
||||
for msg in pip:
|
||||
msg = msg.strip()
|
||||
if msg == 'quit':
|
||||
print("Stopping the slideshow")
|
||||
os.remove(PIPE_PATH)
|
||||
sys.exit(0)
|
||||
pip = open(PIPE_PATH, 'r')
|
||||
|
||||
|
||||
def slideshow(filtered, delay, changer_func):
|
||||
pid = os.fork()
|
||||
if pid > 0:
|
||||
print(f"Starting slideshow with {len(filtered)}, pokemon " +
|
||||
f"and a delay of {delay} minutes between pokemon")
|
||||
print("Forked process to background with pid", pid,
|
||||
"you can stop it with -c")
|
||||
os.environ["POKEMON_TERMINAL_PID"] = str(pid)
|
||||
sys.exit(0)
|
||||
p = Process(target=daemon, args=(time.time(), filtered,))
|
||||
p.daemon = True
|
||||
p.start()
|
||||
random.shuffle(filtered)
|
||||
queque = iter(filtered)
|
||||
while p.is_alive():
|
||||
next_pkmn = next(queque, None)
|
||||
if next_pkmn is None:
|
||||
random.shuffle(filtered)
|
||||
queque = iter(filtered)
|
||||
continue
|
||||
changer_func(next_pkmn.get_path())
|
||||
p.join(delay * 60)
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
|
@ -111,26 +69,16 @@ def main(argv=None):
|
|||
return
|
||||
|
||||
if options.clear:
|
||||
if PIPE_EXISTS:
|
||||
pipe_out = os.open(PIPE_PATH, os.O_WRONLY)
|
||||
os.write(pipe_out, b"quit\n")
|
||||
os.close(pipe_out)
|
||||
scripter.clear_terminal()
|
||||
return
|
||||
|
||||
if is_slideshow and options.id <= 0 and size > 1:
|
||||
if os.name == 'nt':
|
||||
print("Slideshow not supported on Windows yet.")
|
||||
sys.exit(0)
|
||||
if PIPE_EXISTS:
|
||||
print("Slideshow already running in this instance!")
|
||||
sys.exit(0)
|
||||
if options.slideshow <= 0:
|
||||
print("Time has to be greater then 0. You can use decimal values.")
|
||||
return
|
||||
target_func = scripter.change_wallpaper if options.wallpaper else \
|
||||
scripter.change_terminal
|
||||
slideshow(Filter.filtered_list, options.slideshow, target_func)
|
||||
slideshow.start(Filter.filtered_list, options.slideshow, target_func)
|
||||
return
|
||||
|
||||
if options.wallpaper:
|
||||
|
|
38
pokemonterminal/slideshow.py
Normal file
38
pokemonterminal/slideshow.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env python3.6
|
||||
import os
|
||||
import random
|
||||
import sys
|
||||
import time
|
||||
|
||||
from threading import Thread
|
||||
|
||||
def __print_fork(pid, length, delay):
|
||||
print(f"Starting slideshow with {length} Pokemons and a delay of {delay} minutes.")
|
||||
print(f"Forked process to background with PID {pid}. You can stop it with 'pokemon -c'.")
|
||||
|
||||
def __exit_listener():
|
||||
raise Exception('TODO')
|
||||
|
||||
def __slideshow_worker(filtered, delay, changer_func):
|
||||
t = Thread(target=__exit_listener)
|
||||
t.start()
|
||||
random.shuffle(filtered)
|
||||
queque = iter(filtered)
|
||||
while t.is_alive():
|
||||
next_pkmn = next(queque, None)
|
||||
if next_pkmn is None:
|
||||
random.shuffle(filtered)
|
||||
queque = iter(filtered)
|
||||
continue
|
||||
changer_func(next_pkmn.get_path())
|
||||
t.join(delay * 60)
|
||||
|
||||
def start(filtered, delay, changer_func):
|
||||
if os.name == 'nt':
|
||||
raise Exception('TODO')
|
||||
else:
|
||||
pid = os.fork()
|
||||
if pid > 0:
|
||||
__print_fork(pid, len(filtered), delay)
|
||||
sys.exit(0)
|
||||
__slideshow_worker(filtered, delay, changer_func)
|
|
@ -10,20 +10,16 @@ class ConEmuProvider(_TProv):
|
|||
def is_compatible() -> bool:
|
||||
return "CONEMUPID" in os.environ
|
||||
|
||||
def change_terminal(path: str):
|
||||
# ConEmu has issues with the quoting when using an arg list, so use shell=True
|
||||
# ConEmu requires the image path to have escaped slashes. Enforce this by converting all slashes in the path to
|
||||
# backlashes, replace all previously doubled slashes with a single slash, then replace all single slashes with
|
||||
# double backslash
|
||||
cmd = f'ConEmuC -GuiMacro SetOption("Background Image", "{os.path.normpath(path)}")' \
|
||||
.replace('\\\\', '\\') \
|
||||
.replace('\\', '\\\\')
|
||||
output = subprocess.check_output(cmd, shell=True).decode(sys.stdout.encoding)
|
||||
def __run_command(command: str):
|
||||
output = subprocess.check_output(f'ConEmuC -GuiMacro {command}', shell=True).decode(sys.stdout.encoding)
|
||||
if output != 'OK':
|
||||
print(output)
|
||||
|
||||
def change_terminal(path: str):
|
||||
ConEmuProvider.__run_command(f'SetOption("Background Image", "{path}")')
|
||||
|
||||
def clear():
|
||||
subprocess.run('ConEmuC -GuiMacro SetOption("Background Image", "")', shell=True, check=True)
|
||||
ConEmuProvider.__run_command('SetOption("Background Image", "")')
|
||||
|
||||
def __str__():
|
||||
return "ConEmu"
|
||||
|
|
|
@ -9,10 +9,10 @@ class TerminologyProvider(_TProv):
|
|||
return environ.get("TERMINOLOGY") == '1'
|
||||
|
||||
def change_terminal(path: str):
|
||||
run(f'tybg "{path}"', shell=True, check=True)
|
||||
run(f'tybg "{path}"', check=True)
|
||||
|
||||
def clear():
|
||||
run("tybg", shell=True, check=True)
|
||||
run("tybg", check=True)
|
||||
|
||||
def __str__():
|
||||
return "Terminology"
|
|
@ -13,11 +13,11 @@ class TilixProvider(_TProv):
|
|||
|
||||
def change_terminal(path: str):
|
||||
command = f'gsettings set {TilixProvider.setting_key} {TilixProvider.setting_field} "{path}"'
|
||||
run(command, shell=True, check=True)
|
||||
run(command, check=True)
|
||||
|
||||
def clear():
|
||||
command = f'gsettings reset {TilixProvider.setting_key} {TilixProvider.setting_field}'
|
||||
run(command, shell=True, check=True)
|
||||
run(command, check=True)
|
||||
|
||||
def __str__():
|
||||
return "Tilix"
|
||||
|
|
|
@ -18,7 +18,7 @@ class DarwinProvider(_WProv):
|
|||
p.communicate()
|
||||
p.stdin.close()
|
||||
|
||||
def change_wallpaper(path: str) -> None:
|
||||
def change_wallpaper(path: str):
|
||||
script = DarwinProvider.__osa_script_fmt.format(path)
|
||||
DarwinProvider.__run_osascript(str.encode(script))
|
||||
|
||||
|
|
|
@ -7,21 +7,22 @@ from . import WallpaperProvider as _WProv
|
|||
|
||||
|
||||
class FehProvider(_WProv):
|
||||
compatible_wm = ('I3_PID', '_OPENBOX_PID')
|
||||
compatible_wm = ['I3_PID', '_OPENBOX_PID']
|
||||
|
||||
def change_wallpaper(path: str):
|
||||
if (Path.home() / '.fehbg').is_file():
|
||||
command = f'feh --bg-fill "{path}"'
|
||||
else:
|
||||
command = f'feh --no-fehbg --bg-fill "{path}"'
|
||||
subprocess.run(command, shell=True, check=True)
|
||||
subprocess.run(command, check=True)
|
||||
|
||||
def __get_root_props() -> str:
|
||||
return subprocess.check_output('xprop -root -notype', shell=True).decode(sys.stdout.encoding)
|
||||
return subprocess.check_output('xprop -root -notype').decode(sys.stdout.encoding)
|
||||
|
||||
def is_compatible() -> bool:
|
||||
return which("feh") is not None and which("xprop") is not None and \
|
||||
any(wm_signature in FehProvider.__get_root_props() for wm_signature in FehProvider.compatible_wm)
|
||||
return (which("feh") is not None
|
||||
and which("xprop") is not None
|
||||
and any(wm_signature in FehProvider.__get_root_props() for wm_signature in FehProvider.compatible_wm))
|
||||
|
||||
def __str__():
|
||||
return "feh wallpaper tool"
|
||||
|
|
|
@ -6,10 +6,10 @@ from . import WallpaperProvider as _WProv
|
|||
|
||||
class GnomeProvider(_WProv):
|
||||
def change_wallpaper(path: str) -> None:
|
||||
run(f'gsettings set org.gnome.desktop.background picture-uri "file://{path}"', shell=True, check=True)
|
||||
run(f'gsettings set org.gnome.desktop.background picture-uri "file://{path}"', check=True)
|
||||
|
||||
def is_compatible() -> bool:
|
||||
return "gnome" in environ.get("GDMSESSION").lower()
|
||||
return "gnome" in environ.get("DESKTOP_SESSION", default='').lower()
|
||||
|
||||
def __str__():
|
||||
return "GNOME Shell Desktop"
|
||||
|
|
16
pokemonterminal/wallpaper/adapters/nt.py
Normal file
16
pokemonterminal/wallpaper/adapters/nt.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
import os
|
||||
|
||||
from . import WallpaperProvider as _WProv
|
||||
from ctypes import *
|
||||
|
||||
class WindowsProvider(_WProv):
|
||||
__SPI_SETDESKWALLPAPER = 20
|
||||
|
||||
def change_wallpaper(path: str):
|
||||
windll.user32.SystemParametersInfoW(WindowsProvider.__SPI_SETDESKWALLPAPER, 0, path, 0)
|
||||
|
||||
def is_compatible() -> bool:
|
||||
return os.name == 'nt'
|
||||
|
||||
def __str__():
|
||||
return "Windows Desktop"
|
2
setup.py
2
setup.py
|
@ -36,7 +36,7 @@ Pokemon Terminal Themes.
|
|||
from Kanto, Johto, Hoenn, Sinnoh, Unova, and Kalos.
|
||||
|
||||
Change the Terminal Background & Desktop Wallpaper.
|
||||
Supports ITerm2, Terminology, Tilix and ConEmu.""",
|
||||
Supports iTerm2, Terminology, Tilix and ConEmu.""",
|
||||
url="https://github.com/LazoCoder/Pokemon-Terminal",
|
||||
|
||||
author="LazoCoder",
|
||||
|
|
Loading…
Reference in a new issue