From 91e886163e888087bd6b213a3d530fcdcfb0dd91 Mon Sep 17 00:00:00 2001 From: zserg Date: Tue, 26 Jan 2016 22:01:05 +0300 Subject: [PATCH 1/9] core and photo_tests fixed --- elodie/constants.py | 2 +- elodie/filesystem.py | 8 +++--- elodie/localstorage.py | 2 +- elodie/media/media.py | 8 +++--- elodie/media/video.py | 5 ++-- elodie/tests/filesystem_test.py | 46 +++++++++++++++++--------------- elodie/tests/helper.py | 4 +-- elodie/tests/media/photo_test.py | 24 ++++++++++++----- 8 files changed, 57 insertions(+), 42 deletions(-) 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') From 6e6f88c336512f468d2512e692a03ddb41bb54ce Mon Sep 17 00:00:00 2001 From: zserg Date: Wed, 27 Jan 2016 15:54:56 +0300 Subject: [PATCH 2/9] some fixes in subprocess.Popen args for windows/linux support --- elodie/media/media.py | 2 +- elodie/media/video.py | 2 +- elodie/tests/filesystem_test.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/elodie/media/media.py b/elodie/media/media.py index d60b82d..31e2a0d 100644 --- a/elodie/media/media.py +++ b/elodie/media/media.py @@ -106,7 +106,7 @@ class Media(object): source = self.source process_output = subprocess.Popen( - [exiftool, source], + '%s "%s"' % (exiftool, source), stdout=subprocess.PIPE, shell=True, universal_newlines=True diff --git a/elodie/media/video.py b/elodie/media/video.py index d06c03f..3ea78fc 100644 --- a/elodie/media/video.py +++ b/elodie/media/video.py @@ -156,7 +156,7 @@ class Video(Media): source = self.source process_output = subprocess.Popen( - [exiftool, source], + '%s "%s"' % (exiftool, source), stdout=subprocess.PIPE, shell=True, universal_newlines=True diff --git a/elodie/tests/filesystem_test.py b/elodie/tests/filesystem_test.py index 0823fbc..bfe05b1 100644 --- a/elodie/tests/filesystem_test.py +++ b/elodie/tests/filesystem_test.py @@ -50,7 +50,7 @@ def test_create_directory_invalid_permissions(): if os.name == 'nt': raise SkipTest("It isn't implemented on Windows") filesystem = FileSystem() - status = filesystem.create_directory(os.path.join('apathwhichdoesnotexist','afolderwhichdoesnotexist')) + status = filesystem.create_directory('/apathwhichdoesnotexist/afolderwhichdoesnotexist') assert status == False From 582d6090238d95e73098b1b67a64d1e90584eccb Mon Sep 17 00:00:00 2001 From: zserg Date: Wed, 27 Jan 2016 18:28:18 +0400 Subject: [PATCH 3/9] windows timezone problem in filesystem test fixed --- elodie/media/media.py | 3 ++- elodie/tests/filesystem_test.py | 34 ++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/elodie/media/media.py b/elodie/media/media.py index 31e2a0d..cb7971a 100644 --- a/elodie/media/media.py +++ b/elodie/media/media.py @@ -225,7 +225,8 @@ 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( - [exiftool, '-config', exiftool_config, '-xmp-elodie:Album=%s'%(name),source], + '%s -config "%s" -xmp-elodie:Album="%s" "%s"' % + (exiftool, exiftool_config, name, source), stdout=subprocess.PIPE, shell=True ) diff --git a/elodie/tests/filesystem_test.py b/elodie/tests/filesystem_test.py index bfe05b1..875b7b4 100644 --- a/elodie/tests/filesystem_test.py +++ b/elodie/tests/filesystem_test.py @@ -4,6 +4,9 @@ import sys import re import shutil +from datetime import datetime +from datetime import timedelta +import time sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) @@ -12,9 +15,22 @@ from elodie.filesystem import FileSystem from elodie.media.media import Media from elodie.media.photo import Photo from elodie.media.video import Video +from nose.plugins.skip import SkipTest os.environ['TZ'] = 'GMT' +if os.name == 'nt': + tz_shift = (datetime.fromtimestamp(0) - + datetime.utcfromtimestamp(0)).seconds/3600 +else: + tz_shift = 0 + +def path_tz_fix(s_path): + #some_prefix2015-12-05_00-59-26-with-title-some-title.jpg + m = re.search('(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})',s_path) + t_date = datetime.fromtimestamp(time.mktime(time.strptime(m.group(0), '%Y-%m-%d_%H-%M-%S'))) + s_date_fix = (t_date-timedelta(hours=tz_shift)).strftime('%Y-%m-%d_%H-%M-%S') + return re.sub('\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}',s_date_fix,s_path) def test_create_directory_success(): filesystem = FileSystem() @@ -128,14 +144,14 @@ def test_get_file_name_plain(): media = Photo(helper.get_file('plain.jpg')) file_name = filesystem.get_file_name(media) - assert file_name == '2015-12-05_00-59-26-plain.jpg', file_name + assert file_name == path_tz_fix('2015-12-05_00-59-26-plain.jpg'), file_name def test_get_file_name_with_title(): filesystem = FileSystem() media = Photo(helper.get_file('with-title.jpg')) file_name = filesystem.get_file_name(media) - assert file_name == '2015-12-05_00-59-26-with-title-some-title.jpg', file_name + assert file_name == path_tz_fix('2015-12-05_00-59-26-with-title-some-title.jpg'), file_name def test_get_folder_name_by_date(): filesystem = FileSystem() @@ -195,7 +211,7 @@ def test_process_file_plain(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo.jpg') in destination, destination + assert path_tz_fix(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() @@ -215,7 +231,7 @@ def test_process_file_with_title(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo-some-title.jpg') in destination, destination + assert path_tz_fix(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() @@ -235,7 +251,7 @@ def test_process_file_with_location(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo.jpg') in destination, destination + assert path_tz_fix(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() @@ -255,7 +271,7 @@ def test_process_file_with_location_and_title(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo-some-title.jpg') in destination, destination + assert path_tz_fix(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() @@ -275,7 +291,7 @@ def test_process_file_with_album(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo.jpg') in destination, destination + assert path_tz_fix(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() @@ -295,7 +311,7 @@ def test_process_file_with_album_and_title(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg') in destination, destination + assert path_tz_fix(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() @@ -315,4 +331,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 os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg') in destination, destination + assert path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination From 215e95149cea8597afdbd9cc7b801949fb514d61 Mon Sep 17 00:00:00 2001 From: zserg Date: Wed, 27 Jan 2016 19:27:12 +0400 Subject: [PATCH 4/9] gui app for windows fix (v1) --- app/html/css/styles.css | 15 +++++++++ app/html/index.html | 3 ++ app/html/js/handlers.js | 64 +++++++++++++++++++++++++++++++++++++-- app/index.js | 1 + app/modules/broadcast.js | 9 +++--- app/modules/config.js | 6 ++-- app/modules/toolbar-ui.js | 23 ++++++++++++-- elodie/constants.py | 2 +- 8 files changed, 109 insertions(+), 14 deletions(-) diff --git a/app/html/css/styles.css b/app/html/css/styles.css index 9ead916..06e8175 100644 --- a/app/html/css/styles.css +++ b/app/html/css/styles.css @@ -186,3 +186,18 @@ button { small { font-size:.7em; } + #holder { + border: 4px dashed #ccc; + margin: 0 auto; + height: 100px; + color: #ccc; + font-size: 40px; + line-height: 100px; + text-align: center; + -webkit-user-select: none; + } + #holder.hover { + border: 4px dashed #999; + color: #eee; + } + diff --git a/app/html/index.html b/app/html/index.html index b32c823..48fd755 100644 --- a/app/html/index.html +++ b/app/html/index.html @@ -31,6 +31,9 @@
+
+ Drag your app here to run it +
diff --git a/app/html/js/handlers.js b/app/html/js/handlers.js index 4fe1c82..f674e5a 100644 --- a/app/html/js/handlers.js +++ b/app/html/js/handlers.js @@ -6,10 +6,14 @@ var __process__ = {}; if(typeof(require) === 'function') { var ipc = require('ipc'); + var path = require('path'); + var os = require('os'); ipc.on('files', function(files) { + console.log('--files',files); __process__.files = files; }); ipc.on('preview', function(files) { + console.log('--preview',files); handlers.renderPreview(files); }); ipc.on('update-import-success', function(args) { @@ -32,7 +36,11 @@ if(typeof(require) === 'function') { } }); ipc.on('update-photos-success', function(args) { - var response = JSON.parse(args['stdout']); + if(os.platform() == 'win32'){ + var response = JSON.parse(args['stdout'].replace(/\\/g, '\\\\')); + }else{ + var response = JSON.parse(args['stdout']); + } handlers.setSuccessTitle(); handlers.removeProgressIcons(); handlers.updateStatus(response); @@ -40,10 +48,42 @@ if(typeof(require) === 'function') { function Broadcast() { this.send = function(name, message) { + console.log(message); + console.log(name); + console.log('broadcast ',message); ipc.send(name, message); }; } -} + + window.onload = function () { + var broadcast = new Broadcast(); + window.ondragover = function (e){ e.preventDefault(); return false }; + window.ondragover = function (e){ e.preventDefault(); return false }; + var holder = document.getElementById('holder'); + if(holder != null){ + holder.ondragover = function () { this.className = 'hover'; return false; }; + holder.ondragleave = function () { this.className = ''; return false; }; + holder.ondrop = function (e) { + e.preventDefault(); + files = [] + for (var i = 0; i < e.dataTransfer.files.length; ++i) { + console.log(e.dataTransfer.files[i].path); + files.push(e.dataTransfer.files[i].path); + } + console.log('files=',e.dataTransfer); + msg = {}; + //msg['files'] = ['f1','f2','f3']; + msg['files'] = e.dataTransfer.files; + console.log('handlers:',msg['files'].length); + //broadcast.send('load-update-photos', msg); + broadcast.send('load-update-photos', files); + return false; + }; + } + }; + +}; + function Handlers() { var self = this; @@ -161,7 +201,9 @@ function Handlers() { html = ''; for(var i=0; i'; + console.log("preview:",fileUrl(files[i])); + html += '
'; + console.log('html',html); } else { html += '
'; } @@ -200,6 +242,22 @@ function Handlers() { el.style.display = 'block'; } }; + + function fileUrl(str) { + if (typeof str !== 'string') { + throw new Error('Expected a string'); + } + + var pathName = path.resolve(str).replace(/\\/g, '/'); + + // Windows drive letter must be prefixed with a slash + if (pathName[0] !== '/') { + pathName = '/' + pathName; + } + + return encodeURI('file://' + pathName); +}; + } var handlers = new Handlers(); window.addEventListener('click', handlers.dispatch); diff --git a/app/index.js b/app/index.js index 787921b..6e74642 100644 --- a/app/index.js +++ b/app/index.js @@ -16,3 +16,4 @@ ipc.on('update-photos', broadcast.updatePhotos); ipc.on('launch-finder', broadcast.launchFinder); ipc.on('launch-url', broadcast.launchUrl); ipc.on('program-quit', broadcast.programQuit); +ipc.on('load-update-photos', toolbarUi.onDropFiles); \ No newline at end of file diff --git a/app/modules/broadcast.js b/app/modules/broadcast.js index 3f8ff72..9a95a22 100644 --- a/app/modules/broadcast.js +++ b/app/modules/broadcast.js @@ -1,5 +1,5 @@ var exports = module.exports = {}; - +var path = require('path'); var exec = require('child_process').exec, config = require('./config.js'); @@ -24,7 +24,7 @@ exports.importPhotos = function(event, args) { args['source'] = args['source'].normalize(); args['destination'] = args['destination'].normalize(); - update_command = __dirname + '/../../dist/elodie/elodie import --source="' + args['source'] + '" --destination="' + args['destination'] + '"'; + update_command = path.normalize(__dirname + '/../../elodie.py') + ' import --source="' + args['source'] + '" --destination="' + args['destination'] + '"'; //update_command = __dirname + '/../../elodie.py import --source="' + args['source'] + '" --destination="' + args['destination'] + '"'; console.log(update_command); @@ -73,8 +73,9 @@ exports.updatePhotos = function(event, args) { return files } files = normalize(args['files']) - - update_command = __dirname + '/../../dist/elodie/elodie update' + elodie_path = path.normalize(__dirname + '/../../elodie.py'); + console.log(elodie_path); + update_command = elodie_path +' update' //update_command = __dirname + '/../../elodie.py update' if(args['location'].length > 0) { update_command += ' --location="' + args['location'] + '"'; diff --git a/app/modules/config.js b/app/modules/config.js index c4150b7..3af2eb3 100644 --- a/app/modules/config.js +++ b/app/modules/config.js @@ -3,11 +3,11 @@ var fs = require('fs'), defaultConfigFile = (function() { var f = __dirname; for(var i=0; i<2; i++) { - f = f.substr(0, f.lastIndexOf('/')); + f = f.substr(0, f.lastIndexOf('\\')); } - return f + '/config.ini-sample'; + return f + '\\config.ini-sample'; })(), - configFile = (process.env.HOME || process.env.USERPROFILE) + '/.elodie/config.ini', + configFile = (process.env.HOME || process.env.USERPROFILE) + '\\.elodie\\config.ini', hasConfig, setConfig; diff --git a/app/modules/toolbar-ui.js b/app/modules/toolbar-ui.js index f32852b..f6b4af9 100644 --- a/app/modules/toolbar-ui.js +++ b/app/modules/toolbar-ui.js @@ -9,7 +9,7 @@ var menubar = require('menubar'), exports.app = app = menubar( { preloadWindow: true, - dir: __dirname.substr(0, __dirname.lastIndexOf('/')) + '/html', + dir: __dirname.substr(0, __dirname.lastIndexOf('\\')) + '\\html', index: 'index.html', pages: { 'blank': 'blank.html', @@ -18,7 +18,9 @@ exports.app = app = menubar( }, width: 400, height: 500, - 'window-position': 'trayCenter' + 'window-position': 'trayCenter', + 'frame': true, + 'always-on-top': true } ); @@ -59,6 +61,21 @@ exports.ready = function() { }); }; +exports.onDropFiles = function(event, args) { + console.log(args); + console.log('onDropFiles',args); + var files = args; + console.log('Hello',typeof(args)); + loadUrl = app.getOption('pages')['location']; + app.showWindow(); + + app.window.webContents.on('did-finish-load', function() { + app.window.webContents.send('files', files); + app.window.webContents.send('preview', files); + }); +}; + + exports.createWindow = function() { console.log('create-window') }; @@ -76,7 +93,7 @@ exports.show = function() { this.window.loadUrl('file://' + this.getOption('dir') + '/' + loadUrl); loadUrl = null; - //app.window.openDevTools(); + app.window.openDevTools(); }; exports.afterShow = function() { diff --git a/elodie/constants.py b/elodie/constants.py index bc878d1..99c4a1b 100644 --- a/elodie/constants.py +++ b/elodie/constants.py @@ -5,7 +5,7 @@ Settings used by Elodie. from os import path #: If True, debug messages will be printed. -debug = True +debug = False #: Directory in which to store Elodie settings. application_directory = '{}/.elodie'.format(path.expanduser('~')) From 70724d59acd4ad1aa2e07f698581df4a077d99b8 Mon Sep 17 00:00:00 2001 From: zserg Date: Wed, 27 Jan 2016 22:37:29 +0300 Subject: [PATCH 5/9] Accepted-language is set to English to prevent Unocode issues in the location response from openmapquest --- elodie/geolocation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/elodie/geolocation.py b/elodie/geolocation.py index a615761..90da3e4 100644 --- a/elodie/geolocation.py +++ b/elodie/geolocation.py @@ -161,9 +161,10 @@ def reverse_lookup(lat, lon): try: params = {'format': 'json', 'key': key, 'lat': lat, 'lon': lon} + headers = {"Accept-Language": "en-US,en;q=0.5"} r = requests.get( 'http://open.mapquestapi.com/nominatim/v1/reverse.php?%s' % - urllib.urlencode(params) + urllib.urlencode(params),headers=headers ) return r.json() except requests.exceptions.RequestException as e: From 0055aa3a62c8066bfcdff0ac9c12eff12f333c1a Mon Sep 17 00:00:00 2001 From: zserg Date: Wed, 27 Jan 2016 22:38:08 +0300 Subject: [PATCH 6/9] gui app for windows fix --- app/html/css/styles.css | 15 --------------- app/html/index.html | 5 +---- app/html/js/handlers.js | 17 +---------------- app/html/location.html | 2 +- app/modules/broadcast.js | 5 ++--- app/modules/config.js | 7 ++++--- app/modules/toolbar-ui.js | 15 ++++++++------- 7 files changed, 17 insertions(+), 49 deletions(-) diff --git a/app/html/css/styles.css b/app/html/css/styles.css index 06e8175..9ead916 100644 --- a/app/html/css/styles.css +++ b/app/html/css/styles.css @@ -186,18 +186,3 @@ button { small { font-size:.7em; } - #holder { - border: 4px dashed #ccc; - margin: 0 auto; - height: 100px; - color: #ccc; - font-size: 40px; - line-height: 100px; - text-align: center; - -webkit-user-select: none; - } - #holder.hover { - border: 4px dashed #999; - color: #eee; - } - diff --git a/app/html/index.html b/app/html/index.html index 48fd755..52c8826 100644 --- a/app/html/index.html +++ b/app/html/index.html @@ -15,7 +15,7 @@ How can I help you? -- Elodie
-
+

Let me know where your photos are and where you'd like me to put them as I sort them.

@@ -31,9 +31,6 @@
-
- Drag your app here to run it -
diff --git a/app/html/js/handlers.js b/app/html/js/handlers.js index f674e5a..98374ca 100644 --- a/app/html/js/handlers.js +++ b/app/html/js/handlers.js @@ -9,11 +9,9 @@ if(typeof(require) === 'function') { var path = require('path'); var os = require('os'); ipc.on('files', function(files) { - console.log('--files',files); __process__.files = files; }); ipc.on('preview', function(files) { - console.log('--preview',files); handlers.renderPreview(files); }); ipc.on('update-import-success', function(args) { @@ -48,9 +46,6 @@ if(typeof(require) === 'function') { function Broadcast() { this.send = function(name, message) { - console.log(message); - console.log(name); - console.log('broadcast ',message); ipc.send(name, message); }; } @@ -59,10 +54,8 @@ if(typeof(require) === 'function') { var broadcast = new Broadcast(); window.ondragover = function (e){ e.preventDefault(); return false }; window.ondragover = function (e){ e.preventDefault(); return false }; - var holder = document.getElementById('holder'); + var holder = document.getElementById('content'); if(holder != null){ - holder.ondragover = function () { this.className = 'hover'; return false; }; - holder.ondragleave = function () { this.className = ''; return false; }; holder.ondrop = function (e) { e.preventDefault(); files = [] @@ -70,12 +63,6 @@ if(typeof(require) === 'function') { console.log(e.dataTransfer.files[i].path); files.push(e.dataTransfer.files[i].path); } - console.log('files=',e.dataTransfer); - msg = {}; - //msg['files'] = ['f1','f2','f3']; - msg['files'] = e.dataTransfer.files; - console.log('handlers:',msg['files'].length); - //broadcast.send('load-update-photos', msg); broadcast.send('load-update-photos', files); return false; }; @@ -201,9 +188,7 @@ function Handlers() { html = ''; for(var i=0; i'; - console.log('html',html); } else { html += '
'; } diff --git a/app/html/location.html b/app/html/location.html index 21037ff..a697e99 100644 --- a/app/html/location.html +++ b/app/html/location.html @@ -18,7 +18,7 @@
-
+
diff --git a/app/modules/broadcast.js b/app/modules/broadcast.js index 9a95a22..9953063 100644 --- a/app/modules/broadcast.js +++ b/app/modules/broadcast.js @@ -24,7 +24,7 @@ exports.importPhotos = function(event, args) { args['source'] = args['source'].normalize(); args['destination'] = args['destination'].normalize(); - update_command = path.normalize(__dirname + '/../../elodie.py') + ' import --source="' + args['source'] + '" --destination="' + args['destination'] + '"'; + update_command = path.normalize(__dirname + '/../../dist/elodie/elodie') + ' import --source="' + args['source'] + '" --destination="' + args['destination'] + '"'; //update_command = __dirname + '/../../elodie.py import --source="' + args['source'] + '" --destination="' + args['destination'] + '"'; console.log(update_command); @@ -73,8 +73,7 @@ exports.updatePhotos = function(event, args) { return files } files = normalize(args['files']) - elodie_path = path.normalize(__dirname + '/../../elodie.py'); - console.log(elodie_path); + elodie_path = path.normalize(__dirname + '/../../dist/elodie/elodie'); update_command = elodie_path +' update' //update_command = __dirname + '/../../elodie.py update' if(args['location'].length > 0) { diff --git a/app/modules/config.js b/app/modules/config.js index 3af2eb3..60d9064 100644 --- a/app/modules/config.js +++ b/app/modules/config.js @@ -1,13 +1,14 @@ var exports = module.exports = {}; var fs = require('fs'), + os = require('os'), defaultConfigFile = (function() { var f = __dirname; for(var i=0; i<2; i++) { - f = f.substr(0, f.lastIndexOf('\\')); + f = f.substr(0, f.lastIndexOf(os.platform() == 'win32' ? '\\' : '/')); } - return f + '\\config.ini-sample'; + return f + (os.platform() == 'win32' ? '\\config.ini-sample': '/config.ini-sample'); })(), - configFile = (process.env.HOME || process.env.USERPROFILE) + '\\.elodie\\config.ini', + configFile = (process.env.HOME || process.env.USERPROFILE) + (os.platform() == 'win32' ? '\\.elodie\\config.ini' : '/.elodie/config.ini'), hasConfig, setConfig; diff --git a/app/modules/toolbar-ui.js b/app/modules/toolbar-ui.js index f6b4af9..224e596 100644 --- a/app/modules/toolbar-ui.js +++ b/app/modules/toolbar-ui.js @@ -5,11 +5,15 @@ var menubar = require('menubar'), tray = require('tray'), config = require('./config.js'), loadUrl = null; +var os = require('os') +var s_dir = __dirname.substr(0,__dirname.lastIndexOf(os.platform() == 'win32' ? '\\' : '/')) + + (os.platform() == 'win32' ? '\\html' : '/html'); + exports.app = app = menubar( { preloadWindow: true, - dir: __dirname.substr(0, __dirname.lastIndexOf('\\')) + '\\html', + dir: s_dir, index: 'index.html', pages: { 'blank': 'blank.html', @@ -19,8 +23,8 @@ exports.app = app = menubar( width: 400, height: 500, 'window-position': 'trayCenter', - 'frame': true, - 'always-on-top': true + 'frame': os.platform() == 'win32' ? true : false, + 'always-on-top': os.platform() == 'win32' ? true : false } ); @@ -62,10 +66,7 @@ exports.ready = function() { }; exports.onDropFiles = function(event, args) { - console.log(args); - console.log('onDropFiles',args); var files = args; - console.log('Hello',typeof(args)); loadUrl = app.getOption('pages')['location']; app.showWindow(); @@ -93,7 +94,7 @@ exports.show = function() { this.window.loadUrl('file://' + this.getOption('dir') + '/' + loadUrl); loadUrl = null; - app.window.openDevTools(); + //app.window.openDevTools(); }; exports.afterShow = function() { From c9618311fe9bba10b3c7c00a312c410796797066 Mon Sep 17 00:00:00 2001 From: zserg Date: Thu, 28 Jan 2016 10:45:19 +0400 Subject: [PATCH 7/9] gui app: hint to drop files was added on start page --- app/html/index.html | 1 + 1 file changed, 1 insertion(+) diff --git a/app/html/index.html b/app/html/index.html index 52c8826..dd40e8b 100644 --- a/app/html/index.html +++ b/app/html/index.html @@ -18,6 +18,7 @@

Let me know where your photos are and where you'd like me to put them as I sort them. + (You can drop your photos here to update its information.)

From 0d154d67c9f3f6dbde822f6bedb646f0042d1d7d Mon Sep 17 00:00:00 2001 From: ZSerg Date: Thu, 28 Jan 2016 21:30:13 +0300 Subject: [PATCH 8/9] Accepted-Language is moved to constants. TZ fix functions is moved to helper --- elodie/constants.py | 3 +++ elodie/geolocation.py | 2 +- elodie/tests/filesystem_test.py | 30 ++++++++---------------- elodie/tests/helper.py | 39 ++++++++++++++++++++++++++++++++ elodie/tests/media/photo_test.py | 15 +++--------- 5 files changed, 55 insertions(+), 34 deletions(-) diff --git a/elodie/constants.py b/elodie/constants.py index 99c4a1b..0979381 100644 --- a/elodie/constants.py +++ b/elodie/constants.py @@ -21,3 +21,6 @@ script_directory = path.dirname(path.dirname(path.abspath(__file__))) #: Path to Elodie's ExifTool config file. exiftool_config = path.join(script_directory,'configs','ExifTool_config') + +#: Accepted language in responses from MapQuest +accepted_language = 'en' diff --git a/elodie/geolocation.py b/elodie/geolocation.py index 90da3e4..e9ea6a0 100644 --- a/elodie/geolocation.py +++ b/elodie/geolocation.py @@ -161,7 +161,7 @@ def reverse_lookup(lat, lon): try: params = {'format': 'json', 'key': key, 'lat': lat, 'lon': lon} - headers = {"Accept-Language": "en-US,en;q=0.5"} + headers = {"Accept-Language": constants.accepted_language} r = requests.get( 'http://open.mapquestapi.com/nominatim/v1/reverse.php?%s' % urllib.urlencode(params),headers=headers diff --git a/elodie/tests/filesystem_test.py b/elodie/tests/filesystem_test.py index 875b7b4..2c67112 100644 --- a/elodie/tests/filesystem_test.py +++ b/elodie/tests/filesystem_test.py @@ -19,18 +19,6 @@ from nose.plugins.skip import SkipTest os.environ['TZ'] = 'GMT' -if os.name == 'nt': - tz_shift = (datetime.fromtimestamp(0) - - datetime.utcfromtimestamp(0)).seconds/3600 -else: - tz_shift = 0 - -def path_tz_fix(s_path): - #some_prefix2015-12-05_00-59-26-with-title-some-title.jpg - m = re.search('(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})',s_path) - t_date = datetime.fromtimestamp(time.mktime(time.strptime(m.group(0), '%Y-%m-%d_%H-%M-%S'))) - s_date_fix = (t_date-timedelta(hours=tz_shift)).strftime('%Y-%m-%d_%H-%M-%S') - return re.sub('\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}',s_date_fix,s_path) def test_create_directory_success(): filesystem = FileSystem() @@ -144,14 +132,14 @@ def test_get_file_name_plain(): media = Photo(helper.get_file('plain.jpg')) file_name = filesystem.get_file_name(media) - assert file_name == path_tz_fix('2015-12-05_00-59-26-plain.jpg'), file_name + assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain.jpg'), file_name def test_get_file_name_with_title(): filesystem = FileSystem() media = Photo(helper.get_file('with-title.jpg')) file_name = filesystem.get_file_name(media) - assert file_name == path_tz_fix('2015-12-05_00-59-26-with-title-some-title.jpg'), file_name + assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-with-title-some-title.jpg'), file_name def test_get_folder_name_by_date(): filesystem = FileSystem() @@ -211,7 +199,7 @@ def test_process_file_plain(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo.jpg')) in destination, destination + assert helper.path_tz_fix(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() @@ -231,7 +219,7 @@ def test_process_file_with_title(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination + assert helper.path_tz_fix(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() @@ -251,7 +239,7 @@ def test_process_file_with_location(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo.jpg')) in destination, destination + assert helper.path_tz_fix(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() @@ -271,7 +259,7 @@ def test_process_file_with_location_and_title(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination + assert helper.path_tz_fix(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() @@ -291,7 +279,7 @@ def test_process_file_with_album(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo.jpg')) in destination, destination + assert helper.path_tz_fix(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() @@ -311,7 +299,7 @@ def test_process_file_with_album_and_title(): assert origin_checksum is not None, origin_checksum assert origin_checksum == destination_checksum, destination_checksum - assert path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination + assert helper.path_tz_fix(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() @@ -331,4 +319,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 path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination + assert helper.path_tz_fix(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 574241e..9959165 100644 --- a/elodie/tests/helper.py +++ b/elodie/tests/helper.py @@ -3,6 +3,11 @@ import os import random import string import tempfile +import re +import time + +from datetime import datetime +from datetime import timedelta def checksum(file_path, blocksize=65536): hasher = hashlib.sha256() @@ -53,3 +58,37 @@ def random_coordinate(coordinate, precision): def temp_dir(): return tempfile.gettempdir() + +def is_windows(): + return os.name == 'nt' + +# path_tz_fix(file_name) +# Change timestamp in file_name by the offset +# between UTC and local time, i.e. +# 2015-12-05_00-59-26-with-title-some-title.jpg -> +# 2015-12-04_20-59-26-with-title-some-title.jpg +# (Windows only) + +def path_tz_fix(file_name): + if is_windows(): + # Calculate the offset between UTC and local time + tz_shift = (datetime.fromtimestamp(0) - + datetime.utcfromtimestamp(0)).seconds/3600 + # replace timestamp in file_name + m = re.search('(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})',file_name) + t_date = datetime.fromtimestamp(time.mktime(time.strptime(m.group(0), '%Y-%m-%d_%H-%M-%S'))) + s_date_fix = (t_date-timedelta(hours=tz_shift)).strftime('%Y-%m-%d_%H-%M-%S') + return re.sub('\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}',s_date_fix,file_name) + else: + return file_name + +# time_convert(s_time) +# Change s_time (struct_time) by the offset +# between UTC and local time +# (Windows only) + +def time_convert(s_time): + if is_windows(): + return time.gmtime((time.mktime(s_time))) + else: + return s_time diff --git a/elodie/tests/media/photo_test.py b/elodie/tests/media/photo_test.py index eeb2e11..9ae5938 100644 --- a/elodie/tests/media/photo_test.py +++ b/elodie/tests/media/photo_test.py @@ -4,7 +4,6 @@ import os import sys from datetime import datetime -from datetime import timedelta import shutil import tempfile import time @@ -19,14 +18,6 @@ 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() @@ -105,7 +96,7 @@ def test_get_date_taken(): date_taken = photo.get_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 + assert date_taken == helper.time_convert((2015, 12, 5, 0, 59, 26, 5, 339, 0)), date_taken def test_get_date_taken_without_exif(): source = helper.get_file('no-exif.jpg') @@ -147,7 +138,7 @@ 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 time_convert(date_taken) == time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)) - timedelta(hours = tz_shift), metadata['date_taken'] + assert date_taken == helper.time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)), metadata['date_taken'] def test_set_date_taken(): temporary_folder, folder = helper.create_working_folder() @@ -168,7 +159,7 @@ def test_set_date_taken(): shutil.rmtree(folder) #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'] + assert date_taken == helper.time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)), metadata['date_taken'] def test_set_location(): raise SkipTest('gh-31, precision is lost in conversion from decimal to dms') From 5204253ad26d11bc0a6ac920add5fca9bf87d506 Mon Sep 17 00:00:00 2001 From: Jaisen Mathai Date: Thu, 11 Feb 2016 15:24:42 -0800 Subject: [PATCH 9/9] gh-82 Fix notifications from flake8 and add mock module to requirements.txt --- elodie/constants.py | 2 +- elodie/geolocation.py | 2 +- elodie/media/media.py | 2 +- requirements.txt | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/elodie/constants.py b/elodie/constants.py index 0979381..052eb45 100644 --- a/elodie/constants.py +++ b/elodie/constants.py @@ -20,7 +20,7 @@ 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 = path.join(script_directory,'configs','ExifTool_config') +exiftool_config = path.join(script_directory, 'configs', 'ExifTool_config') #: Accepted language in responses from MapQuest accepted_language = 'en' diff --git a/elodie/geolocation.py b/elodie/geolocation.py index e9ea6a0..75b003d 100644 --- a/elodie/geolocation.py +++ b/elodie/geolocation.py @@ -164,7 +164,7 @@ def reverse_lookup(lat, lon): headers = {"Accept-Language": constants.accepted_language} r = requests.get( 'http://open.mapquestapi.com/nominatim/v1/reverse.php?%s' % - urllib.urlencode(params),headers=headers + urllib.urlencode(params), headers=headers ) return r.json() except requests.exceptions.RequestException as e: diff --git a/elodie/media/media.py b/elodie/media/media.py index cb7971a..b98df36 100644 --- a/elodie/media/media.py +++ b/elodie/media/media.py @@ -226,7 +226,7 @@ class Media(object): 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, exiftool_config, name, source), stdout=subprocess.PIPE, shell=True ) diff --git a/requirements.txt b/requirements.txt index cfb3339..8305605 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ click>=6.2,<7.0 LatLon>=1.0.2,<2.0 requests>=2.9.1,<3.0 send2trash>=1.3.0,<2.0 +mock>=1.3.0