2017-01-03 05:58:52 +01:00
|
|
|
"""Load config file as a singleton."""
|
|
|
|
from configparser import RawConfigParser
|
|
|
|
from os import path
|
2021-07-29 18:42:31 +02:00
|
|
|
from elodie import constants
|
2017-01-03 05:58:52 +01:00
|
|
|
|
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
def load_config(file):
|
2017-01-03 05:58:52 +01:00
|
|
|
if hasattr(load_config, "config"):
|
|
|
|
return load_config.config
|
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
if not path.exists(file):
|
2017-01-03 05:58:52 +01:00
|
|
|
return {}
|
|
|
|
|
|
|
|
load_config.config = RawConfigParser()
|
2021-04-16 20:02:14 +02:00
|
|
|
load_config.config.read(file)
|
2017-01-03 05:58:52 +01:00
|
|
|
return load_config.config
|
2019-07-04 11:57:10 +02:00
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
def load_plugin_config(file):
|
|
|
|
config = load_config(file)
|
2019-07-04 11:57:10 +02:00
|
|
|
|
|
|
|
# If plugins are defined in the config we return them as a list
|
|
|
|
# Else we return an empty list
|
|
|
|
if 'Plugins' in config and 'plugins' in config['Plugins']:
|
|
|
|
return config['Plugins']['plugins'].split(',')
|
|
|
|
|
|
|
|
return []
|
2019-07-12 09:40:38 +02:00
|
|
|
|
2021-04-16 20:02:14 +02:00
|
|
|
def load_config_for_plugin(name, file):
|
2019-07-12 09:40:38 +02:00
|
|
|
# Plugins store data using Plugin%PluginName% format.
|
|
|
|
key = 'Plugin{}'.format(name)
|
2021-04-16 20:02:14 +02:00
|
|
|
config = load_config(file)
|
2019-07-12 09:40:38 +02:00
|
|
|
|
|
|
|
if key in config:
|
|
|
|
return config[key]
|
|
|
|
|
|
|
|
return {}
|
2021-07-29 18:42:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_path_definition(config):
|
|
|
|
"""Returns a list of folder definitions.
|
|
|
|
|
|
|
|
Each element in the list represents a folder.
|
|
|
|
Fallback folders are supported and are nested lists.
|
|
|
|
|
|
|
|
:returns: string
|
|
|
|
"""
|
|
|
|
|
|
|
|
if 'Path' in config:
|
|
|
|
if 'format' in config['Path']:
|
|
|
|
return config['Path']['format']
|
|
|
|
elif 'dirs_path' and 'name' in config['Path']:
|
|
|
|
return path.join(config['Path']['dirs_path'],
|
|
|
|
config['Path']['name'])
|
|
|
|
|
|
|
|
return path.join(constants.default_path, constants.default_name)
|
|
|
|
|
|
|
|
|