From 8bd9d1519eee62660c395333a764a5d7443b85e2 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 6 May 2011 22:48:09 -0400 Subject: LIl things. --- scanner/CachePath.py | 2 +- scanner/PhotoAlbum.py | 20 ++++++++++++++++++-- scanner/TreeWalker.py | 33 ++++++++++++++++----------------- 3 files changed, 35 insertions(+), 20 deletions(-) (limited to 'scanner') diff --git a/scanner/CachePath.py b/scanner/CachePath.py index 0d3032b..89fc20e 100644 --- a/scanner/CachePath.py +++ b/scanner/CachePath.py @@ -14,7 +14,7 @@ def trim_base_custom(path, base): def trim_base(path): return trim_base_custom(path, trim_base.base) def cache_base(path): - path = trim_base(path).replace('/', '-').replace(' ', '_').replace('(', '').replace(')', '').replace('#', '').replace('[', '').replace(']', '').replace('"', '').replace("'", '').replace('_-_', '-').lower() + path = trim_base(path).replace('/', '-').replace(' ', '_').replace('(', '').replace('&', '').replace(',', '').replace(')', '').replace('#', '').replace('[', '').replace(']', '').replace('"', '').replace("'", '').replace('_-_', '-').lower() while path.find("--") != -1: path = path.replace("--", "-") while path.find("__") != -1: diff --git a/scanner/PhotoAlbum.py b/scanner/PhotoAlbum.py index 18cd425..50216c9 100644 --- a/scanner/PhotoAlbum.py +++ b/scanner/PhotoAlbum.py @@ -52,6 +52,17 @@ class Album(object): if not self._albums_sorted: self._albums.sort() self._albums_sorted = True + @property + def empty(self): + if len(self._photos) != 0: + return False + if len(self._albums) == 0: + return True + for album in self._albums: + if not album.empty: + return False + return True + def cache(self, base_dir): self._sort() fp = open(os.path.join(base_dir, self.cache_path), 'w') @@ -75,10 +86,15 @@ class Album(object): return album def to_dict(self, cripple=True): self._sort() + subalbums = [] if cripple: - subalbums = [ { "path": trim_base_custom(sub.path, self._path), "date": sub.date } for sub in self._albums ] + for sub in self._albums: + if not sub.empty: + subalbums.append({ "path": trim_base_custom(sub.path, self._path), "date": sub.date }) else: - subalbums = self._albums + for sub in self._albums: + if not sub.empty: + subalbums.append(sub) return { "path": self.path, "date": self.date, "albums": subalbums, "photos": self._photos } def photo_from_path(self, path): for photo in self._photos: diff --git a/scanner/TreeWalker.py b/scanner/TreeWalker.py index 96ca5e5..872b830 100644 --- a/scanner/TreeWalker.py +++ b/scanner/TreeWalker.py @@ -49,9 +49,12 @@ class TreeWalker: if photo.is_valid: self.all_photos.append(photo) album.add_photo(photo) - print "Writing cache of %s" % album.cache_path - album.cache(self.cache_path) - self.all_albums.append(album) + if not album.empty: + print "Writing cache of %s" % album.cache_path + album.cache(self.cache_path) + self.all_albums.append(album) + else: + print "Not writing cache of %s because it's empty" % album.cache_path return album def big_lists(self): photo_list = [] @@ -69,19 +72,15 @@ class TreeWalker: fp.close() def remove_stale(self): - print "Removing stale cache entries." + print "Building list of all cache entries." + all_cache_entries = { "all_photos.json": True, "latest_photos.json": True } + for album in self.all_albums: + all_cache_entries[album.cache_path] = True + for photo in self.all_photos: + for entry in photo.image_caches: + all_cache_entries[entry] = True + print "Searching stale cache entries." for cache in os.listdir(self.cache_path): - match = False - for album in self.all_albums: - if cache == album.cache_path: - match = True - break - if match: - continue - for photo in self.all_photos: - if cache in photo.image_caches: - match = True - break - if not match: + if cache not in all_cache_entries: print "Removing stale cache %s" % cache - os.unlink(os.path.join(self.cache_path, cache)) \ No newline at end of file + os.unlink(os.path.join(self.cache_path, cache)) -- cgit v1.2.3-59-g8ed1b