diff --git a/func.py b/func.py deleted file mode 100644 index 2dcf174..0000000 --- a/func.py +++ /dev/null @@ -1,39 +0,0 @@ -import json -import os -import random -import sys - -def run(command,output=False): - devNull = ">/dev/null" - if output == True: - os.system(command) - else: - os.system(command + devNull) - -def kill(): - ''' - back4.sh will output artifacts if run more 2 processes - It function read PID in /tmp/back4.sh.pid, and kill process. - ''' - with open('/tmp/back4.sh.pid','r') as file: - for pid in file: - run(f'kill {pid}') - -def configParsing(): - pathToConfig = os.path.expanduser('~')+'/.config/wallpyper/config' - with open(pathToConfig) as file: - contentFile = file.read() - config = json.loads(contentFile) - return config - -def setWallPaper(): - PATH = configParsing()['path_to_wallpapers'] - files = os.listdir(PATH) - - media = random.choices(files)[0] - - if media[-4:] != '.gif': - run(f"feh --bg-scale {PATH}{media}") - else: - run(f"back4.sh auto {PATH}{media} & echo $! > /tmp/back4.sh.pid",True) - # > tmp/back4.sh.pid write PID for kill() diff --git a/main.py b/main.py deleted file mode 100755 index b71e19e..0000000 --- a/main.py +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/python3 - -from apscheduler.schedulers.background import BlockingScheduler -from func import * - -def start(): - try: - kill() - except FileNotFoundError: - setWallPaper() - - setWallPaper() - -if __name__ == '__main__': - start() - - sched = BlockingScheduler() - - sched.add_job(start,'cron',\ - second=configParsing()['second'],\ - minute=configParsing()['minute'],\ - hour=configParsing()['hour']) - - sched.start() diff --git a/src/wallpyper.py b/src/wallpyper.py new file mode 100755 index 0000000..d3b36a4 --- /dev/null +++ b/src/wallpyper.py @@ -0,0 +1,15 @@ +#!/usr/bin/python3 + +from wallpyperFuncs import * + +def main(): + if len(sys.argv) == 2 and sys.argv[1] in ("help","--help","-h"): + printHelpInfo() + else: + if len(sys.argv) == 1: setTheme() + elif len(sys.argv) == 2 and sys.argv[1] in ("quit", "stop"): killLastSession(); sys.exit(0) + elif len(sys.argv) == 3 and sys.argv[1] == "set": setTheme(sys.argv[2]) + else: printHelpInfo() + +if __name__ == '__main__': + main() diff --git a/src/wallpyperFuncs.py b/src/wallpyperFuncs.py new file mode 100644 index 0000000..87f0cc8 --- /dev/null +++ b/src/wallpyperFuncs.py @@ -0,0 +1,106 @@ +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 setDinamicWallpaper(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"] == "dinamic": + 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=setDinamicWallpaper, args=(theme["path"],delay,)) + wallpyperThread.start() + + else: + printHelpInfo() + sys.exit(0) \ No newline at end of file