X-Git-Url: https://code.delx.au/bg-scripts/blobdiff_plain/a8d026613301bb315f8f66f50b1b35d8db996744..f6fd0f461b4b1ec453e7f84d0e262e1f34a7907f:/randombg.py diff --git a/randombg.py b/randombg.py index d069b49..00c7d54 100755 --- a/randombg.py +++ b/randombg.py @@ -7,7 +7,12 @@ import asyncore, asynchat, socket import os, os.path, random, sys, time from optparse import OptionParser import logging -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: @@ -18,7 +23,7 @@ try: import asyncsched import wallchanger except ImportError, e: - logging.critical("Missing libraries! Exiting...") + logging.critical("Missing libraries! Exiting...", exc_info=1) sys.exit(1) @@ -35,22 +40,23 @@ class BaseFileList(object): """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') @@ -59,19 +65,24 @@ class BaseFileList(object): 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() @@ -147,29 +158,11 @@ class AllRandomFileList(BaseFileList): def store_cache(self, filename): try: fd = open(filename, 'wb') - pickle.dump(obj = self, file = fd, protocol = 2) + pickle.dump(self, fd, 2) logging.debug("Cache successfully stored") except Exception, e: logging.warning("Storing cache", exc_info=1) - def load_cache(self, filename, rescanPaths = False): - logging.debug('Attempting to load cache from "%s"' % filename) - self.paths.sort() - try: - fd = open(filename, 'rb') - tmp = pickle.load(fd) - if self.paths == tmp.paths: - logging.debug("Path lists match, copying properties") - # Overwrite this object with the other - for attr in ('list', 'imagePointer'): - setattr(self, attr, getattr(tmp, attr)) - else: - logging.debug("Ignoring cache, path lists do not match") - except Exception, e: - logging.warning("Loading cache", exc_info=1) - else: - return True - def get_current_image(self): return self.list[self.imagePointer] @@ -193,6 +186,7 @@ class AllRandomFileList(BaseFileList): 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.""" @@ -235,12 +229,12 @@ class FolderRandomFileList(BaseFileList): 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 logging.debug("Initialising wallchanger") - wallchanger.init(options.background_colour, options.permanent) + wallchanger.init(options.background_colour, options.permanent, options.convert) logging.debug("Initialising file list") if options.all_random: @@ -264,7 +258,10 @@ class Cycler(object): 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) @@ -283,6 +280,7 @@ class Cycler(object): self.task.cancel() self.task = asyncsched.schedule(self.cycle_time, next) logging.debug("Reset timer for %s seconds" % self.cycle_time) + self.filelist.store_cache(self.history_filename) def cmd_reload(self): image = self.filelist.get_current_image() @@ -310,6 +308,9 @@ class Cycler(object): 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) @@ -334,7 +335,6 @@ class Server(asynchat.async_chat): logging.debug('Unknown command received "%s"' % cmd) - class Listener(asyncore.dispatcher): def __init__(self, socket_filename, cycler): asyncore.dispatcher.__init__(self) @@ -383,7 +383,7 @@ def do_client(options, args): 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, @@ -436,13 +436,12 @@ def main(): if options.oneshot: do_oneshot(options, args) - - if os.path.exists(options.socket_filename): - do_client(options, args) else: - do_server(options, args) + if os.path.exists(options.socket_filename): + do_client(options, args) + else: + do_server(options, args) if __name__ == "__main__": main() -