Fix logger
This commit is contained in:
parent
b9c30a4d85
commit
a72d0b0f2c
67
ordigi.py
67
ordigi.py
|
@ -3,7 +3,6 @@
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import logging
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
@ -35,22 +34,6 @@ def _batch(debug):
|
||||||
plugins.run_batch()
|
plugins.run_batch()
|
||||||
|
|
||||||
|
|
||||||
def get_logger(verbose, debug):
|
|
||||||
if debug:
|
|
||||||
level = logging.DEBUG
|
|
||||||
elif verbose:
|
|
||||||
level = logging.INFO
|
|
||||||
else:
|
|
||||||
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(level)
|
|
||||||
logger = logging.getLogger('ordigi')
|
|
||||||
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.')
|
||||||
|
@ -58,6 +41,8 @@ def get_logger(verbose, debug):
|
||||||
help='Dry run only, no change made to the filesystem.')
|
help='Dry run only, no change made to the filesystem.')
|
||||||
@click.option('--destination', '-d', type=click.Path(file_okay=False),
|
@click.option('--destination', '-d', type=click.Path(file_okay=False),
|
||||||
default=None, help='Sort files into this directory.')
|
default=None, help='Sort files into this directory.')
|
||||||
|
@click.option('--clean', '-C', default=False, is_flag=True,
|
||||||
|
help='Clean empty folders')
|
||||||
@click.option('--copy', '-c', default=False, is_flag=True,
|
@click.option('--copy', '-c', default=False, is_flag=True,
|
||||||
help='True if you want files to be copied over from src_dir to\
|
help='True if you want files to be copied over from src_dir to\
|
||||||
dest_dir rather than moved')
|
dest_dir rather than moved')
|
||||||
|
@ -80,7 +65,7 @@ def get_logger(verbose, debug):
|
||||||
@click.option('--verbose', '-v', default=False, is_flag=True,
|
@click.option('--verbose', '-v', default=False, is_flag=True,
|
||||||
help='True if you want to see details of file processing')
|
help='True if you want to see details of file processing')
|
||||||
@click.argument('paths', required=True, nargs=-1, type=click.Path())
|
@click.argument('paths', required=True, nargs=-1, type=click.Path())
|
||||||
def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignore_tags,
|
def _sort(debug, dry_run, destination, clean, copy, exclude_regex, filter_by_ext, ignore_tags,
|
||||||
max_deep, remove_duplicates, reset_cache, verbose, paths):
|
max_deep, remove_duplicates, reset_cache, verbose, paths):
|
||||||
"""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.
|
||||||
|
@ -91,7 +76,7 @@ def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignor
|
||||||
else:
|
else:
|
||||||
mode = 'move'
|
mode = 'move'
|
||||||
|
|
||||||
logger = get_logger(verbose, debug)
|
logger = log.get_logger(verbose, debug)
|
||||||
|
|
||||||
if max_deep is not None:
|
if max_deep is not None:
|
||||||
max_deep = int(max_deep)
|
max_deep = int(max_deep)
|
||||||
|
@ -107,6 +92,8 @@ def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignor
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
paths = set(paths)
|
paths = set(paths)
|
||||||
|
filter_by_ext = set(filter_by_ext)
|
||||||
|
|
||||||
destination = os.path.abspath(os.path.expanduser(destination))
|
destination = os.path.abspath(os.path.expanduser(destination))
|
||||||
|
|
||||||
if not os.path.exists(destination):
|
if not os.path.exists(destination):
|
||||||
|
@ -136,6 +123,9 @@ def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignor
|
||||||
summary, has_errors = filesystem.sort_files(paths, destination, db,
|
summary, has_errors = filesystem.sort_files(paths, destination, db,
|
||||||
remove_duplicates, ignore_tags)
|
remove_duplicates, ignore_tags)
|
||||||
|
|
||||||
|
if clean:
|
||||||
|
remove_empty_folders(destination, logger)
|
||||||
|
|
||||||
if verbose or debug:
|
if verbose or debug:
|
||||||
summary.write()
|
summary.write()
|
||||||
|
|
||||||
|
@ -143,6 +133,42 @@ def _sort(debug, dry_run, destination, copy, exclude_regex, filter_by_ext, ignor
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def remove_empty_folders(path, logger, remove_root=True):
|
||||||
|
'Function to remove empty folders'
|
||||||
|
if not os.path.isdir(path):
|
||||||
|
return
|
||||||
|
|
||||||
|
# remove empty subfolders
|
||||||
|
files = os.listdir(path)
|
||||||
|
if len(files):
|
||||||
|
for f in files:
|
||||||
|
fullpath = os.path.join(path, f)
|
||||||
|
if os.path.isdir(fullpath):
|
||||||
|
remove_empty_folders(fullpath, logger)
|
||||||
|
|
||||||
|
# if folder empty, delete it
|
||||||
|
files = os.listdir(path)
|
||||||
|
if len(files) == 0 and remove_root:
|
||||||
|
logger.info(f"Removing empty folder: {path}")
|
||||||
|
os.rmdir(path)
|
||||||
|
|
||||||
|
|
||||||
|
@click.command('clean')
|
||||||
|
@click.option('--debug', default=False, is_flag=True,
|
||||||
|
help='Override the value in constants.py with True.')
|
||||||
|
@click.option('--verbose', '-v', default=False, is_flag=True,
|
||||||
|
help='True if you want to see details of file processing')
|
||||||
|
@click.argument('path', required=True, nargs=1, type=click.Path())
|
||||||
|
def _clean(debug, verbose, path):
|
||||||
|
"""Remove empty folders
|
||||||
|
Usage: clean [--verbose|--debug] directory [removeRoot]"""
|
||||||
|
|
||||||
|
logger = log.get_logger(verbose, debug)
|
||||||
|
|
||||||
|
remove_empty_folders(path, logger)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@click.command('generate-db')
|
@click.command('generate-db')
|
||||||
@click.option('--path', type=click.Path(file_okay=False),
|
@click.option('--path', type=click.Path(file_okay=False),
|
||||||
required=True, help='Path of your photo library.')
|
required=True, help='Path of your photo library.')
|
||||||
|
@ -222,7 +248,7 @@ def _compare(debug, dry_run, find_duplicates, output_dir, remove_duplicates,
|
||||||
revert_compare, similar_to, similarity, verbose, path):
|
revert_compare, similar_to, similarity, verbose, path):
|
||||||
'''Compare files in directories'''
|
'''Compare files in directories'''
|
||||||
|
|
||||||
logger = get_logger(verbose, debug)
|
logger = log.get_logger(verbose, debug)
|
||||||
|
|
||||||
# Initialize Db
|
# Initialize Db
|
||||||
db = Db(path)
|
db = Db(path)
|
||||||
|
@ -248,6 +274,7 @@ def main():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
main.add_command(_clean)
|
||||||
main.add_command(_compare)
|
main.add_command(_compare)
|
||||||
main.add_command(_sort)
|
main.add_command(_sort)
|
||||||
main.add_command(_generate_db)
|
main.add_command(_generate_db)
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
|
def get_logger(verbose, debug):
|
||||||
|
if debug:
|
||||||
|
level = logging.DEBUG
|
||||||
|
elif verbose:
|
||||||
|
level = logging.INFO
|
||||||
|
else:
|
||||||
|
level = logging.WARNING
|
||||||
|
|
||||||
|
logging.basicConfig(format='%(levelname)s:%(message)s', level=level)
|
||||||
|
logging.getLogger('asyncio').setLevel(level)
|
||||||
|
logger = logging.getLogger('ordigi')
|
||||||
|
logger.level = level
|
||||||
|
return logger
|
||||||
|
|
Loading…
Reference in New Issue