This my first commit

This commit is contained in:
elwld 2022-08-06 15:35:10 +05:00
parent 799ebe31a2
commit 8a93273ffe
4 changed files with 121 additions and 63 deletions

39
func.py
View File

@ -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()

24
main.py
View File

@ -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()

15
src/wallpyper.py Executable file
View File

@ -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()

106
src/wallpyperFuncs.py Normal file
View File

@ -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 <theme> set <theme> 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)