Kleiner Daemon für Slideshow
Verfasst: Montag 9. Juni 2008, 21:37
Hallo, bin neu hier und stelle gleich mal ein kleines Tool vor welches ich zusammengeschustert habe.
Es sucht nach Bilddateien und wechselt in einem bestimmten Abstand dann das Hintergrundbild.
Ich habe das Script auf meinem eee-pc mit openbox laufen, funktioniert sehr gut 
Verbesserungsvorschläge und Meinungen sind willkommen!
Es sucht nach Bilddateien und wechselt in einem bestimmten Abstand dann das Hintergrundbild.
Code: Alles auswählen
#! /usr/bin/python
"""bgcd.py searches for jpg-files in a path and uses the tool feh to set it as background wallpaper.
Written by evilmonkey (there-is-an@evil-monkey-in-my-closet.com).
Usage: bgcd.py (start|stop)
You need the tool 'feh' to run bgcd.py
example: (for use with openbox):
add the following line to your ~/.config/openbox/autostart.sh if bgcd.py is in your PATH
(bgcd.py is in /usr/bin for example)
bgcd.py start &
If you don't have a configfile (~./bgcd_config), bgcd.py automatically creates one for you. The first line is the path to your wallpapers, the second line the seconds until bgcd.py will switch to a new wallpaper. To stop bgcd, just run 'bgcd.py stop'
A logfile is created in /var/tmp/bgcd.log"""
__author__ = "there-is-an@evil-monkey-in-my-closet.com"
__copyright__ = "GPL"
__version__ = "0.7"
# Import libraries
import time
import os
import random
import sys
logfile="/var/tmp/bgcd.log"
configfile=os.getenv("HOME")+"/.bgcd_config"
support=[".jpg",".gif",".png",".pnm"]
example_config_text=os.getenv("HOME")+"/eee-wallpaper\n600"
pidfile="/tmp/bgcd.pid"
def log(text):
"""log(text) writes 'text' to the configfile 'configfile'"""
try:
LOGFILE=open(logfile,'a')
print >> LOGFILE,text
LOGFILE.close()
except:
print "Error while opening file for log"
def remove_log():
"""Removes the old log file"""
try:
os.remove(logfile)
except:
print "Could not remove old logfile"
def create_config_file():
"""This function creates an example config file ~/.bgcd_config"""
try:
CONFIGFILE=open(configfile,'w')
print >> CONFIGFILE,example_config_text
CONFIGFILE.close()
except:
log ("Error writing config file. Exit now...")
os._exit(0)
def start():
os.chdir('/')
(sys.stdin,sys.stdout,sys.stderr) = [ open('/dev/null',m) for m in ('r','a','a')]
# Create a fork of the process to run as daemon
pid = os.fork()
if pid:
# writting process ID to file
PIDFILE = open(pidfile,'w')
print >>PIDFILE,pid
PIDFILE.close()
sys.exit(0)
os.setsid()
os.umask(0)
# Remove old log-file
remove_log()
# Read config-file
try:
CONFIGFILE=open(configfile,'r')
except:
print "No config-file found, creating example config-file"
create_config_file()
CONFIGFILE=open(configfile,'r')
path=CONFIGFILE.readline()
tts=CONFIGFILE.readline()
path=path.strip()
tts=float(tts.strip())
# Check if path exits
if os.path.exists(path) == False:
log (path + " not found! Exit...")
os._exit(0)
if path.endswith("/") == False:
path=path+"/"
dl=[]
log ("Searching in:")
log (path)
log ("found images:")
for dat in os.listdir(path):
if os.path.isfile(path+dat):
for suffix in support:
if dat.endswith(suffix):
dl.append(path+dat)
log (dat)
number=len(dl)
if number==0:
log ("No files found! Exit...")
os._exit(0)
error=False
old=-1
while not error:
try:
i=random.randrange(0, number)
if i==old:
i=i+1
if i>=len(dl):
i=0
old=i
log ("switching to :")
log (dl[i])
os.system("feh --bg-center "+dl[i])
time.sleep(tts)
except:
error=True
log ("ERROR........")
def stop():
try:
PIDFILE = open(pidfile,'r')
pid=PIDFILE.read()
os.kill(int(pid.strip()),15)
log("Terminated by bgcd.py stop")
except:
print "Could not stop process"
def usage():
print "Usage: bgcd.py (start|stop)"
def main():
if len(sys.argv)<2:
sys.exitfunc=usage
sys.exit()
if sys.argv[1]=="start":
start()
else:
if sys.argv[1]=="stop":
stop()
else:
sys.exitfunc=usage
sys.exit()
if __name__ == "__main__":
main()

Verbesserungsvorschläge und Meinungen sind willkommen!