Change command line options for log levels

This commit is contained in:
Cédric Leporcq 2022-08-28 13:46:26 +02:00
parent b7435c4eac
commit 836792429f
3 changed files with 50 additions and 35 deletions

View File

@ -12,11 +12,26 @@ from ordigi.geolocation import GeoLocation
from ordigi import utils from ordigi import utils
_logger_options = [ _logger_options = [
click.option(
'--quiet',
'-q',
default=False,
is_flag=True,
help='Log level set to ERROR',
),
click.option( click.option(
'--verbose', '--verbose',
'-v', '-v',
default='WARNING', default=False,
help='Log level [WARNING,INFO,DEBUG,NOTSET]', is_flag=True,
help='Log level set to INFO',
),
click.option(
'--debug',
'-d',
default=False,
is_flag=True,
help='Log level set to DEBUG',
), ),
] ]
@ -168,7 +183,7 @@ def _check(**kwargs):
""" """
root = Path(kwargs['path']).expanduser().absolute() root = Path(kwargs['path']).expanduser().absolute()
log_level = log.get_level(kwargs['verbose']) log_level = log.get_level(kwargs['quiet'], kwargs['verbose'], kwargs['debug'])
log.console(LOG, level=log_level) log.console(LOG, level=log_level)
collection = Collection(root) collection = Collection(root)
@ -191,7 +206,7 @@ def _check(**kwargs):
@add_options(_filter_options) @add_options(_filter_options)
@click.option( @click.option(
'--dedup-regex', '--dedup-regex',
'-d', '-D',
default=None, default=None,
multiple=True, multiple=True,
help='Regex to match duplicate strings parts', help='Regex to match duplicate strings parts',
@ -218,7 +233,7 @@ def _clean(**kwargs):
"""Clean media collection""" """Clean media collection"""
folders = kwargs['folders'] folders = kwargs['folders']
log_level = log.get_level(kwargs['verbose']) log_level = log.get_level(kwargs['quiet'], kwargs['verbose'], kwargs['debug'])
log.console(LOG, level=log_level) log.console(LOG, level=log_level)
subdirs = kwargs['subdirs'] subdirs = kwargs['subdirs']
@ -268,7 +283,7 @@ def _clean(**kwargs):
def _clone(**kwargs): def _clone(**kwargs):
"""Clone media collection to another location""" """Clone media collection to another location"""
log_level = log.get_level(kwargs['verbose']) log_level = log.get_level(kwargs['quiet'], kwargs['verbose'], kwargs['debug'])
log.console(LOG, level=log_level) log.console(LOG, level=log_level)
src_path = Path(kwargs['src']).expanduser().absolute() src_path = Path(kwargs['src']).expanduser().absolute()
@ -321,7 +336,7 @@ def _compare(**kwargs):
subdirs = kwargs['subdirs'] subdirs = kwargs['subdirs']
root = kwargs['collection'] root = kwargs['collection']
log_level = log.get_level(kwargs['verbose']) log_level = log.get_level(kwargs['quiet'], kwargs['verbose'], kwargs['debug'])
log.console(LOG, level=log_level) log.console(LOG, level=log_level)
paths, root = _get_paths(subdirs, root) paths, root = _get_paths(subdirs, root)
@ -370,7 +385,7 @@ def _compare(**kwargs):
def _edit(**kwargs): def _edit(**kwargs):
"""Edit EXIF metadata in files or directories""" """Edit EXIF metadata in files or directories"""
log_level = log.get_level(kwargs['verbose']) log_level = log.get_level(kwargs['quiet'], kwargs['verbose'], kwargs['debug'])
log.console(LOG, level=log_level) log.console(LOG, level=log_level)
paths, root = _get_paths(kwargs['subdirs'], kwargs['path']) paths, root = _get_paths(kwargs['subdirs'], kwargs['path'])
@ -454,7 +469,7 @@ def _init(**kwargs):
Init media collection database. Init media collection database.
""" """
root = Path(kwargs['path']).expanduser().absolute() root = Path(kwargs['path']).expanduser().absolute()
log_level = log.get_level(kwargs['verbose']) log_level = log.get_level(kwargs['quiet'], kwargs['verbose'], kwargs['debug'])
log.console(LOG, level=log_level) log.console(LOG, level=log_level)
collection = Collection(root) collection = Collection(root)
@ -491,7 +506,7 @@ def _import(**kwargs):
"""Sort files or directories by reading their EXIF and organizing them """Sort files or directories by reading their EXIF and organizing them
according to ordigi.conf preferences. according to ordigi.conf preferences.
""" """
log_level = log.get_level(kwargs['verbose']) log_level = log.get_level(kwargs['quiet'], kwargs['verbose'], kwargs['debug'])
log.console(LOG, level=log_level) log.console(LOG, level=log_level)
src_paths, root = _get_paths(kwargs['src'], kwargs['dest']) src_paths, root = _get_paths(kwargs['src'], kwargs['dest'])
@ -547,7 +562,7 @@ def _sort(**kwargs):
"""Sort files or directories by reading their EXIF and organizing them """Sort files or directories by reading their EXIF and organizing them
according to ordigi.conf preferences. according to ordigi.conf preferences.
""" """
log_level = log.get_level(kwargs['verbose']) log_level = log.get_level(kwargs['quiet'], kwargs['verbose'], kwargs['debug'])
log.console(LOG, level=log_level) log.console(LOG, level=log_level)
paths, root = _get_paths(kwargs['subdirs'], kwargs['dest']) paths, root = _get_paths(kwargs['subdirs'], kwargs['dest'])
@ -598,7 +613,7 @@ def _update(**kwargs):
Update media collection database. Update media collection database.
""" """
root = Path(kwargs['path']).expanduser().absolute() root = Path(kwargs['path']).expanduser().absolute()
log_level = log.get_level(kwargs['verbose']) log_level = log.get_level(kwargs['quiet'], kwargs['verbose'], kwargs['debug'])
log.console(LOG, level=log_level) log.console(LOG, level=log_level)
collection = Collection(root) collection = Collection(root)

View File

@ -46,9 +46,16 @@ def file_logger(logger, file, level=30):
logger.addHandler(handler) logger.addHandler(handler)
def get_level(verbose): def get_level(quiet=False, verbose=False, debug=False, num=None):
"""Return int logging level from string""" """Return int logging level from command line args"""
if verbose.isnumeric(): if num and num.isnumeric():
return int(verbose) return int(verbose)
return int(logging.getLevelName(verbose)) if debug:
return int(logging.getLevelName('DEBUG'))
if verbose:
return int(logging.getLevelName('INFO'))
if quiet:
return int(logging.getLevelName('ERROR'))
return int(logging.getLevelName('WARNING'))

View File

@ -27,7 +27,7 @@ class TestOrdigi:
def setup_class(cls, sample_files_paths): def setup_class(cls, sample_files_paths):
cls.runner = CliRunner() cls.runner = CliRunner()
cls.src_path, cls.file_paths = sample_files_paths cls.src_path, cls.file_paths = sample_files_paths
cls.logger_options = (('--verbose', 'DEBUG'),) cls.logger_options = ('--debug',)
cls.filter_options = ( cls.filter_options = (
('--ignore-tags', 'CreateDate'), ('--ignore-tags', 'CreateDate'),
('--ext', 'jpg'), ('--ext', 'jpg'),
@ -82,10 +82,11 @@ class TestOrdigi:
def test_edit(self, monkeypatch): def test_edit(self, monkeypatch):
bool_options = () bool_options = (
*self.logger_options,
)
arg_options = ( arg_options = (
*self.logger_options,
*self.filter_options, *self.filter_options,
) )
@ -109,6 +110,7 @@ class TestOrdigi:
def test_sort(self): def test_sort(self):
bool_options = ( bool_options = (
*self.logger_options,
# '--interactive', # '--interactive',
'--dry-run', '--dry-run',
'--album-from-folder', '--album-from-folder',
@ -119,7 +121,6 @@ class TestOrdigi:
) )
arg_options = ( arg_options = (
*self.logger_options,
*self.filter_options, *self.filter_options,
('--path-format', '{%Y}/{folder}/{name}.{ext}'), ('--path-format', '{%Y}/{folder}/{name}.{ext}'),
@ -134,36 +135,29 @@ class TestOrdigi:
def test_clone(self, tmp_path): def test_clone(self, tmp_path):
arg_options = (
*self.logger_options,
)
paths = (str(self.src_path), str(tmp_path)) paths = (str(self.src_path), str(tmp_path))
self.assert_cli(cli._init, [str(self.src_path)]) self.assert_cli(cli._init, [str(self.src_path)])
self.assert_cli(cli._clone, ['--dry-run', '--verbose', 'DEBUG', *paths]) self.assert_cli(cli._clone, ['--dry-run', *self.logger_options, *paths])
self.assert_cli(cli._clone, paths) self.assert_cli(cli._clone, paths)
def assert_init(self): def assert_init(self):
for opt, arg in self.logger_options: self.assert_cli(cli._init, [*self.logger_options, str(self.src_path)])
self.assert_cli(cli._init, [opt, arg, str(self.src_path)])
def assert_update(self): def assert_update(self):
file_path = Path(ORDIGI_PATH, 'samples/test_exif/photo.cr2') file_path = Path(ORDIGI_PATH, 'samples/test_exif/photo.cr2')
dest_path = self.src_path / 'photo_moved.cr2' dest_path = self.src_path / 'photo_moved.cr2'
shutil.copyfile(file_path, dest_path) shutil.copyfile(file_path, dest_path)
for opt, arg in self.logger_options: self.assert_cli(cli._update, [*self.logger_options, str(self.src_path)])
self.assert_cli(cli._update, [opt, arg, str(self.src_path)])
self.assert_cli(cli._update, ['--checksum', str(self.src_path)]) self.assert_cli(cli._update, ['--checksum', str(self.src_path)])
def assert_check(self): def assert_check(self):
for opt, arg in self.logger_options: self.assert_cli(cli._check, [*self.logger_options, str(self.src_path)])
self.assert_cli(cli._check, [opt, arg, str(self.src_path)])
def assert_clean(self): def assert_clean(self):
bool_options = ( bool_options = (
*self.logger_options,
# '--interactive', # '--interactive',
'--dry-run', '--dry-run',
'--delete-excluded', '--delete-excluded',
@ -173,7 +167,6 @@ class TestOrdigi:
) )
arg_options = ( arg_options = (
*self.logger_options,
*self.filter_options, *self.filter_options,
('--dedup-regex', r'\d{4}-\d{2}'), ('--dedup-regex', r'\d{4}-\d{2}'),
) )
@ -194,6 +187,7 @@ class TestOrdigi:
def test_import(self, tmp_path): def test_import(self, tmp_path):
bool_options = ( bool_options = (
*self.logger_options,
# '--interactive', # '--interactive',
'--dry-run', '--dry-run',
'--album-from-folder', '--album-from-folder',
@ -204,7 +198,6 @@ class TestOrdigi:
) )
arg_options = ( arg_options = (
*self.logger_options,
('--exclude', '.DS_Store'), ('--exclude', '.DS_Store'),
*self.filter_options, *self.filter_options,
('--path-format', '{%Y}/{folder}/{stem}.{ext}'), ('--path-format', '{%Y}/{folder}/{stem}.{ext}'),
@ -220,6 +213,7 @@ class TestOrdigi:
def test_compare(self): def test_compare(self):
bool_options = ( bool_options = (
*self.logger_options,
# '--interactive', # '--interactive',
'--dry-run', '--dry-run',
'--find-duplicates', '--find-duplicates',
@ -227,7 +221,6 @@ class TestOrdigi:
) )
arg_options = ( arg_options = (
*self.logger_options,
*self.filter_options, *self.filter_options,
# ('--similar-to', ''), # ('--similar-to', ''),
('--similarity', '65'), ('--similarity', '65'),