+ def is_empty(self):
+ return len(self.list) == 0
+
+
+class AllRandomFileList(BaseFileList):
+ def __init__(self):
+ 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)
+ for dirpath, dirsnames, filenames in os.walk(path):
+ for filename in filter_images(filenames):
+ debug('Adding file "%s"' % filename)
+ self.list.append(os.path.join(dirpath, filename))
+
+ 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]
+
+ def __inc_in_range(self, n, amount = 1, rangeMax = None, rangeMin = 0):
+ if rangeMax == None: rangeMax = len(self.list)
+ assert rangeMax > 0
+ return (n + amount) % rangeMax
+
+ def get_next_image(self):
+ self.imagePointer = self.__inc_in_range(self.imagePointer)
+ imageName = self.list[self.imagePointer]
+ debug("Picked file '%s' (pointer=%d) from list" % (imageName, self.imagePointer))
+ return imageName
+
+ def get_prev_image(self):
+ self.imagePointer = self.__inc_in_range(self.imagePointer, amount=-1)
+ imageName = self.list[self.imagePointer]
+ debug("Picked file '%s' (pointer=%d) from list" % (imageName, self.imagePointer))
+ return imageName
+
+ 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."""
+ def __init__(self):
+ self.directories = {}
+
+ 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)
+
+ 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)
+
+ def is_empty(self):
+ return len(self.directories.values()) == 0
+
+
+class Cycler(object):
+ def init(self, options, paths):