]> code.delx.au - bg-scripts/commitdiff
RandomBG: Reimplemented file list caching
authorJames Bunton <jamesbunton@delx.net.au>
Tue, 1 Jul 2008 16:55:41 +0000 (02:55 +1000)
committerJames Bunton <jamesbunton@delx.net.au>
Tue, 1 Jul 2008 16:55:41 +0000 (02:55 +1000)
 * Support for quitting the server.
 * Made FileList.is_empty() do what you'd expect.
 * Save the cache on exit.

bin/randombg.py

index 99933fea4fd9879b1c532b98181a413a0b79606e..8e57f5452c9ab345991322edf6168a77757a1f89 100755 (executable)
@@ -56,7 +56,7 @@ class BaseFileList(object):
                raise NotImplementedError()
 
        def is_empty(self):
-               return False
+               return True
 
 
 class RandomFileList(BaseFileList):
@@ -82,7 +82,7 @@ class RandomFileList(BaseFileList):
                return self.last_image
        
        def is_empty(self):
-               return len(self.list) > 0
+               return len(self.list) == 0
 
 
 class AllRandomFileList(BaseFileList):
@@ -115,7 +115,7 @@ class AllRandomFileList(BaseFileList):
                        pickle.dump(obj = self, file = fd, protocol = 2)
                        debug("Cache successfully stored")
                except Exception, e:
-                       warning("Exception while storing cache: '%s'" % e)
+                       warning("Storing cache: %s" % e)
 
        def load_cache(self, filename, rescanPaths = False):
                debug('Attempting to load cache from "%s"' % filename)
@@ -131,7 +131,7 @@ class AllRandomFileList(BaseFileList):
                        else:
                                debug("Ignoring cache, path lists do not match")
                except Exception, e:
-                       warning("Exception while loading cache: '%s'" % e)
+                       warning("Loading cache: %s" % e)
 
        def get_current_image(self):
                return self.list[self.imagePointer]
@@ -154,7 +154,7 @@ class AllRandomFileList(BaseFileList):
                return imageName
 
        def is_empty(self):
-               return self.list
+               return len(self.list) == 0
 
 class FolderRandomFileList(BaseFileList):
        """A file list that will pick a file randomly within a directory. Each
@@ -186,39 +186,45 @@ class FolderRandomFileList(BaseFileList):
                return os.path.join(directory, filename)
        
        def is_empty(self):
-               return len(self.directories.values())
+               return len(self.directories.values()) == 0
 
 
 class Cycler(object):
        def init(self, options, paths):
-               self.filelist = self.find_files(options, paths)
-               if not self.filelist.is_empty():
-                       error("No images were found. Exiting...")
-                       sys.exit(1)
-       
-               debug("Initialising wallchanger")
-               wallchanger.init(options.background_colour, options.permanent)
                self.cycle_time = options.cycle_time
+               self.history_filename = options.history_filename
 
-               self.task = None
-               self.cmd_next()
+               debug("Initialising wallchanger")
+               wallchanger.init(options.background_colour, options.permanent)
 
-       def find_files(self, options, paths):
+               debug("Initialising file list")
                if options.all_random:
-                       filelist = AllRandomFileList()
+                       self.filelist = AllRandomFileList()
                elif options.folder_random:
-                       filelist = FolderRandomFileList()
+                       self.filelist = FolderRandomFileList()
                else:
-                       filelist = RandomFileList()
+                       self.filelist = RandomFileList()
 
                for path in paths:
-                       filelist.add_path(path)
+                       self.filelist.add_path(path)
 
-               if filelist.load_cache(options.history_filename):
+               if self.filelist.load_cache(self.history_filename):
                        debug("Loaded cache successfully")
                else:
                        debug("Could not load cache")
-                       filelist.scan_paths()
+                       self.filelist.scan_paths()
+
+               if self.filelist.is_empty():
+                       error("No images were found. Exiting...")
+                       sys.exit(1)
+       
+               self.task = None
+               self.cmd_next()
+       
+       def finish(self):
+               self.filelist.store_cache(self.history_filename)
+
+       def find_files(self, options, paths):
                return filelist
 
        def cmd_reset(self):
@@ -256,6 +262,9 @@ class Cycler(object):
                if self.task is not None:
                        self.task.cancel()
                        self.task = None
+       
+       def cmd_exit(self):
+               asyncsched.exit()
 
 class Server(asynchat.async_chat):
        def __init__(self, cycler, conn, addr):
@@ -297,14 +306,15 @@ class Listener(asyncore.dispatcher):
 
 def do_server(options, paths):
        try:
+               cycler = Cycler()
+               listener = Listener(options.socket_filename, cycler)
+               # Initialisation of Cycler delayed so we grab the socket quickly
+               cycler.init(options, paths)
                try:
-                       cycler = Cycler()
-                       listener = Listener(options.socket_filename, cycler)
-                       # Initialisation of Cycler delayed so we grab the socket quickly
-                       cycler.init(options, paths)
                        asyncsched.loop()
                except KeyboardInterrupt:
                        print
+               cycler.finish()
        finally:
                # Make sure that the socket is cleaned up
                try: