2022-09-03 02:53:56 +12:00
|
|
|
from typing import Callable, Optional
|
2022-02-06 02:48:50 +13:00
|
|
|
|
|
|
|
from legendary.core import LegendaryCore
|
2022-10-27 08:57:04 +13:00
|
|
|
from legendary.models.config import LGDConf
|
2022-02-06 02:48:50 +13:00
|
|
|
|
2022-09-03 02:53:56 +12:00
|
|
|
_config: Optional[LGDConf] = None
|
|
|
|
_save_config: Optional[Callable[[], None]] = None
|
2022-02-06 02:48:50 +13:00
|
|
|
|
|
|
|
|
|
|
|
def init_config_handler(core: LegendaryCore):
|
2022-09-03 02:53:56 +12:00
|
|
|
global _config, _save_config
|
|
|
|
_config = core.lgd.config
|
|
|
|
_save_config = core.lgd.save_config
|
2022-02-06 02:48:50 +13:00
|
|
|
|
|
|
|
|
2022-09-05 08:49:43 +12:00
|
|
|
def save_config():
|
|
|
|
if _save_config is None:
|
|
|
|
raise RuntimeError("Uninitialized use of config_helper")
|
|
|
|
_save_config()
|
|
|
|
|
|
|
|
|
2022-03-06 11:23:02 +13:00
|
|
|
def add_option(app_name: str, option: str, value: str):
|
|
|
|
value = value.replace("%%", "%").replace("%", "%%")
|
2022-09-03 02:53:56 +12:00
|
|
|
if not _config.has_section(app_name):
|
|
|
|
_config[app_name] = {}
|
2022-02-06 02:48:50 +13:00
|
|
|
|
2022-09-03 02:53:56 +12:00
|
|
|
_config.set(app_name, option, value)
|
2022-09-05 08:49:43 +12:00
|
|
|
save_config()
|
2022-02-06 02:48:50 +13:00
|
|
|
|
|
|
|
|
|
|
|
def remove_option(app_name, option):
|
2022-09-03 02:53:56 +12:00
|
|
|
if _config.has_option(app_name, option):
|
|
|
|
_config.remove_option(app_name, option)
|
2022-02-06 02:48:50 +13:00
|
|
|
|
2022-09-03 02:53:56 +12:00
|
|
|
if _config.has_section(app_name) and not _config[app_name]:
|
|
|
|
_config.remove_section(app_name)
|
2022-02-06 02:48:50 +13:00
|
|
|
|
2022-09-05 08:49:43 +12:00
|
|
|
save_config()
|
2022-02-06 02:48:50 +13:00
|
|
|
|
|
|
|
|
|
|
|
def remove_section(app_name):
|
2022-09-03 02:53:56 +12:00
|
|
|
if _config.has_section(app_name):
|
|
|
|
_config.remove_section(app_name)
|
2022-09-05 08:49:43 +12:00
|
|
|
save_config()
|