gh-79 Switched to click for argument parsing

This commit is contained in:
Nathan Ostgard 2016-01-18 01:54:12 -08:00
parent 8352bd5f48
commit c7a7f57b46
3 changed files with 58 additions and 47 deletions

View File

@ -48,7 +48,7 @@ Here's dada's (very asynchronous) setup.
* AirDrop is fast, easy for momma to use and once the transfer is finished they don't have to stick around. I'll move it to Dropbox and Dropbox will sync it to their servers. * AirDrop is fast, easy for momma to use and once the transfer is finished they don't have to stick around. I'll move it to Dropbox and Dropbox will sync it to their servers.
* Periodically recategorize photos by fixing their location or date or by adding them to an album. * Periodically recategorize photos by fixing their location or date or by adding them to an album.
* Have a Synology at home set to automatically sync down from Dropbox. * Have a Synology at home set to automatically sync down from Dropbox.
This setup means dada can quickly get photos off his or momma's phone and know that they'll be organized and backed up by the time they're ready to view them. This setup means dada can quickly get photos off his or momma's phone and know that they'll be organized and backed up by the time they're ready to view them.
## Let's organize your existing photos ## Let's organize your existing photos

101
elodie.py
View File

@ -3,9 +3,9 @@
import os import os
import re import re
import sys import sys
from datetime import datetime from datetime import datetime
from docopt import docopt
import click
from send2trash import send2trash from send2trash import send2trash
# Verify that external dependencies are present first, so the user gets a # Verify that external dependencies are present first, so the user gets a
@ -24,16 +24,6 @@ from elodie.filesystem import FileSystem
from elodie.localstorage import Db from elodie.localstorage import Db
def usage():
"""Return usage message
"""
return """
Usage: elodie.py import --destination=<d> [--source=<s>] [--file=<f>] [--album-from-folder] [--trash] [INPUT ...]
elodie.py update [--time=<t>] [--location=<l>] [--album=<a>] [--title=<t>] INPUT ...
-h --help show this
"""
DB = Db() DB = Db()
FILESYSTEM = FileSystem() FILESYSTEM = FileSystem()
@ -69,17 +59,29 @@ def import_file(_file, destination, album_from_folder, trash):
send2trash(_file) send2trash(_file)
def _import(params): @click.command('import')
"""Import files. @click.option('--destination', type=click.Path(file_okay=False),
required=True, help='Copy imported files into this directory.')
@click.option('--source', type=click.Path(file_okay=False),
help='Import files from this directory, if specified.')
@click.option('--file', type=click.Path(dir_okay=False),
help='Import this file, if specified.')
@click.option('--album-from-folder', default=False, is_flag=True,
help="Use images' folders as their album names.")
@click.option('--trash', default=False, is_flag=True,
help='After copying files, move the old files to the trash.')
@click.argument('paths', nargs=-1, type=click.Path())
def _import(destination, source, file, album_from_folder, trash, paths):
"""Import files or directories.
""" """
destination = os.path.expanduser(params['--destination']) destination = os.path.expanduser(destination)
files = set() files = set()
paths = set(params['INPUT']) paths = set(paths)
if params['--source']: if source:
paths.add(params['--source']) paths.add(source)
if params['--file']: if file:
paths.add(params['--file']) paths.add(file)
for path in paths: for path in paths:
path = os.path.expanduser(path) path = os.path.expanduser(path)
if os.path.isdir(path): if os.path.isdir(path):
@ -88,8 +90,8 @@ def _import(params):
files.add(path) files.add(path)
for current_file in files: for current_file in files:
import_file(current_file, destination, params['--album-from-folder'], import_file(current_file, destination, album_from_folder,
params['--trash']) trash)
def update_location(media, file_path, location_name): def update_location(media, file_path, location_name):
@ -128,10 +130,20 @@ def update_time(media, file_path, time_string):
return True return True
def _update(params): @click.command('update')
@click.option('--album', help='Update the image album.')
@click.option('--location', help=('Update the image location. Location '
'should be the name of a place, like "Las '
'Vegas, NV".'))
@click.option('--time', help=('Update the image time. Time should be in '
'YYYY-mm-dd hh:ii:ss or YYYY-mm-dd format.'))
@click.option('--title', help='Update the image title.')
@click.argument('files', nargs=-1, type=click.Path(dir_okay=False),
required=True)
def _update(album, location, time, title, files):
"""Update files. """Update files.
""" """
for file_path in params['INPUT']: for file_path in files:
if not os.path.exists(file_path): if not os.path.exists(file_path):
if constants.debug: if constants.debug:
print 'Could not find %s' % file_path print 'Could not find %s' % file_path
@ -148,12 +160,14 @@ def _update(params):
continue continue
updated = False updated = False
if params['--location']: if location:
updated = update_location(media, file_path, params['--location']) update_location(media, file_path, location)
if params['--time']: updated = True
updated = update_time(media, file_path, params['--time']) if time:
if params['--album']: update_time(media, file_path, time)
media.set_album(params['--album']) updated = True
if album:
media.set_album(album)
updated = True updated = True
# Updating a title can be problematic when doing it 2+ times on a file. # Updating a title can be problematic when doing it 2+ times on a file.
@ -165,10 +179,10 @@ def _update(params):
# Since FileSystem.get_file_name() relies on base_name it will properly # Since FileSystem.get_file_name() relies on base_name it will properly
# rename the file by updating the title instead of appending it. # rename the file by updating the title instead of appending it.
remove_old_title_from_name = False remove_old_title_from_name = False
if params['--title']: if title:
# We call get_metadata() to cache it before making any changes # We call get_metadata() to cache it before making any changes
metadata = media.get_metadata() metadata = media.get_metadata()
title_update_status = media.set_title(params['--title']) title_update_status = media.set_title(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 # @TODO: We should move this to a shared method since
@ -179,7 +193,8 @@ def _update(params):
updated = True updated = True
if updated: if updated:
updated_media = Media.get_class_by_file(file_path, [Audio, Photo, Video]) updated_media = Media.get_class_by_file(file_path,
[Audio, Photo, Video])
# See comments above on why we have to do this when titles # See comments above on why we have to do this when titles
# get updated. # get updated.
if remove_old_title_from_name and len(original_title) > 0: if remove_old_title_from_name and len(original_title) > 0:
@ -200,18 +215,14 @@ def _update(params):
os.path.dirname(os.path.dirname(file_path))) os.path.dirname(os.path.dirname(file_path)))
def main(argv=None): @click.group()
"""Main function call elodie subcommand on files. def main():
""" pass
if argv is None:
argv = sys.argv
params = docopt(usage()) main.add_command(_import)
if params['import']: main.add_command(_update)
_import(params)
elif params['update']:
_update(params)
sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) main()

View File

@ -1,4 +1,4 @@
docopt>=0.6.2,<1.0 click>=6.2,<7.0
LatLon>=1.0.2,<2.0 LatLon>=1.0.2,<2.0
requests>=2.9.1,<3.0 requests>=2.9.1,<3.0
send2trash>=1.3.0,<2.0 send2trash>=1.3.0,<2.0