]> 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 02:54:00 +0000 (12:54 +1000)
committerGreg Darke <greg@tsukasa.net.au>
Wed, 16 Jul 2008 02:54:00 +0000 (12:54 +1000)
randombg.py
wallchanger.py

index 510d786f3f38896eb7722b9b895bb6c9024c96fe..bb4050c9f95fa2c21e6349a8b5dd6b71a321ee90 100755 (executable)
@@ -34,17 +34,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, 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):
+               try:
+                       debug("Attempting to load cache from: %s" % filename)
+                       self.paths.sort()
 
-       def store_cache(self, path):
-               pass
+                       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 load_cache(self, filename, rescanPaths = False):
-               pass
+       def scan_paths(self):
+               raise NotImplementedError()
 
        def get_next_image(self):
                raise NotImplementedError()
@@ -61,8 +91,8 @@ class BaseFileList(object):
 
 class RandomFileList(BaseFileList):
        def __init__(self):
+               super(RandomFileList, self).__init__()
                self.list = []
-               self.paths = []
                self.last_image = None
 
        def scan_paths(self):
@@ -71,30 +101,30 @@ class RandomFileList(BaseFileList):
                                for filename in filter_images(filenames):
                                        self.list.append(os.path.join(dirpath, filename))
 
-       def add_path(self, path):
-               self.paths.append(path)
-               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)
                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):
-               debug("Scanning paths")
-
                self.list = []
                for path in self.paths:
                        debug('Scanning "%s"' % path)
@@ -105,34 +135,6 @@ class AllRandomFileList(BaseFileList):
 
                random.shuffle(self.list)
 
-       def add_path(self, path):
-               self.paths.append(path)
-               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)
-                       debug("Cache successfully stored")
-               except Exception, e:
-                       warning("Storing cache: %s" % e)
-
-       def load_cache(self, filename, rescanPaths = False):
-               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:
-                               debug("Path lists match, copying properties")
-                               # Overwrite this object with the other
-                               for attr in ('list', 'imagePointer'):
-                                       setattr(self, attr, getattr(tmp, attr))
-                       else:
-                               debug("Ignoring cache, path lists do not match")
-               except Exception, e:
-                       warning("Loading cache: %s" % e)
-
        def get_current_image(self):
                return self.list[self.imagePointer]
        
@@ -160,31 +162,37 @@ 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):
-               pass
-       
-       def add_path(self, path):
-               debug('Added path "%s" to the list' % path)
-               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)
-       
+               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)
+
        def get_next_image(self):
                directory = random.choice(self.directories.keys())
                debug('directory: "%s"' % directory)
                filename = random.choice(self.directories[directory])
                debug('filename: "%s"' % filename)
-               return os.path.join(directory, filename)
+               self.last_image = os.path.join(directory, filename)
+               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.directories.values()) == 0
 
@@ -256,7 +264,6 @@ class Cycler(object):
        
        def cmd_rescan(self):
                self.filelist.scan_paths()
-               self.cmd_next()
        
        def cmd_pause(self):
                if self.task is not None:
index b20c0683b84404326f3afd5f2b1fdc27d639a74b..16e7230781a5134154324bcd02d2b8070e759bba 100755 (executable)
@@ -109,7 +109,6 @@ class OSXChanger(BaseChanger):
 
        def __init__(self, *args, **kwargs):
                BaseChanger.__init__(self, *args, **kwargs)
-               self.fix_desktop_plist()
 
        def remove_old_image_cache(self):
                """Cleans up any old temp images"""
@@ -153,6 +152,7 @@ class OSXChanger(BaseChanger):
                        debug('Could not import the Foundation module, you may have problems with dual screens')
 
        def set_image(self, filename):
+               self.fix_desktop_plist()
                if self.convert:
                        filename, ret = self.convert_image_format(filename)
                        if not ret: