Changeset View
Changeset View
Standalone View
Standalone View
swh/core/config.py
# Copyright (C) 2015 The Software Heritage developers | # Copyright (C) 2015 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import configparser | import configparser | ||||
import logging | |||||
import os | import os | ||||
import yaml | import yaml | ||||
SWH_CONFIG_DIRECTORIES = [ | SWH_CONFIG_DIRECTORIES = [ | ||||
'~/.config/swh', | '~/.config/swh', | ||||
'~/.swh', | '~/.swh', | ||||
'/etc/softwareheritage', | '/etc/softwareheritage', | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | |||||
def read_raw_config(base_config_path): | def read_raw_config(base_config_path): | ||||
"""Read the raw config corresponding to base_config_path. | """Read the raw config corresponding to base_config_path. | ||||
Can read yml or ini files. | Can read yml or ini files. | ||||
""" | """ | ||||
yml_file = base_config_path + '.yml' | yml_file = base_config_path + '.yml' | ||||
if exists_accessible(yml_file): | if exists_accessible(yml_file): | ||||
logging.debug('Using config file %s', yml_file) | |||||
with open(yml_file) as f: | with open(yml_file) as f: | ||||
return yaml.safe_load(f) | return yaml.safe_load(f) | ||||
ini_file = base_config_path + '.ini' | ini_file = base_config_path + '.ini' | ||||
if exists_accessible(ini_file): | if exists_accessible(ini_file): | ||||
config = configparser.ConfigParser() | config = configparser.ConfigParser() | ||||
config.read(ini_file) | config.read(ini_file) | ||||
if 'main' in config._sections: | if 'main' in config._sections: | ||||
logging.debug('Using config file %s', ini_file) | |||||
return config._sections['main'] | return config._sections['main'] | ||||
else: | |||||
logging.debug('Ignoring config file %s (no [main])', ini_file) | |||||
olasd: That's an info at best, as it'd log an error message if a config file voluntarily doesn't exist… | |||||
return {} | return {} | ||||
def config_exists(config_path): | def config_exists(config_path): | ||||
"""Check whether the given config exists""" | """Check whether the given config exists""" | ||||
basepath = config_basepath(config_path) | basepath = config_basepath(config_path) | ||||
return any(exists_accessible(basepath + extension) | return any(exists_accessible(basepath + extension) | ||||
for extension in SWH_CONFIG_EXTENSIONS) | for extension in SWH_CONFIG_EXTENSIONS) | ||||
▲ Show 20 Lines • Show All 172 Lines • Show Last 20 Lines |
That's an info at best, as it'd log an error message if a config file voluntarily doesn't exist (e.g. during tests).
exists_accessible already raises relevant errors when a file is present but inaccessible.