Refactor slideshow, add Windows wallpaper changer, and improve consistency in adapter code

This commit is contained in:
Charles Milette 2018-07-18 22:17:52 -04:00 committed by Samuel Henrique
parent 3e378f770c
commit e0934b61dd
11 changed files with 81 additions and 82 deletions

View file

@ -1,13 +1,13 @@
""" """
Pokemon Terminal Themes. Pokemon Terminal Themes.
493 unique Pokemon, 719 unique Pokemon.
from Kanto, Johto, Hoenn and Sinnoh. from Kanto, Johto, Hoenn, Sinnoh, Unova, and Kalos.
Change the Terminal Backgroun & Desktop Wallpaper. Change the Terminal Background & Desktop Wallpaper.
Supports ITerm2, Terminology & Tilix. Supports iTerm2, Terminology, Tilix and ConEmu.
""" """
__version__ = "0.0.1" __version__ = "1.1.0"
__author__ = "LazoCoder" __author__ = "LazoCoder"

View file

@ -4,56 +4,14 @@
import os import os
import random import random
import sys import sys
import time
from multiprocessing import Process from multiprocessing import Process
from pathlib import Path from pathlib import Path
from . import scripter from . import scripter, slideshow
from pokemonterminal.command_flags import parser, is_slideshow from pokemonterminal.command_flags import parser, is_slideshow
from pokemonterminal.database import Database from pokemonterminal.database import Database
from pokemonterminal.filters import Filter 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): def main(argv=None):
@ -111,26 +69,16 @@ def main(argv=None):
return return
if options.clear: 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() scripter.clear_terminal()
return return
if is_slideshow and options.id <= 0 and size > 1: 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: if options.slideshow <= 0:
print("Time has to be greater then 0. You can use decimal values.") print("Time has to be greater then 0. You can use decimal values.")
return return
target_func = scripter.change_wallpaper if options.wallpaper else \ target_func = scripter.change_wallpaper if options.wallpaper else \
scripter.change_terminal scripter.change_terminal
slideshow(Filter.filtered_list, options.slideshow, target_func) slideshow.start(Filter.filtered_list, options.slideshow, target_func)
return return
if options.wallpaper: if options.wallpaper:

View 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)

View file

@ -10,20 +10,16 @@ class ConEmuProvider(_TProv):
def is_compatible() -> bool: def is_compatible() -> bool:
return "CONEMUPID" in os.environ return "CONEMUPID" in os.environ
def change_terminal(path: str): def __run_command(command: str):
# ConEmu has issues with the quoting when using an arg list, so use shell=True output = subprocess.check_output(f'ConEmuC -GuiMacro {command}', shell=True).decode(sys.stdout.encoding)
# 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)
if output != 'OK': if output != 'OK':
print(output) print(output)
def change_terminal(path: str):
ConEmuProvider.__run_command(f'SetOption("Background Image", "{path}")')
def clear(): def clear():
subprocess.run('ConEmuC -GuiMacro SetOption("Background Image", "")', shell=True, check=True) ConEmuProvider.__run_command('SetOption("Background Image", "")')
def __str__(): def __str__():
return "ConEmu" return "ConEmu"

View file

@ -9,10 +9,10 @@ class TerminologyProvider(_TProv):
return environ.get("TERMINOLOGY") == '1' return environ.get("TERMINOLOGY") == '1'
def change_terminal(path: str): def change_terminal(path: str):
run(f'tybg "{path}"', shell=True, check=True) run(f'tybg "{path}"', check=True)
def clear(): def clear():
run("tybg", shell=True, check=True) run("tybg", check=True)
def __str__(): def __str__():
return "Terminology" return "Terminology"

View file

@ -13,11 +13,11 @@ class TilixProvider(_TProv):
def change_terminal(path: str): def change_terminal(path: str):
command = f'gsettings set {TilixProvider.setting_key} {TilixProvider.setting_field} "{path}"' command = f'gsettings set {TilixProvider.setting_key} {TilixProvider.setting_field} "{path}"'
run(command, shell=True, check=True) run(command, check=True)
def clear(): def clear():
command = f'gsettings reset {TilixProvider.setting_key} {TilixProvider.setting_field}' command = f'gsettings reset {TilixProvider.setting_key} {TilixProvider.setting_field}'
run(command, shell=True, check=True) run(command, check=True)
def __str__(): def __str__():
return "Tilix" return "Tilix"

View file

@ -18,7 +18,7 @@ class DarwinProvider(_WProv):
p.communicate() p.communicate()
p.stdin.close() p.stdin.close()
def change_wallpaper(path: str) -> None: def change_wallpaper(path: str):
script = DarwinProvider.__osa_script_fmt.format(path) script = DarwinProvider.__osa_script_fmt.format(path)
DarwinProvider.__run_osascript(str.encode(script)) DarwinProvider.__run_osascript(str.encode(script))

View file

@ -7,21 +7,22 @@ from . import WallpaperProvider as _WProv
class FehProvider(_WProv): class FehProvider(_WProv):
compatible_wm = ('I3_PID', '_OPENBOX_PID') compatible_wm = ['I3_PID', '_OPENBOX_PID']
def change_wallpaper(path: str): def change_wallpaper(path: str):
if (Path.home() / '.fehbg').is_file(): if (Path.home() / '.fehbg').is_file():
command = f'feh --bg-fill "{path}"' command = f'feh --bg-fill "{path}"'
else: else:
command = f'feh --no-fehbg --bg-fill "{path}"' 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: 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: def is_compatible() -> bool:
return which("feh") is not None and which("xprop") is not None and \ return (which("feh") is not None
any(wm_signature in FehProvider.__get_root_props() for wm_signature in FehProvider.compatible_wm) and which("xprop") is not None
and any(wm_signature in FehProvider.__get_root_props() for wm_signature in FehProvider.compatible_wm))
def __str__(): def __str__():
return "feh wallpaper tool" return "feh wallpaper tool"

View file

@ -6,10 +6,10 @@ from . import WallpaperProvider as _WProv
class GnomeProvider(_WProv): class GnomeProvider(_WProv):
def change_wallpaper(path: str) -> None: 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: def is_compatible() -> bool:
return "gnome" in environ.get("GDMSESSION").lower() return "gnome" in environ.get("DESKTOP_SESSION", default='').lower()
def __str__(): def __str__():
return "GNOME Shell Desktop" return "GNOME Shell Desktop"

View 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"

View file

@ -36,7 +36,7 @@ Pokemon Terminal Themes.
from Kanto, Johto, Hoenn, Sinnoh, Unova, and Kalos. from Kanto, Johto, Hoenn, Sinnoh, Unova, and Kalos.
Change the Terminal Background & Desktop Wallpaper. 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", url="https://github.com/LazoCoder/Pokemon-Terminal",
author="LazoCoder", author="LazoCoder",