Implement unicode-safe print function as part of the log module (#298)
This commit is contained in:
parent
2e2c103cfb
commit
283611a21e
27
elodie.py
27
elodie.py
|
@ -34,7 +34,7 @@ FILESYSTEM = FileSystem()
|
||||||
|
|
||||||
|
|
||||||
def import_file(_file, destination, album_from_folder, trash, allow_duplicates):
|
def import_file(_file, destination, album_from_folder, trash, allow_duplicates):
|
||||||
|
|
||||||
_file = _decode(_file)
|
_file = _decode(_file)
|
||||||
destination = _decode(destination)
|
destination = _decode(destination)
|
||||||
|
|
||||||
|
@ -42,19 +42,20 @@ def import_file(_file, destination, album_from_folder, trash, allow_duplicates):
|
||||||
"""
|
"""
|
||||||
if not os.path.exists(_file):
|
if not os.path.exists(_file):
|
||||||
log.warn('Could not find %s' % _file)
|
log.warn('Could not find %s' % _file)
|
||||||
print('{"source":"%s", "error_msg":"Could not find %s"}' % \
|
log.all('{"source":"%s", "error_msg":"Could not find %s"}' %
|
||||||
(_file, _file))
|
(_file, _file))
|
||||||
return
|
return
|
||||||
# Check if the source, _file, is a child folder within destination
|
# Check if the source, _file, is a child folder within destination
|
||||||
elif destination.startswith(os.path.abspath(os.path.dirname(_file))+os.sep):
|
elif destination.startswith(os.path.abspath(os.path.dirname(_file))+os.sep):
|
||||||
print('{"source": "%s", "destination": "%s", "error_msg": "Source cannot be in destination"}' % (_file, destination))
|
log.all('{"source": "%s", "destination": "%s", "error_msg": "Source cannot be in destination"}' % (
|
||||||
|
_file, destination))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
media = Media.get_class_by_file(_file, get_all_subclasses())
|
media = Media.get_class_by_file(_file, get_all_subclasses())
|
||||||
if not media:
|
if not media:
|
||||||
log.warn('Not a supported file (%s)' % _file)
|
log.warn('Not a supported file (%s)' % _file)
|
||||||
print('{"source":"%s", "error_msg":"Not a supported file"}' % _file)
|
log.all('{"source":"%s", "error_msg":"Not a supported file"}' % _file)
|
||||||
return
|
return
|
||||||
|
|
||||||
if album_from_folder:
|
if album_from_folder:
|
||||||
|
@ -63,7 +64,7 @@ def import_file(_file, destination, album_from_folder, trash, allow_duplicates):
|
||||||
dest_path = FILESYSTEM.process_file(_file, destination,
|
dest_path = FILESYSTEM.process_file(_file, destination,
|
||||||
media, allowDuplicate=allow_duplicates, move=False)
|
media, allowDuplicate=allow_duplicates, move=False)
|
||||||
if dest_path:
|
if dest_path:
|
||||||
print('%s -> %s' % (_file, dest_path))
|
log.all('%s -> %s' % (_file, dest_path))
|
||||||
if trash:
|
if trash:
|
||||||
send2trash(_file)
|
send2trash(_file)
|
||||||
|
|
||||||
|
@ -187,8 +188,8 @@ def update_location(media, file_path, location_name):
|
||||||
'latitude'], location_coords['longitude'])
|
'latitude'], location_coords['longitude'])
|
||||||
if not location_status:
|
if not location_status:
|
||||||
log.error('Failed to update location')
|
log.error('Failed to update location')
|
||||||
print(('{"source":"%s",' % file_path,
|
log.all(('{"source":"%s",' % file_path,
|
||||||
'"error_msg":"Failed to update location"}'))
|
'"error_msg":"Failed to update location"}'))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -202,7 +203,7 @@ def update_time(media, file_path, time_string):
|
||||||
elif re.match(r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}\d{2}$', time_string):
|
elif re.match(r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}\d{2}$', time_string):
|
||||||
msg = ('Invalid time format. Use YYYY-mm-dd hh:ii:ss or YYYY-mm-dd')
|
msg = ('Invalid time format. Use YYYY-mm-dd hh:ii:ss or YYYY-mm-dd')
|
||||||
log.error(msg)
|
log.error(msg)
|
||||||
print('{"source":"%s", "error_msg":"%s"}' % (file_path, msg))
|
log.all('{"source":"%s", "error_msg":"%s"}' % (file_path, msg))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
time = datetime.strptime(time_string, time_format)
|
time = datetime.strptime(time_string, time_format)
|
||||||
|
@ -242,8 +243,8 @@ def _update(album, location, time, title, paths, debug):
|
||||||
has_errors = True
|
has_errors = True
|
||||||
result.append((current_file, False))
|
result.append((current_file, False))
|
||||||
log.warn('Could not find %s' % current_file)
|
log.warn('Could not find %s' % current_file)
|
||||||
print('{"source":"%s", "error_msg":"Could not find %s"}' % \
|
log.all('{"source":"%s", "error_msg":"Could not find %s"}' %
|
||||||
(current_file, current_file))
|
(current_file, current_file))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
current_file = os.path.expanduser(current_file)
|
current_file = os.path.expanduser(current_file)
|
||||||
|
@ -310,8 +311,8 @@ def _update(album, location, time, title, paths, debug):
|
||||||
dest_path = FILESYSTEM.process_file(current_file, destination,
|
dest_path = FILESYSTEM.process_file(current_file, destination,
|
||||||
updated_media, move=True, allowDuplicate=True)
|
updated_media, move=True, allowDuplicate=True)
|
||||||
log.info(u'%s -> %s' % (current_file, dest_path))
|
log.info(u'%s -> %s' % (current_file, dest_path))
|
||||||
print('{"source":"%s", "destination":"%s"}' % (current_file,
|
log.all('{"source":"%s", "destination":"%s"}' % (current_file,
|
||||||
dest_path))
|
dest_path))
|
||||||
# If the folder we moved the file out of or its parent are empty
|
# If the folder we moved the file out of or its parent are empty
|
||||||
# we delete it.
|
# we delete it.
|
||||||
FILESYSTEM.delete_directory_if_empty(os.path.dirname(current_file))
|
FILESYSTEM.delete_directory_if_empty(os.path.dirname(current_file))
|
||||||
|
|
|
@ -9,13 +9,16 @@ from json import dumps
|
||||||
|
|
||||||
from elodie import constants
|
from elodie import constants
|
||||||
|
|
||||||
|
def all(message):
|
||||||
|
_print(message)
|
||||||
|
|
||||||
|
|
||||||
def info(message):
|
def info(message):
|
||||||
_print(message)
|
_print_debug(message)
|
||||||
|
|
||||||
|
|
||||||
def info_json(payload):
|
def info_json(payload):
|
||||||
_print(dumps(payload))
|
_print_debug(dumps(payload))
|
||||||
|
|
||||||
|
|
||||||
def progress(message='.', new_line=False):
|
def progress(message='.', new_line=False):
|
||||||
|
@ -26,21 +29,31 @@ def progress(message='.', new_line=False):
|
||||||
|
|
||||||
|
|
||||||
def warn(message):
|
def warn(message):
|
||||||
_print(message)
|
_print_debug(message)
|
||||||
|
|
||||||
|
|
||||||
def warn_json(payload):
|
def warn_json(payload):
|
||||||
_print(dumps(payload))
|
_print_debug(dumps(payload))
|
||||||
|
|
||||||
|
|
||||||
def error(message):
|
def error(message):
|
||||||
_print(message)
|
_print_debug(message)
|
||||||
|
|
||||||
|
|
||||||
def error_json(payload):
|
def error_json(payload):
|
||||||
_print(dumps(payload))
|
_print_debug(dumps(payload))
|
||||||
|
|
||||||
|
|
||||||
def _print(string):
|
def _print_debug(string):
|
||||||
if(constants.debug is True):
|
if(constants.debug is True):
|
||||||
print(string)
|
_print(string)
|
||||||
|
|
||||||
|
def _print(s):
|
||||||
|
try:
|
||||||
|
print(s)
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
for c in s:
|
||||||
|
try:
|
||||||
|
print(c, end='')
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
print('?', end='')
|
Loading…
Reference in New Issue