]> code.delx.au - bg-scripts/blobdiff - randombg.py
Hack to make it work with Python 2.6
[bg-scripts] / randombg.py
index 0bc4b7973610495f8ac665bc82c11a4d21b72406..9a587bb2e5b2270d36e72d965380f504160e14f5 100755 (executable)
@@ -65,10 +65,11 @@ 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)
 
@@ -162,24 +163,6 @@ class AllRandomFileList(BaseFileList):
                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', 'favourites'):
-                                       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]
        
@@ -203,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."""
@@ -245,7 +229,7 @@ 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
 
@@ -274,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)
@@ -293,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()
@@ -324,8 +312,8 @@ class Cycler(object):
                self.filelist.add_to_favourites()
 
 class Server(asynchat.async_chat):
-       def __init__(self, cycler, conn, addr):
-               asynchat.async_chat.__init__(self, conn=conn)
+       def __init__(self, cycler, sock):
+               asynchat.async_chat.__init__(self, sock)
                self.cycler = cycler
                self.ibuffer = []
                self.set_terminator("\n")
@@ -347,6 +335,14 @@ class Server(asynchat.async_chat):
                        logging.debug('Unknown command received "%s"' % cmd)
 
 
+class SockHackWrap(object):
+       def __init__(self, sock, addr):
+               self.__sock = sock
+               self.__addr = addr
+       def getpeername(self):
+               return self.__addr
+       def __getattr__(self, key):
+               return getattr(self.__sock, key)
 
 class Listener(asyncore.dispatcher):
        def __init__(self, socket_filename, cycler):
@@ -357,8 +353,8 @@ class Listener(asyncore.dispatcher):
                self.listen(2) # Backlog = 2
        
        def handle_accept(self):
-               conn, addr = self.accept()
-               Server(self.cycler, conn, addr)
+               sock, addr = self.accept()
+               Server(self.cycler, SockHackWrap(sock, addr))
        
        def writable(self):
                return False
@@ -396,7 +392,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, 
@@ -449,13 +445,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()
-