pythonify elodie.py

Remove C like parentheses around conditions.
Replace “xxx is None” => “not x”.
Use tuple when list content won’t change.
Fix some others PEP8 warnings.
This commit is contained in:
Fabrice Laporte 2015-12-30 01:30:53 +01:00
parent 4ca2013572
commit 8a7725f68e
1 changed files with 38 additions and 32 deletions

View File

@ -61,35 +61,39 @@ def _import(params):
def _update(params): def _update(params):
location_coords = None location_coords = None
for file_path in params['INPUT']: for file_path in params['INPUT']:
if(not os.path.exists(file_path)): if not os.path.exists(file_path):
if(constants.debug == True): if constants.debug:
print 'Could not find %s' % file_path print 'Could not find %s' % file_path
print '{"source":"%s", "error_msg":"Could not find %s"}' % (file_path, file_path) print '{"source":"%s", "error_msg":"Could not find %s"}' % \
(file_path, file_path)
continue continue
file_path = os.path.expanduser(file_path) file_path = os.path.expanduser(file_path)
destination = os.path.expanduser(os.path.dirname(os.path.dirname(os.path.dirname(file_path)))) destination = os.path.expanduser(os.path.dirname(os.path.dirname(
os.path.dirname(file_path))))
media = Media.get_class_by_file(file_path, [Photo, Video]) media = Media.get_class_by_file(file_path, (Photo, Video))
if(media is None): if not media:
continue continue
updated = False updated = False
if(params['--location'] is not None): if params['--location']:
if(location_coords is None): if not location_coords:
location_coords = geolocation.coordinates_by_name(params['--location']) location_coords = geolocation.coordinates_by_name(
params['--location'])
if(location_coords is not None and 'latitude' in location_coords and 'longitude' in location_coords): if location_coords and 'latitude' in location_coords and \
location_status = media.set_location(location_coords['latitude'], location_coords['longitude']) 'longitude' in location_coords:
if(location_status != True): location_status = media.set_location(location_coords[
if(constants.debug == True): 'latitude'], location_coords['longitude'])
if not location_status:
if constants.debug:
print 'Failed to update location' print 'Failed to update location'
print '{"source":"%s","error_msg":"Failed to update location"}' % file_path print '{"source":"%s","error_msg":"Failed to update location"}' % file_path
sys.exit(1) sys.exit(1)
updated = True updated = True
if params['--time']:
if(params['--time'] is not None):
time_string = params['--time'] time_string = params['--time']
time_format = '%Y-%m-%d %H:%M:%S' time_format = '%Y-%m-%d %H:%M:%S'
if(re.match('^\d{4}-\d{2}-\d{2}$', time_string)): if(re.match('^\d{4}-\d{2}-\d{2}$', time_string)):
@ -106,40 +110,42 @@ def _update(params):
media.set_date_taken(time) media.set_date_taken(time)
updated = True updated = True
if(params['--album'] is not None): if params['--album']:
media.set_album(params['--album']) media.set_album(params['--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.
# You would end up with img_001.jpg -> img_001-first-title.jpg -> img_001-first-title-second-title.jpg. # You would end up with img_001.jpg -> img_001-first-title.jpg ->
# img_001-first-title-second-title.jpg.
# To resolve that we have to track the prior title (if there was one. # To resolve that we have to track the prior title (if there was one.
# Then we massage the updated_media's metadata['base_name'] to remove the old title. # Then we massage the updated_media's metadata['base_name'] to remove
# Since FileSystem.get_file_name() relies on base_name it will properly rename the file by updating the title # the old title.
# instead of appending it. # Since FileSystem.get_file_name() relies on base_name it will properly
# 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'] is not None): if params['--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(params['--title'])
original_title = metadata['title'] original_title = metadata['title']
if(title_update_status and original_title is not None): 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 FileSystem.get_file_name() does it too.
original_title = re.sub('\W+', '-', original_title.lower()) original_title = re.sub('\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 == True): 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 is True 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(original_base_name.replace('-%s' % original_title, '')) updated_media.set_metadata_basename(
original_base_name.replace('-%s' % original_title, ''))
dest_path = filesystem.process_file(file_path, destination, updated_media, move=True, allowDuplicate=True)
if(constants.debug == True):
print '%s -> %s' % (file_path, dest_path)
dest_path = filesystem.process_file(file_path, destination,
updated_media, move=True, allowDuplicate=True)
if constants.debug:
print '{"source":"%s", "destination":"%s"}' % (file_path, dest_path) print '{"source":"%s", "destination":"%s"}' % (file_path, dest_path)
# If the folder we moved the file out of or its parent are empty we delete it. # If the folder we moved the file out of or its parent are empty we delete it.
filesystem.delete_directory_if_empty(os.path.dirname(file_path)) filesystem.delete_directory_if_empty(os.path.dirname(file_path))