X-Git-Url: https://code.delx.au/bg-scripts/blobdiff_plain/5f9578e04b4023f580643c388cfa7a3f2baa7990..0769d8f2a3ce12b2023335b5c3c9bbbd76b7d531:/randombg.py diff --git a/randombg.py b/randombg.py index 3f1609c..bb2936b 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) @@ -33,17 +38,47 @@ def filter_images(filenames): class BaseFileList(object): """Base file list implementation""" - def scan_paths(self): - raise NotImplementedError() + def __init__(self): + self.paths = [] def add_path(self, path): - raise NotImplementedError() + self.paths.append(path) - def store_cache(self, path): - pass + def store_cache(self, filename): + try: + logging.debug("Attempting to store cache") + fd = open(filename, 'wb') + pickle.dump(obj = self, file = fd, protocol = 2) + logging.debug("Cache successfully stored") + except Exception, e: + warning("Storing cache: %s" % e) - def load_cache(self, filename, rescanPaths = False): - pass + def load_cache(self, filename): + try: + logging.debug("Attempting to load cache from: %s" % filename) + self.paths.sort() + + fd = open(filename, 'rb') + tmp = pickle.load(fd) + + 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") + + for attr, value in tmp.__dict__.items(): + setattr(self, attr, value) + + return True + + except Exception, e: + logging.warning("Loading cache: %s" % e) + return False + + def scan_paths(self): + raise NotImplementedError() def get_next_image(self): raise NotImplementedError() @@ -60,8 +95,8 @@ class BaseFileList(object): class RandomFileList(BaseFileList): def __init__(self): + super(RandomFileList, self).__init__() self.list = [] - self.paths = [] self.last_image = None def scan_paths(self): @@ -80,14 +115,20 @@ class RandomFileList(BaseFileList): logging.debug("Picked file '%s' from list" % self.last_image) return self.last_image + def get_current_image(self): + if self.last_image: + return self.last_image + else: + return self.get_next_image() + def is_empty(self): return len(self.list) == 0 class AllRandomFileList(BaseFileList): def __init__(self): + super(AllRandomFileList, self).__init__() self.list = None - self.paths = [] self.imagePointer = 0 # Scan the input directory, and then randomize the file list @@ -111,7 +152,7 @@ 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) @@ -131,6 +172,8 @@ class AllRandomFileList(BaseFileList): 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] @@ -159,7 +202,9 @@ class FolderRandomFileList(BaseFileList): """A file list that will pick a file randomly within a directory. Each directory has the same chance of being chosen.""" def __init__(self): + super(FolderRandomFileList, self).__init__() self.directories = {} + self.last_image = None def scan_paths(self): pass @@ -184,6 +229,12 @@ class FolderRandomFileList(BaseFileList): logging.debug('filename: "%s"' % filename) return os.path.join(directory, filename) + def get_current_image(self): + if self.last_image: + return self.last_image + else: + return self.get_next_image() + def is_empty(self): return len(self.directories.values()) == 0 @@ -255,7 +306,6 @@ class Cycler(object): def cmd_rescan(self): self.filelist.scan_paths() - self.cmd_next() def cmd_pause(self): if self.task is not None: