From 8b66b45bf06cbabfb6962caad57d2211d432ecd8 Mon Sep 17 00:00:00 2001 From: James Bunton Date: Wed, 2 Jul 2008 02:55:41 +1000 Subject: [PATCH] RandomBG: Reimplemented file list caching * Support for quitting the server. * Made FileList.is_empty() do what you'd expect. * Save the cache on exit. --- bin/randombg.py | 62 ++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/bin/randombg.py b/bin/randombg.py index 99933fe..8e57f54 100755 --- a/bin/randombg.py +++ b/bin/randombg.py @@ -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: -- 2.39.2