core and photo_tests fixed

This commit is contained in:
zserg 2016-01-26 22:01:05 +03:00
parent 5babcf3583
commit 91e886163e
8 changed files with 57 additions and 42 deletions

View File

@ -20,4 +20,4 @@ location_db = '{}/location.json'.format(application_directory)
script_directory = path.dirname(path.dirname(path.abspath(__file__))) script_directory = path.dirname(path.dirname(path.abspath(__file__)))
#: Path to Elodie's ExifTool config file. #: Path to Elodie's ExifTool config file.
exiftool_config = '%s/configs/ExifTool_config' % script_directory exiftool_config = path.join(script_directory,'configs','ExifTool_config')

View File

@ -68,7 +68,7 @@ class FileSystem(object):
extensions is None or extensions is None or
filename.lower().endswith(extensions) filename.lower().endswith(extensions)
): ):
files.append('%s/%s' % (dirname, filename)) files.append(os.path.join(dirname, filename))
return files return files
def get_current_directory(self): def get_current_directory(self):
@ -164,7 +164,7 @@ class FileSystem(object):
path.append('Unknown Location') path.append('Unknown Location')
# return '/'.join(path[::-1]) # return '/'.join(path[::-1])
return '/'.join(path) return os.path.join(*path)
def process_file(self, _file, destination, media, **kwargs): def process_file(self, _file, destination, media, **kwargs):
move = False move = False
@ -179,9 +179,9 @@ class FileSystem(object):
directory_name = self.get_folder_path(metadata) directory_name = self.get_folder_path(metadata)
dest_directory = '%s/%s' % (destination, directory_name) dest_directory = os.path.join(destination, directory_name)
file_name = self.get_file_name(media) file_name = self.get_file_name(media)
dest_path = '%s/%s' % (dest_directory, file_name) dest_path = os.path.join(dest_directory, file_name)
db = Db() db = Db()
checksum = db.checksum(_file) checksum = db.checksum(_file)

View File

@ -98,7 +98,7 @@ class Db(object):
:returns: str or None :returns: str or None
""" """
hasher = hashlib.sha256() hasher = hashlib.sha256()
with open(file_path, 'r') as f: with open(file_path, 'rb') as f:
buf = f.read(blocksize) buf = f.read(blocksize)
while len(buf) > 0: while len(buf) > 0:

View File

@ -106,9 +106,10 @@ class Media(object):
source = self.source source = self.source
process_output = subprocess.Popen( process_output = subprocess.Popen(
['%s "%s"' % (exiftool, source)], [exiftool, source],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
shell=True shell=True,
universal_newlines=True
) )
output = process_output.stdout.read() output = process_output.stdout.read()
@ -224,8 +225,7 @@ class Media(object):
if(constants.debug is True): if(constants.debug is True):
print '%s -config "%s" -xmp-elodie:Album="%s" "%s"' % (exiftool, exiftool_config, name, source) # noqa print '%s -config "%s" -xmp-elodie:Album="%s" "%s"' % (exiftool, exiftool_config, name, source) # noqa
process_output = subprocess.Popen( process_output = subprocess.Popen(
['%s -config "%s" -xmp-elodie:Album="%s" "%s"' % [exiftool, '-config', exiftool_config, '-xmp-elodie:Album=%s'%(name),source],
(exiftool, exiftool_config, name, source)],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
shell=True shell=True
) )

View File

@ -156,9 +156,10 @@ class Video(Media):
source = self.source source = self.source
process_output = subprocess.Popen( process_output = subprocess.Popen(
['%s "%s"' % (exiftool, source)], [exiftool, source],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
shell=True shell=True,
universal_newlines=True
) )
return process_output.stdout.read() return process_output.stdout.read()

View File

@ -18,7 +18,7 @@ os.environ['TZ'] = 'GMT'
def test_create_directory_success(): def test_create_directory_success():
filesystem = FileSystem() filesystem = FileSystem()
folder = '%s/%s' % (helper.temp_dir(), helper.random_string(10)) folder = os.path.join(helper.temp_dir(), helper.random_string(10))
status = filesystem.create_directory(folder) status = filesystem.create_directory(folder)
# Needs to be a subdirectory # Needs to be a subdirectory
@ -34,7 +34,7 @@ def test_create_directory_success():
def test_create_directory_recursive_success(): def test_create_directory_recursive_success():
filesystem = FileSystem() filesystem = FileSystem()
folder = '%s/%s/%s' % (helper.temp_dir(), helper.random_string(10), helper.random_string(10)) folder = os.path.join(helper.temp_dir(), helper.random_string(10), helper.random_string(10))
status = filesystem.create_directory(folder) status = filesystem.create_directory(folder)
# Needs to be a subdirectory # Needs to be a subdirectory
@ -47,14 +47,16 @@ def test_create_directory_recursive_success():
shutil.rmtree(folder) shutil.rmtree(folder)
def test_create_directory_invalid_permissions(): def test_create_directory_invalid_permissions():
if os.name == 'nt':
raise SkipTest("It isn't implemented on Windows")
filesystem = FileSystem() filesystem = FileSystem()
status = filesystem.create_directory('/apathwhichdoesnotexist/afolderwhichdoesnotexist') status = filesystem.create_directory(os.path.join('apathwhichdoesnotexist','afolderwhichdoesnotexist'))
assert status == False assert status == False
def test_delete_directory_if_empty(): def test_delete_directory_if_empty():
filesystem = FileSystem() filesystem = FileSystem()
folder = '%s/%s' % (helper.temp_dir(), helper.random_string(10)) folder = os.path.join(helper.temp_dir(), helper.random_string(10))
os.makedirs(folder) os.makedirs(folder)
assert os.path.isdir(folder) == True assert os.path.isdir(folder) == True
@ -67,7 +69,7 @@ def test_delete_directory_if_empty():
def test_delete_directory_if_empty_when_not_empty(): def test_delete_directory_if_empty_when_not_empty():
filesystem = FileSystem() filesystem = FileSystem()
folder = '%s/%s/%s' % (helper.temp_dir(), helper.random_string(10), helper.random_string(10)) folder = os.path.join(helper.temp_dir(), helper.random_string(10), helper.random_string(10))
os.makedirs(folder) os.makedirs(folder)
parent_folder = os.path.dirname(folder) parent_folder = os.path.dirname(folder)
@ -152,34 +154,34 @@ def test_get_folder_path_plain():
media = Photo(helper.get_file('plain.jpg')) media = Photo(helper.get_file('plain.jpg'))
path = filesystem.get_folder_path(media.get_metadata()) path = filesystem.get_folder_path(media.get_metadata())
assert path == '2015-12-Dec/Unknown Location', path assert path == os.path.join('2015-12-Dec','Unknown Location'), path
def test_get_folder_path_with_title(): def test_get_folder_path_with_title():
filesystem = FileSystem() filesystem = FileSystem()
media = Photo(helper.get_file('with-title.jpg')) media = Photo(helper.get_file('with-title.jpg'))
path = filesystem.get_folder_path(media.get_metadata()) path = filesystem.get_folder_path(media.get_metadata())
assert path == '2015-12-Dec/Unknown Location', path assert path == os.path.join('2015-12-Dec','Unknown Location'), path
def test_get_folder_path_with_location(): def test_get_folder_path_with_location():
filesystem = FileSystem() filesystem = FileSystem()
media = Photo(helper.get_file('with-location.jpg')) media = Photo(helper.get_file('with-location.jpg'))
path = filesystem.get_folder_path(media.get_metadata()) path = filesystem.get_folder_path(media.get_metadata())
assert path == '2015-12-Dec/Sunnyvale', path assert path == os.path.join('2015-12-Dec','Sunnyvale'), path
def test_get_folder_path_with_location_and_title(): def test_get_folder_path_with_location_and_title():
filesystem = FileSystem() filesystem = FileSystem()
media = Photo(helper.get_file('with-location-and-title.jpg')) media = Photo(helper.get_file('with-location-and-title.jpg'))
path = filesystem.get_folder_path(media.get_metadata()) path = filesystem.get_folder_path(media.get_metadata())
assert path == '2015-12-Dec/Sunnyvale', path assert path == os.path.join('2015-12-Dec','Sunnyvale'), path
def test_process_file_plain(): def test_process_file_plain():
filesystem = FileSystem() filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder() temporary_folder, folder = helper.create_working_folder()
origin = '%s/photo.jpg' % folder origin = os.path.join(folder,'photo.jpg')
shutil.copyfile(helper.get_file('plain.jpg'), origin) shutil.copyfile(helper.get_file('plain.jpg'), origin)
media = Photo(origin) media = Photo(origin)
@ -193,7 +195,7 @@ def test_process_file_plain():
assert origin_checksum is not None, origin_checksum assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum assert origin_checksum == destination_checksum, destination_checksum
assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-photo.jpg' in destination, destination assert os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo.jpg') in destination, destination
def test_process_file_with_title(): def test_process_file_with_title():
filesystem = FileSystem() filesystem = FileSystem()
@ -213,13 +215,13 @@ def test_process_file_with_title():
assert origin_checksum is not None, origin_checksum assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum assert origin_checksum == destination_checksum, destination_checksum
assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-photo-some-title.jpg' in destination, destination assert os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo-some-title.jpg') in destination, destination
def test_process_file_with_location(): def test_process_file_with_location():
filesystem = FileSystem() filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder() temporary_folder, folder = helper.create_working_folder()
origin = '%s/photo.jpg' % folder origin = os.path.join(folder,'photo.jpg')
shutil.copyfile(helper.get_file('with-location.jpg'), origin) shutil.copyfile(helper.get_file('with-location.jpg'), origin)
media = Photo(origin) media = Photo(origin)
@ -233,13 +235,13 @@ def test_process_file_with_location():
assert origin_checksum is not None, origin_checksum assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum assert origin_checksum == destination_checksum, destination_checksum
assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-photo.jpg' in destination, destination assert os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo.jpg') in destination, destination
def test_process_file_with_location_and_title(): def test_process_file_with_location_and_title():
filesystem = FileSystem() filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder() temporary_folder, folder = helper.create_working_folder()
origin = '%s/photo.jpg' % folder origin = os.path.join(folder,'photo.jpg')
shutil.copyfile(helper.get_file('with-location-and-title.jpg'), origin) shutil.copyfile(helper.get_file('with-location-and-title.jpg'), origin)
media = Photo(origin) media = Photo(origin)
@ -253,13 +255,13 @@ def test_process_file_with_location_and_title():
assert origin_checksum is not None, origin_checksum assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum assert origin_checksum == destination_checksum, destination_checksum
assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-photo-some-title.jpg' in destination, destination assert os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo-some-title.jpg') in destination, destination
def test_process_file_with_album(): def test_process_file_with_album():
filesystem = FileSystem() filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder() temporary_folder, folder = helper.create_working_folder()
origin = '%s/photo.jpg' % folder origin = os.path.join(folder,'photo.jpg')
shutil.copyfile(helper.get_file('with-album.jpg'), origin) shutil.copyfile(helper.get_file('with-album.jpg'), origin)
media = Photo(origin) media = Photo(origin)
@ -273,13 +275,13 @@ def test_process_file_with_album():
assert origin_checksum is not None, origin_checksum assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum assert origin_checksum == destination_checksum, destination_checksum
assert '2015-12-Dec/Test Album/2015-12-05_00-59-26-photo.jpg' in destination, destination assert os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo.jpg') in destination, destination
def test_process_file_with_album_and_title(): def test_process_file_with_album_and_title():
filesystem = FileSystem() filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder() temporary_folder, folder = helper.create_working_folder()
origin = '%s/photo.jpg' % folder origin = os.path.join(folder,'photo.jpg')
shutil.copyfile(helper.get_file('with-album-and-title.jpg'), origin) shutil.copyfile(helper.get_file('with-album-and-title.jpg'), origin)
media = Photo(origin) media = Photo(origin)
@ -293,13 +295,13 @@ def test_process_file_with_album_and_title():
assert origin_checksum is not None, origin_checksum assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum assert origin_checksum == destination_checksum, destination_checksum
assert '2015-12-Dec/Test Album/2015-12-05_00-59-26-photo-some-title.jpg' in destination, destination assert os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg') in destination, destination
def test_process_file_with_album_and_title_and_location(): def test_process_file_with_album_and_title_and_location():
filesystem = FileSystem() filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder() temporary_folder, folder = helper.create_working_folder()
origin = '%s/photo.jpg' % folder origin = os.path.join(folder,'photo.jpg')
shutil.copyfile(helper.get_file('with-album-and-title-and-location.jpg'), origin) shutil.copyfile(helper.get_file('with-album-and-title-and-location.jpg'), origin)
media = Photo(origin) media = Photo(origin)
@ -313,4 +315,4 @@ def test_process_file_with_album_and_title_and_location():
assert origin_checksum is not None, origin_checksum assert origin_checksum is not None, origin_checksum
assert origin_checksum == destination_checksum, destination_checksum assert origin_checksum == destination_checksum, destination_checksum
assert '2015-12-Dec/Test Album/2015-12-05_00-59-26-photo-some-title.jpg' in destination, destination assert os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg') in destination, destination

View File

@ -17,14 +17,14 @@ def checksum(file_path, blocksize=65536):
def create_working_folder(): def create_working_folder():
temporary_folder = tempfile.gettempdir() temporary_folder = tempfile.gettempdir()
folder = '%s/%s/%s' % (temporary_folder, random_string(10), random_string(10)) folder = os.path.join(temporary_folder, random_string(10), random_string(10))
os.makedirs(folder) os.makedirs(folder)
return (temporary_folder, folder) return (temporary_folder, folder)
def get_file(name): def get_file(name):
current_folder = os.path.dirname(os.path.realpath(__file__)) current_folder = os.path.dirname(os.path.realpath(__file__))
return '%s/files/%s' % (current_folder, name) return os.path.join(current_folder, 'files', name)
def get_test_location(): def get_test_location():
return (61.013710, 99.196656, 'Siberia') return (61.013710, 99.196656, 'Siberia')

View File

@ -3,7 +3,8 @@
import os import os
import sys import sys
import datetime from datetime import datetime
from datetime import timedelta
import shutil import shutil
import tempfile import tempfile
import time import time
@ -18,6 +19,14 @@ from elodie.media.media import Media
from elodie.media.photo import Photo from elodie.media.photo import Photo
os.environ['TZ'] = 'GMT' os.environ['TZ'] = 'GMT'
if os.name == 'nt':
tz_shift = (datetime.fromtimestamp(0) -
datetime.utcfromtimestamp(0)).seconds/3600
else:
tz_shift = 0
def time_convert(s_time):
return datetime.fromtimestamp(time.mktime(s_time))
def test_photo_extensions(): def test_photo_extensions():
photo = Photo() photo = Photo()
@ -95,7 +104,8 @@ def test_get_date_taken():
photo = Photo(helper.get_file('plain.jpg')) photo = Photo(helper.get_file('plain.jpg'))
date_taken = photo.get_date_taken() date_taken = photo.get_date_taken()
assert date_taken == (2015, 12, 5, 0, 59, 26, 5, 339, 0), date_taken # assert date_taken == (2015, 12, 5, 0, 59, 26, 5, 339, 0), date_taken
assert time_convert(date_taken) == time_convert((2015, 12, 5, 0, 59, 26, 5, 339, 0)) - timedelta(hours = tz_shift), date_taken
def test_get_date_taken_without_exif(): def test_get_date_taken_without_exif():
source = helper.get_file('no-exif.jpg') source = helper.get_file('no-exif.jpg')
@ -125,7 +135,7 @@ def test_set_date_taken_with_missing_datetimeoriginal():
shutil.copyfile(helper.get_file('no-exif.jpg'), origin) shutil.copyfile(helper.get_file('no-exif.jpg'), origin)
photo = Photo(origin) photo = Photo(origin)
status = photo.set_date_taken(datetime.datetime(2013, 9, 30, 7, 6, 5)) status = photo.set_date_taken(datetime(2013, 9, 30, 7, 6, 5))
assert status == True, status assert status == True, status
@ -136,7 +146,8 @@ def test_set_date_taken_with_missing_datetimeoriginal():
shutil.rmtree(folder) shutil.rmtree(folder)
assert date_taken == (2013, 9, 30, 7, 6, 5, 0, 273, 0), metadata['date_taken'] #assert date_taken == (2013, 9, 30, 7, 6, 5, 0, 273, 0), metadata['date_taken']
assert time_convert(date_taken) == time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)) - timedelta(hours = tz_shift), metadata['date_taken']
def test_set_date_taken(): def test_set_date_taken():
temporary_folder, folder = helper.create_working_folder() temporary_folder, folder = helper.create_working_folder()
@ -145,7 +156,7 @@ def test_set_date_taken():
shutil.copyfile(helper.get_file('plain.jpg'), origin) shutil.copyfile(helper.get_file('plain.jpg'), origin)
photo = Photo(origin) photo = Photo(origin)
status = photo.set_date_taken(datetime.datetime(2013, 9, 30, 7, 6, 5)) status = photo.set_date_taken(datetime(2013, 9, 30, 7, 6, 5))
assert status == True, status assert status == True, status
@ -156,7 +167,8 @@ def test_set_date_taken():
shutil.rmtree(folder) shutil.rmtree(folder)
assert date_taken == (2013, 9, 30, 7, 6, 5, 0, 273, 0), metadata['date_taken'] #assert date_taken == (2013, 9, 30, 7, 6, 5, 0, 273, 0), metadata['date_taken']
assert time_convert(date_taken) == time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)) - timedelta(hours = tz_shift), metadata['date_taken']
def test_set_location(): def test_set_location():
raise SkipTest('gh-31, precision is lost in conversion from decimal to dms') raise SkipTest('gh-31, precision is lost in conversion from decimal to dms')