diff --git a/elodie/constants.py b/elodie/constants.py index c3c0f61..bc878d1 100644 --- a/elodie/constants.py +++ b/elodie/constants.py @@ -20,4 +20,4 @@ location_db = '{}/location.json'.format(application_directory) script_directory = path.dirname(path.dirname(path.abspath(__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') diff --git a/elodie/filesystem.py b/elodie/filesystem.py index be0f58e..0dcec4f 100644 --- a/elodie/filesystem.py +++ b/elodie/filesystem.py @@ -68,7 +68,7 @@ class FileSystem(object): extensions is None or filename.lower().endswith(extensions) ): - files.append('%s/%s' % (dirname, filename)) + files.append(os.path.join(dirname, filename)) return files def get_current_directory(self): @@ -164,7 +164,7 @@ class FileSystem(object): path.append('Unknown Location') # return '/'.join(path[::-1]) - return '/'.join(path) + return os.path.join(*path) def process_file(self, _file, destination, media, **kwargs): move = False @@ -179,9 +179,9 @@ class FileSystem(object): 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) - dest_path = '%s/%s' % (dest_directory, file_name) + dest_path = os.path.join(dest_directory, file_name) db = Db() checksum = db.checksum(_file) diff --git a/elodie/localstorage.py b/elodie/localstorage.py index 9373ef4..8f1d837 100644 --- a/elodie/localstorage.py +++ b/elodie/localstorage.py @@ -98,7 +98,7 @@ class Db(object): :returns: str or None """ hasher = hashlib.sha256() - with open(file_path, 'r') as f: + with open(file_path, 'rb') as f: buf = f.read(blocksize) while len(buf) > 0: diff --git a/elodie/media/media.py b/elodie/media/media.py index 57813f1..d60b82d 100644 --- a/elodie/media/media.py +++ b/elodie/media/media.py @@ -106,9 +106,10 @@ class Media(object): source = self.source process_output = subprocess.Popen( - ['%s "%s"' % (exiftool, source)], + [exiftool, source], stdout=subprocess.PIPE, - shell=True + shell=True, + universal_newlines=True ) output = process_output.stdout.read() @@ -224,8 +225,7 @@ class Media(object): if(constants.debug is True): print '%s -config "%s" -xmp-elodie:Album="%s" "%s"' % (exiftool, exiftool_config, name, source) # noqa process_output = subprocess.Popen( - ['%s -config "%s" -xmp-elodie:Album="%s" "%s"' % - (exiftool, exiftool_config, name, source)], + [exiftool, '-config', exiftool_config, '-xmp-elodie:Album=%s'%(name),source], stdout=subprocess.PIPE, shell=True ) diff --git a/elodie/media/video.py b/elodie/media/video.py index ad5021a..d06c03f 100644 --- a/elodie/media/video.py +++ b/elodie/media/video.py @@ -156,9 +156,10 @@ class Video(Media): source = self.source process_output = subprocess.Popen( - ['%s "%s"' % (exiftool, source)], + [exiftool, source], stdout=subprocess.PIPE, - shell=True + shell=True, + universal_newlines=True ) return process_output.stdout.read() diff --git a/elodie/tests/filesystem_test.py b/elodie/tests/filesystem_test.py index fe4c4a7..0823fbc 100644 --- a/elodie/tests/filesystem_test.py +++ b/elodie/tests/filesystem_test.py @@ -18,7 +18,7 @@ os.environ['TZ'] = 'GMT' def test_create_directory_success(): 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) # Needs to be a subdirectory @@ -34,7 +34,7 @@ def test_create_directory_success(): def test_create_directory_recursive_success(): 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) # Needs to be a subdirectory @@ -47,14 +47,16 @@ def test_create_directory_recursive_success(): shutil.rmtree(folder) def test_create_directory_invalid_permissions(): + if os.name == 'nt': + raise SkipTest("It isn't implemented on Windows") filesystem = FileSystem() - status = filesystem.create_directory('/apathwhichdoesnotexist/afolderwhichdoesnotexist') + status = filesystem.create_directory(os.path.join('apathwhichdoesnotexist','afolderwhichdoesnotexist')) assert status == False def test_delete_directory_if_empty(): 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) 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(): 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) parent_folder = os.path.dirname(folder) @@ -152,34 +154,34 @@ def test_get_folder_path_plain(): media = Photo(helper.get_file('plain.jpg')) 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(): filesystem = FileSystem() media = Photo(helper.get_file('with-title.jpg')) 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(): filesystem = FileSystem() media = Photo(helper.get_file('with-location.jpg')) 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(): filesystem = FileSystem() media = Photo(helper.get_file('with-location-and-title.jpg')) 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(): filesystem = FileSystem() 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) media = Photo(origin) @@ -193,7 +195,7 @@ def test_process_file_plain(): assert origin_checksum is not None, origin_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(): filesystem = FileSystem() @@ -213,13 +215,13 @@ def test_process_file_with_title(): assert origin_checksum is not None, origin_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(): filesystem = FileSystem() 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) media = Photo(origin) @@ -233,13 +235,13 @@ def test_process_file_with_location(): assert origin_checksum is not None, origin_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(): filesystem = FileSystem() 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) 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 == 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(): filesystem = FileSystem() 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) media = Photo(origin) @@ -273,13 +275,13 @@ def test_process_file_with_album(): assert origin_checksum is not None, origin_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(): filesystem = FileSystem() 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) 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 == 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(): filesystem = FileSystem() 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) 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 == 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 diff --git a/elodie/tests/helper.py b/elodie/tests/helper.py index 023726c..574241e 100644 --- a/elodie/tests/helper.py +++ b/elodie/tests/helper.py @@ -17,14 +17,14 @@ def checksum(file_path, blocksize=65536): def create_working_folder(): 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) return (temporary_folder, folder) def get_file(name): 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(): return (61.013710, 99.196656, 'Siberia') diff --git a/elodie/tests/media/photo_test.py b/elodie/tests/media/photo_test.py index 41809b6..eeb2e11 100644 --- a/elodie/tests/media/photo_test.py +++ b/elodie/tests/media/photo_test.py @@ -3,7 +3,8 @@ import os import sys -import datetime +from datetime import datetime +from datetime import timedelta import shutil import tempfile import time @@ -18,6 +19,14 @@ from elodie.media.media import Media from elodie.media.photo import Photo 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(): photo = Photo() @@ -95,7 +104,8 @@ def test_get_date_taken(): photo = Photo(helper.get_file('plain.jpg')) 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(): 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) 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 @@ -136,7 +146,8 @@ def test_set_date_taken_with_missing_datetimeoriginal(): 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(): temporary_folder, folder = helper.create_working_folder() @@ -145,7 +156,7 @@ def test_set_date_taken(): shutil.copyfile(helper.get_file('plain.jpg'), 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 @@ -156,7 +167,8 @@ def test_set_date_taken(): 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(): raise SkipTest('gh-31, precision is lost in conversion from decimal to dms')