gh-16 Add --album-from-folder parameter to elodie.py and media.set_album_from_folder method

This commit is contained in:
Jaisen Mathai 2015-12-02 23:47:54 -08:00
parent 47627df3ad
commit 3442f2cc46
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
def usage():
return """Usage: main.py import --source=<s> --destination=<d>
main.py import --file=<f> --destination=<d>
main.py update [--time=<t>] [--location=<l>] [--album=<a>] [--title=<t>] INPUT ...
return """Usage: elodie.py import --source=<s> --destination=<d> [--album-from-folder]
elodie.py import --file=<f> --destination=<d> [--album-from-folder]
elodie.py update [--time=<t>] [--location=<l>] [--album=<a>] [--title=<t>] INPUT ...
-h --help show this
"""
@ -41,6 +41,9 @@ def _import(params):
if(media.__name__ == 'Video'):
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)
if(dest_path is not None):
print '%s -> %s' % (current_file, dest_path)
@ -50,6 +53,9 @@ def _import(params):
if(media.__name__ == 'Video'):
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)
if(dest_path is not None):
print '%s -> %s' % (current_file, dest_path)

View File

@ -237,11 +237,11 @@ class Media(object):
@returns, dictionary or None for non-photo files
"""
def get_metadata(self):
def get_metadata(self, update_cache=False):
if(not self.is_valid()):
return None
if(self.metadata is not None):
if(self.metadata is not None and update_cache == False):
return self.metadata
source = self.source
@ -254,7 +254,8 @@ class Media(object):
'title': self.get_title(),
'mime_type': self.get_mimetype(),
'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
@ -322,8 +323,28 @@ class Media(object):
exiftool_backup_file = '%s%s' % (source, '_original')
if(os.path.isfile(exiftool_backup_file) is True):
os.remove(exiftool_backup_file)
self.set_metadata(album=name)
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.
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.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
def get_class_by_file(Media, _file, classes):
extension = os.path.splitext(_file)[1][1:].lower()