Fix logger

This commit is contained in:
Cédric Leporcq 2021-08-08 13:02:15 +02:00
parent bfada5e740
commit 62bd72c77e
3 changed files with 29 additions and 17 deletions

16
dozo.py
View File

@ -34,6 +34,15 @@ def _batch(debug):
plugins.run_batch()
def get_logger(level=logging.WARNING):
logging.basicConfig(format='%(levelname)s:%(message)s', level=level)
logging.debug('This message should appear on the console')
logging.info('So should this')
logging.getLogger('asyncio').setLevel(logging.WARNING)
logger = logging.getLogger('dozo')
logger.level = level
return logger
@click.command('sort')
@click.option('--debug', default=False, is_flag=True,
help='Override the value in constants.py with True.')
@ -79,13 +88,12 @@ def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignor
elif verbose:
constants.debug = logging.INFO
else:
constants.debug = logging.ERROR
constants.debug = logging.WARNING
if max_deep is not None:
max_deep = int(max_deep)
logger = logging.getLogger('dozo')
logger.setLevel(constants.debug)
logger = get_logger(constants.debug)
cache = True
if reset_cache:
@ -224,6 +232,8 @@ def _compare(debug, dry_run, find_duplicates, output_dir, remove_duplicates,
# Initialize Db
db = Db(path)
logger = get_logger(constants.debug)
filesystem = FileSystem(mode='move', dry_run=dry_run, logger=logger)
if revert_compare:

View File

@ -388,13 +388,13 @@ class FileSystem(object):
if metadata['date_original'] is not None:
if (date_filename is not None and
date_filename != date_original):
log.warn(f"{basename} time mark is different from {date_original}")
self.logger.warn(f"{basename} time mark is different from {date_original}")
# TODO ask for keep date taken, filename time, or neither
return metadata['date_original']
elif True:
if date_filename is not None:
if date_created is not None and date_filename > date_created:
log.warn(f"{basename} time mark is more recent than {date_created}")
self.logger.warn(f"{basename} time mark is more recent than {date_created}")
return date_filename
if True:
if date_created is not None:
@ -568,7 +568,7 @@ class FileSystem(object):
conflict_file_list = set()
for src_path, subdirs in self.get_files_in_path(path):
# Process files
media = get_media_class(src_path, ignore_tags)
media = get_media_class(src_path, ignore_tags, self.logger)
if media:
metadata = media.get_metadata()
# Get the destination path according to metadata
@ -685,7 +685,7 @@ class FileSystem(object):
continue
checksum1 = db.checksum(image)
# Process files
# media = get_media_class(src_path, False)
# media = get_media_class(src_path, False, self.logger)
# TODO compare metadata
# if media:
# metadata = media.get_metadata()

View File

@ -35,7 +35,7 @@ class Media():
extensions = PHOTO + AUDIO + VIDEO
def __init__(self, sources=None, ignore_tags=set()):
def __init__(self, sources=None, ignore_tags=set(), logger=logging.getLogger()):
self.source = sources
self.reset_cache()
self.date_original = [
@ -74,6 +74,7 @@ class Media():
self.metadata = None
self.exif_metadata = None
self.ignore_tags = ignore_tags
self.logger = logger
def format_metadata(self, **kwargs):
@ -212,7 +213,7 @@ class Media():
@classmethod
def get_class_by_file(cls, _file, classes, ignore_tags=set()):
def get_class_by_file(cls, _file, classes, ignore_tags=set(), logger=logging.getLogger()):
"""Static method to get a media object by file.
"""
basestring = (bytes, str)
@ -230,7 +231,7 @@ class Media():
if os.path.basename(_file) == '.DS_Store':
return None
else:
return Media(_file, ignore_tags=ignore_tags)
return Media(_file, ignore_tags=ignore_tags, logger=logger)
@classmethod
@ -362,7 +363,7 @@ class Media():
# dt_list = map(int, dt_list)
# return datetime(*dt_list)
except BaseException or dateutil.parser._parser.ParserError as e:
log.error(e)
self.logger.error(e)
return None
return None
@ -604,16 +605,17 @@ def get_all_subclasses(cls=None):
return subclasses
def get_media_class(_file, ignore_tags=set()):
def get_media_class(_file, ignore_tags=set(), logger=logging.getLogger()):
if not os.path.exists(_file):
logging.warning(f'Could not find {_file}')
logging.error(f'Could not find {_file}')
logger.warning(f'Could not find {_file}')
logger.error(f'Could not find {_file}')
return False
media = Media.get_class_by_file(_file, get_all_subclasses(), ignore_tags=set())
media = Media.get_class_by_file(_file, get_all_subclasses(),
ignore_tags=set(), logger=logger)
if not media:
logging.warning(f'File{_file} is not supported')
logging.error(f'File {_file} can\'t be imported')
logger.warning(f'File{_file} is not supported')
logger.error(f'File {_file} can\'t be imported')
return False
return media