2011-05-05 13:04:40 +02:00
|
|
|
import os
|
|
|
|
import os.path
|
2011-05-05 14:42:04 +02:00
|
|
|
from datetime import datetime
|
2011-05-06 00:37:15 +02:00
|
|
|
from PhotoAlbum import Photo, Album
|
|
|
|
from CachePath import json_cache, set_cache_path_base, file_mtime
|
2011-05-05 13:04:40 +02:00
|
|
|
|
|
|
|
class TreeWalker:
|
|
|
|
def __init__(self, album_path, cache_path):
|
2011-05-06 00:37:15 +02:00
|
|
|
self.album_path = os.path.abspath(album_path)
|
|
|
|
self.cache_path = os.path.abspath(cache_path)
|
2011-05-05 13:04:40 +02:00
|
|
|
set_cache_path_base(self.album_path)
|
|
|
|
self.all_albums = list()
|
|
|
|
self.all_photos = list()
|
2011-05-06 00:37:15 +02:00
|
|
|
self.walk(self.album_path)
|
2011-05-05 13:04:40 +02:00
|
|
|
self.remove_stale()
|
|
|
|
def walk(self, path):
|
2011-05-05 15:17:21 +02:00
|
|
|
print "Walking %s" % path
|
2011-05-05 13:04:40 +02:00
|
|
|
cache = os.path.join(self.cache_path, json_cache(path))
|
|
|
|
cached = False
|
2011-05-05 14:42:04 +02:00
|
|
|
cached_album = None
|
|
|
|
if os.path.exists(cache):
|
2011-05-05 15:17:21 +02:00
|
|
|
print "Has cache %s" % path
|
2011-05-05 14:42:04 +02:00
|
|
|
cached_album = Album.from_cache(cache)
|
2011-05-06 00:37:15 +02:00
|
|
|
if file_mtime(path) <= file_mtime(cache):
|
2011-05-05 15:17:21 +02:00
|
|
|
print "Album is fully cached"
|
2011-05-05 14:42:04 +02:00
|
|
|
cached = True
|
|
|
|
album = cached_album
|
2011-05-06 00:37:15 +02:00
|
|
|
for photo in album.photos:
|
|
|
|
self.all_photos.append(photo)
|
2011-05-05 14:42:04 +02:00
|
|
|
if not cached:
|
2011-05-05 13:04:40 +02:00
|
|
|
album = Album(path)
|
|
|
|
for entry in os.listdir(path):
|
|
|
|
entry = os.path.join(path, entry)
|
|
|
|
if os.path.isdir(entry):
|
|
|
|
album.add_album(self.walk(entry))
|
|
|
|
elif not cached and os.path.isfile(entry):
|
2011-05-05 14:42:04 +02:00
|
|
|
cache_hit = False
|
|
|
|
if cached_album:
|
|
|
|
cached_photo = cached_album.photo_from_path(entry)
|
2011-05-06 00:37:15 +02:00
|
|
|
if cached_photo and file_mtime(entry) <= cached_photo.attributes["DateTimeFile"]:
|
2011-05-05 15:17:21 +02:00
|
|
|
print "Photo cache hit %s" % entry
|
2011-05-05 14:42:04 +02:00
|
|
|
cache_hit = True
|
|
|
|
photo = cached_photo
|
|
|
|
if not cache_hit:
|
2011-05-06 00:37:15 +02:00
|
|
|
print "No cache, scanning %s" % entry
|
2011-05-05 14:42:04 +02:00
|
|
|
photo = Photo(entry, self.cache_path)
|
2011-05-05 13:04:40 +02:00
|
|
|
if photo.is_valid:
|
|
|
|
self.all_photos.append(photo)
|
|
|
|
album.add_photo(photo)
|
2011-05-05 15:17:21 +02:00
|
|
|
print "Writing cache of %s" % album.cache_path
|
2011-05-05 13:04:40 +02:00
|
|
|
album.cache(self.cache_path)
|
|
|
|
self.all_albums.append(album)
|
|
|
|
return album
|
|
|
|
def remove_stale(self):
|
2011-05-06 00:37:15 +02:00
|
|
|
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:
|
|
|
|
print "Removing stale cache %s" % cache
|
|
|
|
os.unlink(os.path.join(self.cache_path, cache))
|