]> code.delx.au - bg-scripts/commitdiff
Automated merge with ssh://hg@kagami.tsukasa.net.au/bg_scripts/
authorGreg Darke <greg@tsukasa.net.au>
Wed, 16 Jul 2008 06:59:23 +0000 (16:59 +1000)
committerGreg Darke <greg@tsukasa.net.au>
Wed, 16 Jul 2008 06:59:23 +0000 (16:59 +1000)
1  2 
randombg.py

diff --combined randombg.py
index e58875ade1a623c5563a49655d3e1699b92c61ae,bb4050c9f95fa2c21e6349a8b5dd6b71a321ee90..d069b4983c990b358e1639af2de4d01d7fe33d6f
@@@ -7,6 -7,7 +7,6 @@@ import asyncore, asynchat, socke
  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
@@@ -18,7 -19,7 +18,7 @@@ try
        import asyncsched
        import wallchanger
  except ImportError, e:
 -      critical("Missing libraries! Exiting...")
 +      logging.critical("Missing libraries! Exiting...")
        sys.exit(1)
  
  
@@@ -33,17 -34,47 +33,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:
+                       debug("Attempting to store cache")
+                       fd = open(filename, 'wb')
+                       pickle.dump(obj = self, file = fd, protocol = 2)
+                       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:
+                       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:
+                       warning("Loading cache: %s" % e)
+                       return False
+       def scan_paths(self):
+               raise NotImplementedError()
  
        def get_next_image(self):
                raise NotImplementedError()
@@@ -60,8 -91,8 +90,8 @@@
  
  class RandomFileList(BaseFileList):
        def __init__(self):
+               super(RandomFileList, self).__init__()
                self.list = []
-               self.paths = []
                self.last_image = None
  
        def scan_paths(self):
                                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):
+               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
        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(obj = self, file = fd, protocol = 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]
        
        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):
@@@ -161,31 -162,37 +197,39 @@@ 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):
 -              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:
+                       return self.last_image
+               else:
+                       return self.get_next_image()
        def is_empty(self):
                return len(self.directories.values()) == 0
  
@@@ -195,10 -202,10 +239,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:
                        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
                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()
        
        def cmd_rescan(self):
                self.filelist.scan_paths()
-               self.cmd_next()
        
        def cmd_pause(self):
                if self.task is not None:
@@@ -282,13 -288,13 +325,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)
  
  
  
@@@ -334,7 -340,7 +377,7 @@@ 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()