X-Git-Url: https://code.delx.au/bg-scripts/blobdiff_plain/6f5b1de5fb53fa3b63321995709b0b0707cff238..5f9578e04b4023f580643c388cfa7a3f2baa7990:/randombg.py diff --git a/randombg.py b/randombg.py index c20dd40..3f1609c 100755 --- a/randombg.py +++ b/randombg.py @@ -7,7 +7,6 @@ import asyncore, asynchat, socket import os, os.path, random, sys, time from optparse import OptionParser import logging -from logging import debug, info, warning, error, critical logging.basicConfig(format="%(levelname)s: %(message)s") try: import cPickle as pickle @@ -19,7 +18,7 @@ try: import asyncsched import wallchanger except ImportError, e: - critical("Missing libraries! Exiting...") + logging.critical("Missing libraries! Exiting...") sys.exit(1) @@ -73,12 +72,12 @@ class RandomFileList(BaseFileList): def add_path(self, path): self.paths.append(path) - debug('Added path "%s" to the list' % 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 is_empty(self): @@ -93,45 +92,45 @@ class AllRandomFileList(BaseFileList): # Scan the input directory, and then randomize the file list def scan_paths(self): - debug("Scanning paths") + 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) - debug('Added path "%s" to the list' % path) + logging.debug('Added path "%s" to the list' % path) def store_cache(self, filename): try: fd = open(filename, 'wb') pickle.dump(obj = self, file = fd, protocol = 2) - debug("Cache successfully stored") + logging.debug("Cache successfully stored") except Exception, e: - warning("Storing cache: %s" % e) + logging.warning("Storing cache", exc_info=1) def load_cache(self, filename, rescanPaths = False): - debug('Attempting to load cache from "%s"' % filename) + 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: - debug("Path lists match, copying properties") + 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: - debug("Ignoring cache, path lists do not match") + logging.debug("Ignoring cache, path lists do not match") except Exception, e: - warning("Loading cache: %s" % e) + logging.warning("Loading cache", exc_info=1) def get_current_image(self): return self.list[self.imagePointer] @@ -144,13 +143,13 @@ class AllRandomFileList(BaseFileList): 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): @@ -166,23 +165,23 @@ class FolderRandomFileList(BaseFileList): pass def add_path(self, path): - debug('Added path "%s" to the list' % path) + logging.debug('Added path "%s" to the list' % path) for dirpath, dirs, filenames in os.walk(path): - debug('Scanning "%s" for images' % dirpath) + 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 - debug('Adding "%s" to "%s"' % (filenames, dirpath)) + logging.debug('Adding "%s" to "%s"' % (filenames, dirpath)) else: - debug("No images found in '%s'" % dirpath) + 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) + logging.debug('filename: "%s"' % filename) return os.path.join(directory, filename) def is_empty(self): @@ -194,10 +193,10 @@ class Cycler(object): self.cycle_time = options.cycle_time self.history_filename = options.history_filename - debug("Initialising wallchanger") + logging.debug("Initialising wallchanger") wallchanger.init(options.background_colour, options.permanent) - debug("Initialising file list") + logging.debug("Initialising file list") if options.all_random: self.filelist = AllRandomFileList() elif options.folder_random: @@ -209,13 +208,13 @@ class Cycler(object): self.filelist.add_path(path) if self.filelist.load_cache(self.history_filename): - debug("Loaded cache successfully") + 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 @@ -237,7 +236,7 @@ class Cycler(object): 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) def cmd_reload(self): image = self.filelist.get_current_image() @@ -281,13 +280,13 @@ class Server(asynchat.async_chat): 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)