Moved copying of photos to functions.py, simplified decision whether to copy photo or not.

This commit is contained in:
2020-03-11 22:26:03 +01:00
parent 92b08170f2
commit 2ad8b4a1fb
2 changed files with 26 additions and 28 deletions

View File

@@ -4,6 +4,8 @@
''' Classes used by main program. '''
from datetime import datetime
import os
import shutil
import pyexiv2
class Radiation:
@@ -28,12 +30,27 @@ class Radiation:
class Photo:
''' Reads and writes Exif metadata'''
def __init__(self, photo, local_timezone):
def __init__(self, photo, local_timezone, dest_dir, dry_run):
self.get_date = self._get_creation_date(photo, local_timezone)
self.photo = photo
self.copy_photo = self._copy_photo(photo, dest_dir, dry_run)
def __repr__(self):
return 'Photo Creation Date: %s' % str(self.get_date)
return 'Photo: %s Creation Date: %s' % (str(self.copy_photo), str(self.get_date))
def _copy_photo(self, photo, dest_dir, dry_run):
# Determine where to work on photo and copy it there if needed.
# Get image file name out of path
photo_basename = os.path.basename(photo)
# be os aware and use the correct directory delimiter for destfile
dest_photo = os.path.join(dest_dir, photo_basename)
# Copy photo to dest_dir and return its (new) filename
# if not in dry_run mode or if dest_dir is different from src_dir.
if dry_run is True:
return photo
elif dest_dir != '.':
shutil.copy(photo, dest_photo)
return dest_photo
def _get_creation_date(self, photo, local_timezone):
# Load Exif data from photo
@@ -46,11 +63,11 @@ class Photo:
pic_aware_time = pic_naive_time.astimezone(local_timezone)
return pic_aware_time
def write_exif(self, radiation, latitude, longitude, dry_run):
def write_exif(self, photo, radiation, latitude, longitude, dry_run):
''' UNTESTED ! '''
metadata = pyexiv2.ImageMetadata(self.photo)
metadata = pyexiv2.ImageMetadata(photo)
# Set new UserComment
new_comment = 'Radiation ☢ ' + str(radiation) + ' µS/h'
@@ -66,6 +83,6 @@ class Photo:
metadata[key] = pyexiv2.ExifTag(key, value)
# Write Exif tags to file, if not in dry-run mode
if dry_run == 'True':
if dry_run is not True:
metadata.write()
return new_comment