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() 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.command('sort')
@click.option('--debug', default=False, is_flag=True, @click.option('--debug', default=False, is_flag=True,
help='Override the value in constants.py with 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: elif verbose:
constants.debug = logging.INFO constants.debug = logging.INFO
else: else:
constants.debug = logging.ERROR constants.debug = logging.WARNING
if max_deep is not None: if max_deep is not None:
max_deep = int(max_deep) max_deep = int(max_deep)
logger = logging.getLogger('dozo') logger = get_logger(constants.debug)
logger.setLevel(constants.debug)
cache = True cache = True
if reset_cache: if reset_cache:
@ -224,6 +232,8 @@ def _compare(debug, dry_run, find_duplicates, output_dir, remove_duplicates,
# Initialize Db # Initialize Db
db = Db(path) db = Db(path)
logger = get_logger(constants.debug)
filesystem = FileSystem(mode='move', dry_run=dry_run, logger=logger) filesystem = FileSystem(mode='move', dry_run=dry_run, logger=logger)
if revert_compare: if revert_compare:

View File

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

View File

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