diff --git a/elodie/compatability.py b/elodie/compatability.py index 41e822a..424d4a4 100644 --- a/elodie/compatability.py +++ b/elodie/compatability.py @@ -1,5 +1,18 @@ def _decode(string, encoding='utf8'): + """Return a utf8 encoded unicode string. + + Python2 and Python3 differ in how they handle strings. + So we do a few checks to see if the string is ascii or unicode. + Then we decode it if needed. + """ if hasattr(string, 'decode'): + # If the string is already unicode we return it. + try: + if isinstance(string, unicode): + return string + except NameError: + pass + return string.decode(encoding) return string diff --git a/elodie/tests/elodie_test.py b/elodie/tests/elodie_test.py index 327eb33..0dc81bc 100644 --- a/elodie/tests/elodie_test.py +++ b/elodie/tests/elodie_test.py @@ -87,12 +87,12 @@ def test_import_file_video(): assert helper.path_tz_fix(os.path.join('2015-01-Jan','California','2015-01-19_12-45-11-video.mov')) in dest_path, dest_path -def test_import_file_path_unicode(): - raise SkipTest("Temporarily skipping unicode test. sh-167") +def test_import_file_path_utf8_encoded_ascii(): temporary_folder, folder = helper.create_working_folder() temporary_folder_destination, folder_destination = helper.create_working_folder() origin = text_type(folder)+u'/unicode'+six_unichr(160)+u'filename.txt' + # encode the unicode string to ascii origin = origin.encode('utf-8') shutil.copyfile(helper.get_file('valid.txt'), origin) @@ -104,7 +104,24 @@ def test_import_file_path_unicode(): shutil.rmtree(folder) shutil.rmtree(folder_destination) - assert helper.path_tz_fix(os.path.join('2016-04-Apr','Unknown Location',u'2016-04-07_11-15-26-unicode\xa0filename-sample-title.txt')) in dest_path, dest_path + assert helper.path_tz_fix(os.path.join('2016-04-Apr','London',u'2016-04-07_11-15-26-unicode\xa0filename-sample-title.txt')) in dest_path, dest_path + +def test_import_file_path_unicode(): + temporary_folder, folder = helper.create_working_folder() + temporary_folder_destination, folder_destination = helper.create_working_folder() + + origin = text_type(folder)+u'/unicode'+six_unichr(160)+u'filename.txt' + + shutil.copyfile(helper.get_file('valid.txt'), origin) + + helper.reset_dbs() + dest_path = elodie.import_file(origin, folder_destination, False, False, False) + helper.restore_dbs() + + shutil.rmtree(folder) + shutil.rmtree(folder_destination) + + assert helper.path_tz_fix(os.path.join('2016-04-Apr','London',u'2016-04-07_11-15-26-unicode\xa0filename-sample-title.txt')) in dest_path, dest_path def test_import_file_allow_duplicate_false(): temporary_folder, folder = helper.create_working_folder()