#!/usr/bin/env python
-VERSION = "2.0"
+VERSION = "2.1"
-import asyncore, asynchat, socket
-import os, os.path, random, sys, time
+import asyncore
+import asynchat
+import socket
+import os
+import random
+import sys
+import time
from optparse import OptionParser
import logging
-from logging import debug, info, warning, error, critical
-logging.basicConfig(format="%(levelname)s: %(message)s")
+try:
+ logging.basicConfig(format="%(levelname)s: %(message)s")
+except TypeError:
+# Python 2.3's logging.basicConfig does not support parameters
+ logging.basicConfig()
+
try:
import cPickle as pickle
except ImportError:
import asyncsched
import wallchanger
except ImportError, e:
- critical("Missing libraries! Exiting...")
+ logging.critical("Missing libraries! Exiting...", exc_info=1)
sys.exit(1)
"""Base file list implementation"""
def __init__(self):
self.paths = []
+ self.favourites = []
def add_path(self, path):
self.paths.append(path)
def store_cache(self, filename):
try:
- debug("Attempting to store cache")
+ logging.debug("Attempting to store cache")
fd = open(filename, 'wb')
- pickle.dump(obj = self, file = fd, protocol = 2)
- debug("Cache successfully stored")
+ pickle.dump(self, fd, 2)
+ logging.debug("Cache successfully stored")
except Exception, e:
warning("Storing cache: %s" % e)
def load_cache(self, filename):
try:
- debug("Attempting to load cache from: %s" % filename)
+ logging.debug("Attempting to load cache from: %s" % filename)
self.paths.sort()
fd = open(filename, 'rb')
if tmp.__class__ != self.__class__:
raise ValueError("Using different file list type")
- self.paths.sort()
- if self.paths != getattr(tmp, "paths"):
- raise ValueError("Path list changed")
+ tmp.paths.sort()
+ if self.paths != tmp.paths:
+ raise ValueError, "Path list changed"
+ # Overwrite this object with the other
for attr, value in tmp.__dict__.items():
setattr(self, attr, value)
return True
except Exception, e:
- warning("Loading cache: %s" % e)
+ logging.warning("Loading cache: %s" % e)
return False
+ def add_to_favourites(self):
+ '''Adds the current image to the list of favourites'''
+ self.favourites.append(self.get_current_image())
+
def scan_paths(self):
raise NotImplementedError()
for filename in filter_images(filenames):
self.list.append(os.path.join(dirpath, filename))
+ def add_path(self, path):
+ self.paths.append(path)
+ logging.debug('Added path "%s" to the list' % path)
+
def get_next_image(self):
n = random.randint(0, len(self.list)-1)
self.last_image = self.list[n]
- debug("Picked file '%s' from list" % self.last_image)
+ logging.debug("Picked file '%s' from list" % self.last_image)
return self.last_image
def get_current_image(self):
# Scan the input directory, and then randomize the file list
def scan_paths(self):
+ logging.debug("Scanning paths")
+
self.list = []
for path in self.paths:
- debug('Scanning "%s"' % path)
+ logging.debug('Scanning "%s"' % path)
for dirpath, dirsnames, filenames in os.walk(path):
for filename in filter_images(filenames):
- debug('Adding file "%s"' % filename)
+ logging.debug('Adding file "%s"' % filename)
self.list.append(os.path.join(dirpath, filename))
random.shuffle(self.list)
+ def add_path(self, path):
+ self.paths.append(path)
+ logging.debug('Added path "%s" to the list' % path)
+
+ def store_cache(self, filename):
+ try:
+ fd = open(filename, 'wb')
+ pickle.dump(self, fd, 2)
+ logging.debug("Cache successfully stored")
+ except Exception, e:
+ logging.warning("Storing cache", exc_info=1)
+
def get_current_image(self):
return self.list[self.imagePointer]
def get_next_image(self):
self.imagePointer = self.__inc_in_range(self.imagePointer)
imageName = self.list[self.imagePointer]
- debug("Picked file '%s' (pointer=%d) from list" % (imageName, self.imagePointer))
+ logging.debug("Picked file '%s' (pointer=%d) from list" % (imageName, self.imagePointer))
return imageName
def get_prev_image(self):
self.imagePointer = self.__inc_in_range(self.imagePointer, amount=-1)
imageName = self.list[self.imagePointer]
- debug("Picked file '%s' (pointer=%d) from list" % (imageName, self.imagePointer))
+ logging.debug("Picked file '%s' (pointer=%d) from list" % (imageName, self.imagePointer))
return imageName
def is_empty(self):
return len(self.list) == 0
+
class FolderRandomFileList(BaseFileList):
"""A file list that will pick a file randomly within a directory. Each
directory has the same chance of being chosen."""
self.last_image = None
def scan_paths(self):
- for path in self.paths:
- for dirpath, dirs, filenames in os.walk(path):
- debug('Scanning "%s" for images' % dirpath)
- if self.directories.has_key(dirpath):
- continue
- filenames = list(filter_images(filenames))
- if len(filenames):
- self.directories[dirpath] = filenames
- debug('Adding "%s" to "%s"' % (filenames, dirpath))
- else:
- debug("No images found in '%s'" % dirpath)
-
+ pass
+
+ def add_path(self, path):
+ logging.debug('Added path "%s" to the list' % path)
+ for dirpath, dirs, filenames in os.walk(path):
+ logging.debug('Scanning "%s" for images' % dirpath)
+ if self.directories.has_key(dirpath):
+ continue
+ filenames = list(filter_images(filenames))
+ if len(filenames):
+ self.directories[dirpath] = filenames
+ logging.debug('Adding "%s" to "%s"' % (filenames, dirpath))
+ else:
+ logging.debug("No images found in '%s'" % dirpath)
+
def get_next_image(self):
directory = random.choice(self.directories.keys())
- debug('directory: "%s"' % directory)
+ logging.debug('directory: "%s"' % directory)
filename = random.choice(self.directories[directory])
- debug('filename: "%s"' % filename)
- self.last_image = os.path.join(directory, filename)
- return self.last_image
+ logging.debug('filename: "%s"' % filename)
+ return os.path.join(directory, filename)
def get_current_image(self):
if self.last_image:
class Cycler(object):
- def init(self, options, paths):
+ def init(self, options, paths, oneshot=False):
self.cycle_time = options.cycle_time
- self.history_filename = options.history_filename
+ self.cache_filename = options.cache_filename
- debug("Initialising wallchanger")
- wallchanger.init(options.background_colour, options.permanent)
+ logging.debug("Initialising wallchanger")
+ wallchanger.init(options.background_colour, options.permanent, options.convert)
- debug("Initialising file list")
+ logging.debug("Initialising file list")
if options.all_random:
self.filelist = AllRandomFileList()
elif options.folder_random:
for path in paths:
self.filelist.add_path(path)
- if self.filelist.load_cache(self.history_filename):
- debug("Loaded cache successfully")
+ if self.filelist.load_cache(self.cache_filename):
+ logging.debug("Loaded cache successfully")
else:
- debug("Could not load cache")
+ logging.debug("Could not load cache")
self.filelist.scan_paths()
if self.filelist.is_empty():
- error("No images were found. Exiting...")
+ logging.error("No images were found. Exiting...")
sys.exit(1)
self.task = None
- self.cmd_reload()
+ if oneshot:
+ self.cmd_next()
+ else:
+ self.cmd_reload()
def finish(self):
- self.filelist.store_cache(self.history_filename)
+ self.filelist.store_cache(self.cache_filename)
def find_files(self, options, paths):
return filelist
if self.task is not None:
self.task.cancel()
self.task = asyncsched.schedule(self.cycle_time, next)
- debug("Reset timer for %s seconds" % self.cycle_time)
+ logging.debug("Reset timer for %s seconds" % self.cycle_time)
+ self.filelist.store_cache(self.cache_filename)
def cmd_reload(self):
image = self.filelist.get_current_image()
def cmd_exit(self):
asyncsched.exit()
+ def cmd_favourite(self):
+ self.filelist.add_to_favourites()
+
class Server(asynchat.async_chat):
- def __init__(self, cycler, conn, addr):
- asynchat.async_chat.__init__(self, conn=conn)
+ def __init__(self, cycler, sock):
+ asynchat.async_chat.__init__(self, sock)
self.cycler = cycler
self.ibuffer = []
self.set_terminator("\n")
self.ibuffer = []
prefix, cmd = line.split(None, 1)
if prefix != "cmd":
- debug('Bad line received "%s"' % line)
+ logging.debug('Bad line received "%s"' % line)
return
if hasattr(self.cycler, "cmd_" + cmd):
- debug('Executing command "%s"' % cmd)
+ logging.debug('Executing command "%s"' % cmd)
getattr(self.cycler, "cmd_" + cmd)()
else:
- debug('Unknown command received "%s"' % cmd)
+ logging.debug('Unknown command received "%s"' % cmd)
+class SockHackWrap(object):
+ def __init__(self, sock, addr):
+ self.__sock = sock
+ self.__addr = addr
+ def getpeername(self):
+ return self.__addr
+ def __getattr__(self, key):
+ return getattr(self.__sock, key)
class Listener(asyncore.dispatcher):
def __init__(self, socket_filename, cycler):
self.listen(2) # Backlog = 2
def handle_accept(self):
- conn, addr = self.accept()
- Server(self.cycler, conn, addr)
+ sock, addr = self.accept()
+ Server(self.cycler, SockHackWrap(sock, addr))
def writable(self):
return False
def do_server(options, paths):
+ try:
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ sock.connect(options.socket_filename)
+ print >>sys.stderr, "Server is already running! Will do nothing"
+ return
+ except:
+ pass
+
+ try:
+ os.unlink(options.socket_filename)
+ except OSError:
+ pass
+
try:
cycler = Cycler()
listener = Listener(options.socket_filename, cycler)
# Make sure that the socket is cleaned up
try:
os.unlink(options.socket_filename)
- except:
+ except OSError:
pass
def do_client(options, args):
sock = sock.makefile()
for i, cmd in enumerate(args):
sock.write("cmd %s\n" % cmd)
- if i+1 != len(args):
+ if i < len(args) - 1:
time.sleep(options.cycle_time)
sock.close()
def do_oneshot(options, paths):
cycler = Cycler()
- cycler.init(options, paths)
+ cycler.init(options, paths, oneshot=True)
def build_parser():
parser = OptionParser(version="%prog " + VERSION,
parser.add_option("--socket",
action="store", type="string", dest="socket_filename", default=os.path.expanduser('~/.randombg_socket'),
help="Location of the command/control socket.")
- parser.add_option("--history-file",
- action="store", type="string", dest="history_filename", default=os.path.expanduser('~/.randombg_historyfile'),
+ parser.add_option("--cache-file",
+ action="store", type="string", dest="cache_filename", default=os.path.expanduser('~/.randombg_cache'),
help="Stores the location of the last image to be loaded.")
+ parser.add_option("--server",
+ action="store_true", dest="server", default=False,
+ help="Run in server mode to listen for clients.")
return parser
def main():
elif options.verbose >= 2:
logging.getLogger().setLevel(logging.DEBUG)
+ if options.server:
+ do_server(options, args)
+ return
+
if options.oneshot:
do_oneshot(options, args)
+ return
- if os.path.exists(options.socket_filename):
+ try:
do_client(options, args)
- else:
- do_server(options, args)
+ return
+ except Exception, e:
+ print >>sys.stderr, "Failed to connect to server:", e
if __name__ == "__main__":
main()
-