diff --git a/ordigi.py b/ordigi.py index 090e31d..6a0cf68 100755 --- a/ordigi.py +++ b/ordigi.py @@ -79,6 +79,8 @@ def _get_exclude(opt, exclude): exclude = opt['exclude'] return set(exclude) +def get_collection_config(root): + return Config(os.path.join(root, '.ordigi', 'ordigi.conf')) @click.command('sort') @add_options(_logger_options) @@ -158,7 +160,7 @@ def sort(**kwargs): according to ordigi.conf preferences. """ - destination = kwargs['destination'] + root = kwargs['destination'] log_level = log.level(kwargs['verbose'], kwargs['debug']) paths = kwargs['paths'] @@ -175,20 +177,20 @@ def sort(**kwargs): cache = False if len(paths) > 1: - if not destination: + if not root: # Use last path argument as destination - destination = paths[-1] + root = paths[-1] paths = paths[0:-1] elif paths: # Source and destination are the same - destination = paths[0] + root = paths[0] else: logger.error(f'`ordigi sort` need at least one path argument') sys.exit(1) paths = set(paths) - config = Config(constants.CONFIG_FILE) + config = get_collection_config(root) opt = config.get_options() path_format = opt['path_format'] @@ -199,7 +201,7 @@ def sort(**kwargs): filter_by_ext = set(kwargs['filter_by_ext']) collection = Collection( - destination, + root, path_format, kwargs['album_from_folder'], cache, @@ -223,7 +225,7 @@ def sort(**kwargs): ) if kwargs['clean']: - collection.remove_empty_folders(destination) + collection.remove_empty_folders(root) if log_level < 30: summary.print() @@ -271,7 +273,6 @@ def clean(**kwargs): """Remove empty folders Usage: clean [--verbose|--debug] directory [removeRoot]""" - import ipdb; ipdb.set_trace() result = True dry_run = kwargs['dry_run'] folders = kwargs['folders'] @@ -287,7 +288,7 @@ def clean(**kwargs): if not root: root = path - config = Config(constants.CONFIG_FILE) + config = get_collection_config(root) opt = config.get_options() exclude = _get_exclude(opt, kwargs['exclude']) @@ -329,13 +330,14 @@ def clean(**kwargs): @click.argument('path', required=True, nargs=1, type=click.Path()) def init(**kwargs): """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() log_level = log.level(kwargs['verbose'], kwargs['debug']) logger = log.get_logger(level=log_level) 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) if log_level < 30: @@ -347,13 +349,14 @@ def init(**kwargs): @click.argument('path', required=True, nargs=1, type=click.Path()) def update(**kwargs): """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() log_level = log.level(kwargs['verbose'], kwargs['debug']) logger = log.get_logger(level=log_level) 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) if log_level < 30: @@ -367,9 +370,10 @@ def check(**kwargs): """check db and verify hashes""" log_level = log.level(kwargs['verbose'], kwargs['debug']) logger = log.get_logger(level=log_level) - config = Config(constants.CONFIG_FILE) + root = kwargs['path'] + config = get_collection_config(root) 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() if result: summary, result = collection.check_files() @@ -435,7 +439,7 @@ def compare(**kwargs): if not root: root = kwargs['path'] - config = Config(constants.CONFIG_FILE) + config = get_collection_config(root) opt = config.get_options() exclude = _get_exclude(opt, kwargs['exclude']) diff --git a/ordigi/config.py b/ordigi/config.py index 9096f51..c471622 100644 --- a/ordigi/config.py +++ b/ordigi/config.py @@ -7,16 +7,20 @@ from geopy.geocoders import options as gopt class Config: """Manage config file""" - def __init__(self, conf_path=None, conf={}): + def __init__(self, conf_path=constants.CONFIG_FILE, conf={}): self.conf_path = conf_path - if conf_path == None: - self.conf = conf - else: + if conf == {}: 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): - with open(self.conf_path, 'w') as conf_path: - conf.write(conf_path) + with open(self.conf_path, 'w') as conf_file: + conf.write(conf_file) return True return False @@ -87,6 +91,7 @@ class Config: if 'max_deep' in self.conf['Path']: options['max_deep'] = int(self.conf['Path']['max_deep']) + options['exclude'] = [] if 'Exclusions' in self.conf: options['exclude'] = [value for key, value in self.conf.items('Exclusions')] diff --git a/ordigi/constants.py b/ordigi/constants.py index 15fc838..512e406 100644 --- a/ordigi/constants.py +++ b/ordigi/constants.py @@ -20,4 +20,4 @@ default_path = '{%Y-%m-%b}/{album}|{city}' default_name = '{%Y-%m-%d_%H-%M-%S}-{name}-{title}.%l{ext}' default_geocoder = 'Nominatim' -CONFIG_FILE = f'{application_directory}/ordigi.conf' +CONFIG_FILE = path.join(application_directory, 'ordigi.conf') diff --git a/tests/conftest.py b/tests/conftest.py index 6dff480..fbe6b52 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -68,8 +68,8 @@ def conf_path(): 'geocoder': 'Nominatium' } conf_path = Path(conf_dir, "ordigi.conf") - config = Config(conf_path) - config.write(conf) + with open(conf_path, 'w') as conf_file: + conf.write(conf_file) yield conf_path diff --git a/tests/test_config.py b/tests/test_config.py index 72f2f46..4e7cd36 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -39,8 +39,9 @@ class TestConfig: def test_load_config_no_exist(self): # test file not exist - config = Config('filename') - assert config.conf == {} + config = Config() + config.conf_path = 'filename' + assert config.load_config() == {} def test_load_config_invalid(self, conf_path): # test invalid config