import pytomlpp import os import random import sys from time import sleep from threading import Thread def printHelpInfo(): print("""Usage: wallpyper [command] Commands: quit, stop shutdown wallpyper set set theme help, --help, -h print this information""") def runCommand(commandName, output=False): if output: os.system(commandName) else: os.system(commandName + ">/dev/null") def killBack4(): ''' back4.sh will output artifacts if run more 2 processes It function read PID in /tmp/back4.sh.pid, and kill process. ''' try: with open('/tmp/back4.sh.pid','r') as file: for pid in file: runCommand(f'kill {pid}') except FileNotFoundError: pass def killLastSession(): killBack4() try: with open('/tmp/wallpyper.pid','r') as file: for pid in file: runCommand(f'kill {pid}') except FileNotFoundError: pass with open('/tmp/wallpyper.pid','w') as file: file.write(str(os.getpid())) def getConfig(): configFileName = os.path.expanduser('~')+'/.config/wallpyper/config.toml' with open(configFileName) as file: return pytomlpp.loads(file.read()) def setWallpaper(wallpaperName): killBack4() if wallpaperName[-4:] != '.gif': runCommand(f'feh --bg-scale "{wallpaperName}"') else: runCommand(f'back4.sh auto "{wallpaperName}" & echo $! > /tmp/back4.sh.pid',True) # > tmp/back4.sh.pid write PID for kill() def setRandomWallpaper(path): wallpaperName = path+random.choices(os.listdir(path))[0] setWallpaper(wallpaperName) def setDynamicWallpaper(path, delay): while True: setRandomWallpaper(path) sleep(delay) def setTheme(themeName = None): killLastSession() config = getConfig() if themeName == None: themeName = config["default_theme"] theme = config[themeName] if theme["type"] == "static": try: if theme["random"]: setRandomWallpaper(theme["path"]) else: setWallpaper(theme["url"]) except KeyError: setWallpaper(theme["url"]) elif theme["type"] == "dynamic": delay = list(map(int,theme["delay"].split(":"))) delay.reverse() while len(delay) < 3: delay.append(0) delay = delay[0] + delay[1]*60 + delay[2]*3600 wallpyperThread = Thread(target=setDynamicWallpaper, args=(theme["path"],delay,)) wallpyperThread.start() else: printHelpInfo() sys.exit(0)