bot-base/config/base.py
Louis Chauvet 3f416c5682
[main] Update some docstrings, remove config check
[doc] Put all config_types on same page, add link to python and discord.py doc
[config] Add some doc, solve KeyError when loading config file
[config-types] Add some doc, add guild type
2020-04-21 02:59:08 +02:00

124 lines
3.3 KiB
Python

from __future__ import annotations
import typing
import toml
BaseType = typing.TypeVar("BaseType")
class Config:
#: :class:`str`: Path of config file
path: str
#: :class:`typing.Type` [:class:`BaseType`]: Current fields
fields: typing.Dict[str, BaseType]
def __init__(self, path: str) -> None:
"""
Create config object
Basic usage:
>>> config = Config("doctest_config.toml")
:param str path: Path of config file
"""
self.fields = {}
self.path = path
def register(self, name: str, type_: typing.Type[BaseType]) -> None:
"""
Register option
Basic usage:
>>> from config.config_types import factory, Int
>>> config = Config("doctest_config.toml")
>>> config.register("my_parameter", factory(Int))
:param str name: Name of config parameter
:param typing.Type[BaseType] type_: Type of config parameter
"""
self.fields.update({
name: type_()
})
def set(self, values: dict) -> None:
"""
Set all parameters with values (and override old ones)
Basic usage:
>>> from config.config_types import factory, Int
>>> config = Config("doctest_config.toml")
>>> config.register("my_parameter", factory(Int))
>>> config.set({"my_parameter": 3})
:type values: dict
:param values: dict of parameters
"""
for k, v in values.items():
try:
self.fields[k].set(v)
except KeyError:
# TODO: trouver un moyen de warn
pass
def save(self) -> None:
"""
Save config to ``self.file``
Basic usage:
>>> from config.config_types import factory, Int
>>> config = Config("doctest_config.toml")
>>> config.register("my_parameter", factory(Int))
>>> config.set({"my_parameter": 3})
>>> config.save()
"""
with open(self.path, 'w') as file:
toml.dump({k: v.to_save() for k, v in self.fields.items()}, file)
def load(self) -> None:
"""
Load config from ``self.file``
Basic usage:
>>> from config.config_types import factory, Int
>>> config = Config("doctest_config.toml")
>>> config.register("my_parameter", factory(Int))
>>> config.set({"my_parameter": 3})
>>> config.save()
>>> new_config = Config("doctest_config.toml")
>>> new_config.register("my_parameter", factory(Int))
>>> new_config.load()
>>> new_config["my_parameter"]
3
:return: None
"""
try:
with open(self.path, 'r') as file:
self.set(toml.load(file))
except FileNotFoundError:
self.save()
def __getitem__(self, item: str) -> typing.Any:
"""
Get field from config
:param str item: Config field to get
Basic usage:
>>> from config.config_types import factory, Int
>>> config = Config("doctest_config.toml")
>>> config.register("my_parameter", factory(Int))
>>> config.set({"my_parameter": 3})
>>> print(config["my_parameter"])
3
"""
return self.fields[item].get()