LIl things.

This commit is contained in:
Jason A. Donenfeld
2011-05-06 22:48:09 -04:00
parent 2e017d0df3
commit 8bd9d1519e
7 changed files with 109 additions and 28 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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))
os.unlink(os.path.join(self.cache_path, cache))