]> code.delx.au - bg-scripts/commitdiff
Various hacks
authorJames Bunton <jamesbunton@delx.net.au>
Tue, 28 Feb 2012 22:31:08 +0000 (09:31 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Tue, 28 Feb 2012 22:31:08 +0000 (09:31 +1100)
 * Changed historyfile to cachefile
 * Added --server to force server mode, otherwise client mode is assumed
 * If in server mode, socket will be forcibly removed if it exists

randombg.py

index 9a587bb2e5b2270d36e72d965380f504160e14f5..4727b20b7d85604f255a7747b1e748d58c40e072 100755 (executable)
@@ -1,10 +1,15 @@
 #!/usr/bin/env python
 
-VERSION = "2.0"
+VERSION = "2.1"
 
 
-import asyncore, asynchat, socket
-import os, os.path, random, sys, time
+import asyncore
+import asynchat
+import socket
+import os
+import random
+import sys
+import time
 from optparse import OptionParser
 import logging
 try:
@@ -231,7 +236,7 @@ class FolderRandomFileList(BaseFileList):
 class Cycler(object):
        def init(self, options, paths, oneshot=False):
                self.cycle_time = options.cycle_time
-               self.history_filename = options.history_filename
+               self.cache_filename = options.cache_filename
 
                logging.debug("Initialising wallchanger")
                wallchanger.init(options.background_colour, options.permanent, options.convert)
@@ -247,7 +252,7 @@ class Cycler(object):
                for path in paths:
                        self.filelist.add_path(path)
 
-               if self.filelist.load_cache(self.history_filename):
+               if self.filelist.load_cache(self.cache_filename):
                        logging.debug("Loaded cache successfully")
                else:
                        logging.debug("Could not load cache")
@@ -264,7 +269,7 @@ class Cycler(object):
                        self.cmd_reload()
        
        def finish(self):
-               self.filelist.store_cache(self.history_filename)
+               self.filelist.store_cache(self.cache_filename)
 
        def find_files(self, options, paths):
                return filelist
@@ -280,7 +285,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)
+               self.filelist.store_cache(self.cache_filename)
        
        def cmd_reload(self):
                image = self.filelist.get_current_image()
@@ -349,6 +354,10 @@ class Listener(asyncore.dispatcher):
                asyncore.dispatcher.__init__(self)
                self.cycler = cycler
                self.create_socket(socket.AF_UNIX, socket.SOCK_STREAM)
+               try:
+                       os.unlink(socket_filename)
+               except OSError:
+                       pass
                self.bind(socket_filename)
                self.listen(2) # Backlog = 2
        
@@ -429,9 +438,12 @@ def build_parser():
        parser.add_option("--socket",
                action="store", type="string", dest="socket_filename", default=os.path.expanduser('~/.randombg_socket'),
                help="Location of the command/control socket.")
-       parser.add_option("--history-file",
-               action="store", type="string", dest="history_filename", default=os.path.expanduser('~/.randombg_historyfile'),
+       parser.add_option("--cache-file",
+               action="store", type="string", dest="cache_filename", default=os.path.expanduser('~/.randombg_cache'),
                help="Stores the location of the last image to be loaded.")
+       parser.add_option("--server",
+               action="store_true", dest="server", default=False,
+               help="Run in server mode to listen for clients.")
        return parser
 
 def main():
@@ -443,13 +455,19 @@ def main():
        elif options.verbose >= 2:
                logging.getLogger().setLevel(logging.DEBUG)
        
+       if options.server:
+               do_server(options, args)
+               return
+
        if options.oneshot:
                do_oneshot(options, args)
-       else:
-               if os.path.exists(options.socket_filename):
-                       do_client(options, args)
-               else:
-                       do_server(options, args)
+               return
+
+       try:
+               do_client(options, args)
+               return
+       except Exception, e:
+               print >>sys.stderr, "Failed to connect to server:", e
 
 
 if __name__ == "__main__":