WallPyPer/src/wallpyperFuncs.py

163 lines
3.7 KiB
Python

import pytomlpp
import os
import random
import sys
from time import sleep
from threading import Thread
from datetime import datetime
def printHelpInfo():
print("""Usage: wallpyper [command]
Commands:
quit, stop shutdown wallpyper
set <theme> set <theme> theme
help, --help, -h print this information""")
sys.exit(0)
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 setNextWallpaper(path):
if 'wallpaperIndex' not in globals():
globals()['wallpaperIndex'] = -1
global wallpaperIndex
wallpaperIndex+=1
wallpaperName = path + sorted(os.listdir(path))[wallpaperIndex%len(os.listdir(path))]
setWallpaper(wallpaperName)
def setTheme(themeName = None, nightMode = False):
config = getConfig()
if nightMode:
theme = config[themeName]["night"]
else:
theme = config[themeName]
dynamic = False
random = False
if "dynamic" in theme and theme["dynamic"]:
dynamic = True
if "random" in theme and theme["random"]:
random = True
if 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
if 'threadIsAlive' not in globals():
globals()['threadIsAlive'] = True
global threadIsAlive
while threadIsAlive:
if random:
setRandomWallpaper(theme["path"])
else:
setNextWallpaper(theme["path"])
sleep(delay)
else:
if random:
setRandomWallpaper(theme["path"])
else:
setWallpaper(theme["path"])
def getThemes():
config = getConfig()
themes = []
for e in config:
if type(config[e]) == dict:
themes.append(e)
return themes
def timeDiff(time1, time2):
time1 = list(map(int,time1.split(":")))
time2 = list(map(int,time2.split(":")))
seconds = 0
diff = 0
while (time1[2]+diff)%60 != time2[2]:
diff+=1
seconds+=diff
if (diff + time1[2] > 60): time1[1]+=1
diff = 0
while (time1[1]+diff)%60 != time2[1]:
diff+=1
seconds+=diff*60
if (diff + time1[1] > 60): time1[0]+=1
diff = 0
while (time1[0]+diff)%24 != time2[0]:
diff+=1
seconds+=diff*60*60
return seconds