Merge pull request #20 from jmathai/gh-16-preserve-folder-name

gh-16 Add --album-from-folder parameter to elodie.py and media.set_album_from_folder
This commit is contained in:
Jaisen Mathai 2015-12-07 20:35:08 -08:00
commit 5b050fe485
2 changed files with 44 additions and 6 deletions

View File

@ -20,9 +20,9 @@ from elodie.filesystem import FileSystem
from elodie.localstorage import Db from elodie.localstorage import Db
def usage(): def usage():
return """Usage: main.py import --source=<s> --destination=<d> return """Usage: elodie.py import --source=<s> --destination=<d> [--album-from-folder]
main.py import --file=<f> --destination=<d> elodie.py import --file=<f> --destination=<d> [--album-from-folder]
main.py update [--time=<t>] [--location=<l>] [--album=<a>] [--title=<t>] INPUT ... elodie.py update [--time=<t>] [--location=<l>] [--album=<a>] [--title=<t>] INPUT ...
-h --help show this -h --help show this
""" """
@ -41,6 +41,9 @@ def _import(params):
if(media.__name__ == 'Video'): if(media.__name__ == 'Video'):
filesystem.set_date_from_path_video(media) filesystem.set_date_from_path_video(media)
if(params['--album-from-folder'] == True):
media.set_album_from_folder()
dest_path = filesystem.process_file(current_file, destination, media, allowDuplicate=False, move=False) dest_path = filesystem.process_file(current_file, destination, media, allowDuplicate=False, move=False)
if(dest_path is not None): if(dest_path is not None):
print '%s -> %s' % (current_file, dest_path) print '%s -> %s' % (current_file, dest_path)
@ -50,6 +53,9 @@ def _import(params):
if(media.__name__ == 'Video'): if(media.__name__ == 'Video'):
filesystem.set_date_from_path_video(media) filesystem.set_date_from_path_video(media)
if(params['--album-from-folder'] == True):
media.set_album_from_folder()
dest_path = filesystem.process_file(current_file, destination, media, allowDuplicate=False, move=False) dest_path = filesystem.process_file(current_file, destination, media, allowDuplicate=False, move=False)
if(dest_path is not None): if(dest_path is not None):
print '%s -> %s' % (current_file, dest_path) print '%s -> %s' % (current_file, dest_path)

View File

@ -237,11 +237,11 @@ class Media(object):
@returns, dictionary or None for non-photo files @returns, dictionary or None for non-photo files
""" """
def get_metadata(self): def get_metadata(self, update_cache=False):
if(not self.is_valid()): if(not self.is_valid()):
return None return None
if(self.metadata is not None): if(self.metadata is not None and update_cache == False):
return self.metadata return self.metadata
source = self.source source = self.source
@ -254,7 +254,8 @@ class Media(object):
'title': self.get_title(), 'title': self.get_title(),
'mime_type': self.get_mimetype(), 'mime_type': self.get_mimetype(),
'base_name': os.path.splitext(os.path.basename(source))[0], 'base_name': os.path.splitext(os.path.basename(source))[0],
'extension': self.get_extension() 'extension': self.get_extension(),
'directory_path': os.path.dirname(source)
} }
return self.metadata return self.metadata
@ -322,8 +323,28 @@ class Media(object):
exiftool_backup_file = '%s%s' % (source, '_original') exiftool_backup_file = '%s%s' % (source, '_original')
if(os.path.isfile(exiftool_backup_file) is True): if(os.path.isfile(exiftool_backup_file) is True):
os.remove(exiftool_backup_file) os.remove(exiftool_backup_file)
self.set_metadata(album=name)
return True return True
def set_album_from_folder(self):
metadata = self.get_metadata()
print 'huh/'
# If this file has an album already set we do not overwrite EXIF
if(metadata['album'] is not None):
return False
folder = os.path.basename(metadata['directory_path'])
# If folder is empty we skip
if(len(folder) == 0):
return False
self.set_album(folder)
return True
""" """
Specifically update the basename attribute in the metadata dictionary for this instance. Specifically update the basename attribute in the metadata dictionary for this instance.
This is used for when we update the EXIF title of a media file. This is used for when we update the EXIF title of a media file.
@ -336,6 +357,17 @@ class Media(object):
self.get_metadata() self.get_metadata()
self.metadata['base_name'] = new_basename self.metadata['base_name'] = new_basename
"""
Method to manually update attributes in metadata.
@params, named paramaters
"""
def set_metadata(self, **kwargs):
metadata = self.get_metadata()
for key in kwargs:
if(key in metadata):
self.metadata[key] = kwargs[key]
@classmethod @classmethod
def get_class_by_file(Media, _file, classes): def get_class_by_file(Media, _file, classes):
extension = os.path.splitext(_file)[1][1:].lower() extension = os.path.splitext(_file)[1][1:].lower()