Allow ordigi.conf file per collection

This commit is contained in:
Cédric Leporcq 2021-10-17 20:04:39 +02:00
parent 513adb2890
commit c24014b98f
5 changed files with 37 additions and 27 deletions

View File

@ -79,6 +79,8 @@ def _get_exclude(opt, exclude):
exclude = opt['exclude'] exclude = opt['exclude']
return set(exclude) return set(exclude)
def get_collection_config(root):
return Config(os.path.join(root, '.ordigi', 'ordigi.conf'))
@click.command('sort') @click.command('sort')
@add_options(_logger_options) @add_options(_logger_options)
@ -158,7 +160,7 @@ def sort(**kwargs):
according to ordigi.conf preferences. according to ordigi.conf preferences.
""" """
destination = kwargs['destination'] root = kwargs['destination']
log_level = log.level(kwargs['verbose'], kwargs['debug']) log_level = log.level(kwargs['verbose'], kwargs['debug'])
paths = kwargs['paths'] paths = kwargs['paths']
@ -175,20 +177,20 @@ def sort(**kwargs):
cache = False cache = False
if len(paths) > 1: if len(paths) > 1:
if not destination: if not root:
# Use last path argument as destination # Use last path argument as destination
destination = paths[-1] root = paths[-1]
paths = paths[0:-1] paths = paths[0:-1]
elif paths: elif paths:
# Source and destination are the same # Source and destination are the same
destination = paths[0] root = paths[0]
else: else:
logger.error(f'`ordigi sort` need at least one path argument') logger.error(f'`ordigi sort` need at least one path argument')
sys.exit(1) sys.exit(1)
paths = set(paths) paths = set(paths)
config = Config(constants.CONFIG_FILE) config = get_collection_config(root)
opt = config.get_options() opt = config.get_options()
path_format = opt['path_format'] path_format = opt['path_format']
@ -199,7 +201,7 @@ def sort(**kwargs):
filter_by_ext = set(kwargs['filter_by_ext']) filter_by_ext = set(kwargs['filter_by_ext'])
collection = Collection( collection = Collection(
destination, root,
path_format, path_format,
kwargs['album_from_folder'], kwargs['album_from_folder'],
cache, cache,
@ -223,7 +225,7 @@ def sort(**kwargs):
) )
if kwargs['clean']: if kwargs['clean']:
collection.remove_empty_folders(destination) collection.remove_empty_folders(root)
if log_level < 30: if log_level < 30:
summary.print() summary.print()
@ -271,7 +273,6 @@ def clean(**kwargs):
"""Remove empty folders """Remove empty folders
Usage: clean [--verbose|--debug] directory [removeRoot]""" Usage: clean [--verbose|--debug] directory [removeRoot]"""
import ipdb; ipdb.set_trace()
result = True result = True
dry_run = kwargs['dry_run'] dry_run = kwargs['dry_run']
folders = kwargs['folders'] folders = kwargs['folders']
@ -287,7 +288,7 @@ def clean(**kwargs):
if not root: if not root:
root = path root = path
config = Config(constants.CONFIG_FILE) config = get_collection_config(root)
opt = config.get_options() opt = config.get_options()
exclude = _get_exclude(opt, kwargs['exclude']) exclude = _get_exclude(opt, kwargs['exclude'])
@ -329,13 +330,14 @@ def clean(**kwargs):
@click.argument('path', required=True, nargs=1, type=click.Path()) @click.argument('path', required=True, nargs=1, type=click.Path())
def init(**kwargs): def init(**kwargs):
"""Regenerate the hash.json database which contains all of the sha256 signatures of media files.""" """Regenerate the hash.json database which contains all of the sha256 signatures of media files."""
config = Config(constants.CONFIG_FILE) root = kwargs['path']
config = get_collection_config(root)
opt = config.get_options() opt = config.get_options()
log_level = log.level(kwargs['verbose'], kwargs['debug']) log_level = log.level(kwargs['verbose'], kwargs['debug'])
logger = log.get_logger(level=log_level) logger = log.get_logger(level=log_level)
loc = GeoLocation(opt['geocoder'], logger, opt['prefer_english_names'], opt['timeout']) loc = GeoLocation(opt['geocoder'], logger, opt['prefer_english_names'], opt['timeout'])
collection = Collection(kwargs['path'], None, exclude=opt['exclude'], mode='move', logger=logger) collection = Collection(root, None, exclude=opt['exclude'], mode='move', logger=logger)
summary = collection.init(loc) summary = collection.init(loc)
if log_level < 30: if log_level < 30:
@ -347,13 +349,14 @@ def init(**kwargs):
@click.argument('path', required=True, nargs=1, type=click.Path()) @click.argument('path', required=True, nargs=1, type=click.Path())
def update(**kwargs): def update(**kwargs):
"""Regenerate the hash.json database which contains all of the sha256 signatures of media files.""" """Regenerate the hash.json database which contains all of the sha256 signatures of media files."""
config = Config(constants.CONFIG_FILE) root = kwargs['path']
config = get_collection_config(root)
opt = config.get_options() opt = config.get_options()
log_level = log.level(kwargs['verbose'], kwargs['debug']) log_level = log.level(kwargs['verbose'], kwargs['debug'])
logger = log.get_logger(level=log_level) logger = log.get_logger(level=log_level)
loc = GeoLocation(opt['geocoder'], logger, opt['prefer_english_names'], opt['timeout']) loc = GeoLocation(opt['geocoder'], logger, opt['prefer_english_names'], opt['timeout'])
collection = Collection(kwargs['path'], None, exclude=opt['exclude'], mode='move', logger=logger) collection = Collection(root, None, exclude=opt['exclude'], mode='move', logger=logger)
summary = collection.update(loc) summary = collection.update(loc)
if log_level < 30: if log_level < 30:
@ -367,9 +370,10 @@ def check(**kwargs):
"""check db and verify hashes""" """check db and verify hashes"""
log_level = log.level(kwargs['verbose'], kwargs['debug']) log_level = log.level(kwargs['verbose'], kwargs['debug'])
logger = log.get_logger(level=log_level) logger = log.get_logger(level=log_level)
config = Config(constants.CONFIG_FILE) root = kwargs['path']
config = get_collection_config(root)
opt = config.get_options() opt = config.get_options()
collection = Collection(kwargs['path'], None, exclude=opt['exclude'], mode='move', logger=logger) collection = Collection(root, None, exclude=opt['exclude'], mode='move', logger=logger)
result = collection.check_db() result = collection.check_db()
if result: if result:
summary, result = collection.check_files() summary, result = collection.check_files()
@ -435,7 +439,7 @@ def compare(**kwargs):
if not root: if not root:
root = kwargs['path'] root = kwargs['path']
config = Config(constants.CONFIG_FILE) config = get_collection_config(root)
opt = config.get_options() opt = config.get_options()
exclude = _get_exclude(opt, kwargs['exclude']) exclude = _get_exclude(opt, kwargs['exclude'])

View File

@ -7,16 +7,20 @@ from geopy.geocoders import options as gopt
class Config: class Config:
"""Manage config file""" """Manage config file"""
def __init__(self, conf_path=None, conf={}): def __init__(self, conf_path=constants.CONFIG_FILE, conf={}):
self.conf_path = conf_path self.conf_path = conf_path
if conf_path == None: if conf == {}:
self.conf = conf
else:
self.conf = self.load_config() self.conf = self.load_config()
if self.conf == {}:
# Fallback to default config
self.conf_path = constants.CONFIG_FILE
self.conf = self.load_config()
else:
self.conf = conf
def write(self, conf): def write(self, conf):
with open(self.conf_path, 'w') as conf_path: with open(self.conf_path, 'w') as conf_file:
conf.write(conf_path) conf.write(conf_file)
return True return True
return False return False
@ -87,6 +91,7 @@ class Config:
if 'max_deep' in self.conf['Path']: if 'max_deep' in self.conf['Path']:
options['max_deep'] = int(self.conf['Path']['max_deep']) options['max_deep'] = int(self.conf['Path']['max_deep'])
options['exclude'] = []
if 'Exclusions' in self.conf: if 'Exclusions' in self.conf:
options['exclude'] = [value for key, value in self.conf.items('Exclusions')] options['exclude'] = [value for key, value in self.conf.items('Exclusions')]

View File

@ -20,4 +20,4 @@ default_path = '{%Y-%m-%b}/{album}|{city}'
default_name = '{%Y-%m-%d_%H-%M-%S}-{name}-{title}.%l{ext}' default_name = '{%Y-%m-%d_%H-%M-%S}-{name}-{title}.%l{ext}'
default_geocoder = 'Nominatim' default_geocoder = 'Nominatim'
CONFIG_FILE = f'{application_directory}/ordigi.conf' CONFIG_FILE = path.join(application_directory, 'ordigi.conf')

View File

@ -68,8 +68,8 @@ def conf_path():
'geocoder': 'Nominatium' 'geocoder': 'Nominatium'
} }
conf_path = Path(conf_dir, "ordigi.conf") conf_path = Path(conf_dir, "ordigi.conf")
config = Config(conf_path) with open(conf_path, 'w') as conf_file:
config.write(conf) conf.write(conf_file)
yield conf_path yield conf_path

View File

@ -39,8 +39,9 @@ class TestConfig:
def test_load_config_no_exist(self): def test_load_config_no_exist(self):
# test file not exist # test file not exist
config = Config('filename') config = Config()
assert config.conf == {} config.conf_path = 'filename'
assert config.load_config() == {}
def test_load_config_invalid(self, conf_path): def test_load_config_invalid(self, conf_path):
# test invalid config # test invalid config