fix PEP8 warnings

This commit is contained in:
Fabrice Laporte 2015-12-31 00:29:13 +01:00
parent 636b43fca4
commit 02bae17e95
1 changed files with 42 additions and 20 deletions

View File

@ -17,15 +17,23 @@ from elodie.localstorage import Db
def usage(): def usage():
return """Usage: elodie.py import --source=<s> --destination=<d> [--album-from-folder] """Return usage message
"""
return """
Usage: elodie.py import --source=<s> --destination=<d> [--album-from-folder]
elodie.py import --file=<f> --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 ... elodie.py update [--time=<t>] [--location=<l>] [--album=<a>] [--title=<t>] INPUT ...
-h --help show this -h --help show this
"""
DB = Db()
FILESYSTEM = FileSystem()
def import_file(_file, destination, album_from_folder):
"""Set file metadata and move it to destination.
""" """
def import_file(_file, destination):
media = Media.get_class_by_file(_file, (Photo, Video)) media = Media.get_class_by_file(_file, (Photo, Video))
if not media: if not media:
if constants.debug: if constants.debug:
@ -34,28 +42,32 @@ def import_file(_file, destination):
return return
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']: if album_from_folder:
media.set_album_from_folder() media.set_album_from_folder()
dest_path = filesystem.process_file(_file, destination, dest_path = FILESYSTEM.process_file(_file, destination,
media, allowDuplicate=False, move=False) media, allowDuplicate=False, move=False)
if dest_path: if dest_path:
print '%s -> %s' % (_file, dest_path) print '%s -> %s' % (_file, dest_path)
def _import(params): def _import(params):
"""Import files.
"""
destination = os.path.expanduser(params['--destination']) destination = os.path.expanduser(params['--destination'])
if params['--source']: if params['--source']:
source = os.path.expanduser(params['--source']) source = os.path.expanduser(params['--source'])
files = filesystem.get_all_files(source, None) files = FILESYSTEM.get_all_files(source, None)
elif params['--file']: elif params['--file']:
files = [os.path.expanduser(params['--file'])] files = [os.path.expanduser(params['--file'])]
for current_file in files: for current_file in files:
import_file(current_file, destination) import_file(current_file, destination, params['--album-from-folder'])
def update_location(media, file_path, location_name): def update_location(media, file_path, location_name):
"""Update location exif metadata of media. """Update location exif metadata of media.
""" """
@ -135,37 +147,47 @@ def _update(params):
title_update_status = media.set_title(params['--title']) title_update_status = media.set_title(params['--title'])
original_title = metadata['title'] original_title = metadata['title']
if title_update_status and original_title: if title_update_status and original_title:
# @TODO: We should move this to a shared method since FileSystem.get_file_name() does it too. # @TODO: We should move this to a shared method since
original_title = re.sub('\W+', '-', original_title.lower()) # FileSystem.get_file_name() does it too.
original_title = re.sub(r'\W+', '-', original_title.lower())
original_base_name = metadata['base_name'] original_base_name = metadata['base_name']
remove_old_title_from_name = True remove_old_title_from_name = True
updated = True updated = True
if updated: if updated:
updated_media = Media.get_class_by_file(file_path, (Photo, Video)) updated_media = Media.get_class_by_file(file_path, (Photo, Video))
# See comments above on why we have to do this when titles get updated. # See comments above on why we have to do this when titles
# get updated.
if remove_old_title_from_name and len(original_title) > 0: if remove_old_title_from_name and len(original_title) > 0:
updated_media.get_metadata() updated_media.get_metadata()
updated_media.set_metadata_basename( updated_media.set_metadata_basename(
original_base_name.replace('-%s' % original_title, '')) original_base_name.replace('-%s' % original_title, ''))
dest_path = filesystem.process_file(file_path, destination, dest_path = FILESYSTEM.process_file(file_path, destination,
updated_media, move=True, allowDuplicate=True) updated_media, move=True, allowDuplicate=True)
if constants.debug: if constants.debug:
print u'%s -> %s' % (file_path, dest_path) print u'%s -> %s' % (file_path, dest_path)
print '{"source":"%s", "destination":"%s"}' % (file_path, dest_path) print '{"source":"%s", "destination":"%s"}' % (file_path,
# If the folder we moved the file out of or its parent are empty we delete it. dest_path)
filesystem.delete_directory_if_empty(os.path.dirname(file_path)) # If the folder we moved the file out of or its parent are empty
filesystem.delete_directory_if_empty(os.path.dirname(os.path.dirname(file_path))) # we delete it.
FILESYSTEM.delete_directory_if_empty(os.path.dirname(file_path))
FILESYSTEM.delete_directory_if_empty(
os.path.dirname(os.path.dirname(file_path)))
db = Db() def main(argv=None):
filesystem = FileSystem() """Main function call elodie subcommand on files.
"""
if __name__ == '__main__': if argv is None:
argv = sys.argv
params = docopt(usage()) params = docopt(usage())
if params['import']: if params['import']:
_import(params) _import(params)
elif params['update']: elif params['update']:
_update(params) _update(params)
sys.exit(0) sys.exit(0)
if __name__ == '__main__':
sys.exit(main())