Clean up date time conversions.

This commit is contained in:
Jason A. Donenfeld 2011-05-05 07:21:12 -04:00
parent e6c1dfa95c
commit c05145aee2
2 changed files with 18 additions and 12 deletions

View File

@ -12,8 +12,6 @@ def trim_base(path):
if path.startswith('/'): if path.startswith('/'):
path = path[1:] path = path[1:]
return path return path
def untrim_base(path):
return os.path.join(trim_base.base, path)
def cache_base(path): def cache_base(path):
path = trim_base(path).replace('/', '-').replace(' ', '_') path = trim_base(path).replace('/', '-').replace(' ', '_')
if len(path) == 0: if len(path) == 0:
@ -97,11 +95,13 @@ class Photo(object):
def __init__(self, path, attributes=None): def __init__(self, path, attributes=None):
self._path = trim_base(path) self._path = trim_base(path)
self.is_valid = True self.is_valid = True
if attributes is not None: mtime = datetime.fromtimestamp(os.path.getmtime(path))
if attributes is not None and attributes["FileTime"] >= mtime:
self._attributes = attributes self._attributes = attributes
return return
else:
self._attributes = {} self._attributes = {}
self._attributes["FileTime"] = mtime
try: try:
i = Image.open(path) i = Image.open(path)
except: except:
@ -117,21 +117,27 @@ class Photo(object):
if not isinstance(decoded, int) and decoded not in ['JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'FileSource', 'MakerNote', 'UserComment', 'ImageDescription', 'ComponentsConfiguration']: if not isinstance(decoded, int) and decoded not in ['JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'FileSource', 'MakerNote', 'UserComment', 'ImageDescription', 'ComponentsConfiguration']:
if isinstance(value, str): if isinstance(value, str):
value = value.strip() value = value.strip()
if decoded.startswith("DateTime"):
try:
value = datetime.strptime(value, '%Y:%m:%d %H:%M:%S')
except:
pass
self._attributes[decoded] = value self._attributes[decoded] = value
@property @property
def name(self): def name(self):
return os.path.basename(self._path) return os.path.basename(self._path)
def __str__(self): def __str__(self):
return self.name return self.name
@property def cache_path(self, size):
def cache_paths(self): return image_cache(self.path, size)
return [image_cache(self.path, size) for size in [100, 640, 1024]]
@property @property
def date(self): def date(self):
if "DateTime" in self._attributes: if "DateTimeOriginal" in self._attributes:
return datetime.strptime(self._attributes["DateTime"], '%Y:%m:%d %H:%M:%S') return self._attributes["DateTimeOriginal"]
elif "DateTime" in self._attributes:
return self._attributes["DateTime"]
else: else:
return datetime.fromtimestamp(os.path.getmtime(untrim_base(self._path))) return self._attributes["FileTime"]
def __cmp__(self, other): def __cmp__(self, other):
return cmp(self.date, other.date) return cmp(self.date, other.date)
@property @property

View File

@ -32,4 +32,4 @@ class TreeWalker:
self.all_albums.append(album) self.all_albums.append(album)
return album return album
def remove_stale(self): def remove_stale(self):
pass pass #TODO: remove left over caches