Check if string is unicode before encoding (#180)

This commit is contained in:
Jaisen Mathai 2017-01-08 14:43:01 -08:00 committed by GitHub
parent 78880a6020
commit 2e0a59b7d8
2 changed files with 33 additions and 3 deletions

View File

@ -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

View File

@ -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()