[bot-base] Faut pas sauvegarder quand on initialise les valeurs par défaut, sinon ca sert à rien de sauvegarder une config
[config] Ajout de la possibilité de ne pas sauvegarder
This commit is contained in:
parent
517b5fed23
commit
8dd9ff86a3
1
Pipfile
1
Pipfile
@ -12,6 +12,7 @@ toml = "*"
|
||||
pytest = "*"
|
||||
sphinx = "*"
|
||||
sphinx-rtd-theme = "*"
|
||||
watchgod = "*"
|
||||
|
||||
[requires]
|
||||
python_version = "3.8"
|
||||
|
10
Pipfile.lock
generated
10
Pipfile.lock
generated
@ -1,7 +1,7 @@
|
||||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "56274993c51b422f6d178e8a21bbf669b8b508facb513d6c63f3373ebf707863"
|
||||
"sha256": "267e2db73eae9033f8531715235a968fe4ddd2ca4f09ccc266d5f6da869acb23"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
@ -486,6 +486,14 @@
|
||||
],
|
||||
"version": "==1.25.9"
|
||||
},
|
||||
"watchgod": {
|
||||
"hashes": [
|
||||
"sha256:59700dab7445aa8e6067a5b94f37bae90fc367554549b1ed2e9d0f4f38a90d2a",
|
||||
"sha256:e9cca0ab9c63f17fc85df9fd8bd18156ff00aff04ebe5976cee473f4968c6858"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==0.6"
|
||||
},
|
||||
"wcwidth": {
|
||||
"hashes": [
|
||||
"sha256:cafe2186b3c009a04067022ce1dcd79cb38d8d65ee4f4791b8888d6599d1bbe1",
|
||||
|
@ -45,7 +45,7 @@ class BotBase(discord.Client):
|
||||
"modules": [],
|
||||
"data_folder": data_folder,
|
||||
"modules_folder": modules_folder,
|
||||
})
|
||||
}, no_save=True)
|
||||
|
||||
self.config.load()
|
||||
|
||||
|
@ -15,7 +15,7 @@ class Config:
|
||||
#: :class:`typing.Type` [:class:`BaseType`]: Current fields
|
||||
fields: typing.Dict[str, BaseType]
|
||||
|
||||
def __init__(self, path: str) -> None:
|
||||
def __init__(self, path: typing.Optional[str]) -> None:
|
||||
"""
|
||||
Create config object
|
||||
|
||||
@ -45,7 +45,7 @@ class Config:
|
||||
name: type_()
|
||||
})
|
||||
|
||||
def set(self, values: dict) -> None:
|
||||
def set(self, values: dict, no_save: bool = False) -> None:
|
||||
"""
|
||||
Set all parameters with values (and override old ones)
|
||||
|
||||
@ -54,17 +54,21 @@ class Config:
|
||||
>>> from config.config_types import factory, Int
|
||||
>>> config = Config("doctest_config.toml")
|
||||
>>> config.register("my_parameter", factory(Int))
|
||||
>>> config.set({"my_parameter": 3})
|
||||
>>> config.set({"my_parameter": 3}) #doctest: +SKIP
|
||||
>>> config.set({"my_parameter": 4}, no_save=True)
|
||||
|
||||
: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
|
||||
if not no_save:
|
||||
self.save()
|
||||
|
||||
def save(self) -> None:
|
||||
"""
|
||||
@ -75,9 +79,10 @@ class Config:
|
||||
>>> from config.config_types import factory, Int
|
||||
>>> config = Config("doctest_config.toml")
|
||||
>>> config.register("my_parameter", factory(Int))
|
||||
>>> config.set({"my_parameter": 3})
|
||||
>>> config.set({"my_parameter": 3}) #doctest: +SKIP
|
||||
>>> config.save() #doctest: +SKIP
|
||||
"""
|
||||
if self.path is not None:
|
||||
os.makedirs(os.path.dirname(self.path), exist_ok=True)
|
||||
with open(self.path, 'w') as file:
|
||||
toml.dump({k: v.to_save() for k, v in self.fields.items()}, file)
|
||||
@ -91,7 +96,7 @@ class Config:
|
||||
>>> from config.config_types import factory, Int
|
||||
>>> config = Config("doctest_config.toml")
|
||||
>>> config.register("my_parameter", factory(Int))
|
||||
>>> config.set({"my_parameter": 3})
|
||||
>>> config.set({"my_parameter": 3}) #doctest: +SKIP
|
||||
>>> config.save() #doctest: +SKIP
|
||||
>>> new_config = Config("doctest_config.toml")
|
||||
>>> new_config.register("my_parameter", factory(Int))
|
||||
@ -101,6 +106,7 @@ class Config:
|
||||
|
||||
:return: None
|
||||
"""
|
||||
if self.path is not None:
|
||||
try:
|
||||
with open(self.path, 'r') as file:
|
||||
self.set(toml.load(file))
|
||||
@ -119,8 +125,12 @@ class Config:
|
||||
>>> 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"])
|
||||
>>> config.set({"my_parameter": 3}) #doctest: +SKIP
|
||||
>>> print(config["my_parameter"]) #doctest: +SKIP
|
||||
3
|
||||
"""
|
||||
self.load()
|
||||
return self.fields[item].get()
|
||||
|
||||
def __str__(self):
|
||||
return f"<Config with fields <{', '.join(f'{k} = {v}' for k, v in self.fields.items())}>>"
|
||||
|
12
src/main.py
12
src/main.py
@ -22,10 +22,10 @@ def setup_logging(default_path='data/log_config.json', default_level=logging.INF
|
||||
logging.basicConfig(level=default_level)
|
||||
|
||||
|
||||
setup_logging()
|
||||
|
||||
if __name__ == "__main__":
|
||||
client = BotBase(max_messages=500000, data_folder="datas", modules_folder=os.environ.get("LOCAL_MODULES", "modules"))
|
||||
def main():
|
||||
setup_logging()
|
||||
client = BotBase(max_messages=500000, data_folder="datas",
|
||||
modules_folder=os.environ.get("LOCAL_MODULES", "modules"))
|
||||
|
||||
async def start_bot():
|
||||
await client.start(os.environ.get("DISCORD_TOKEN"))
|
||||
@ -33,3 +33,7 @@ if __name__ == "__main__":
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(start_bot())
|
||||
loop.run_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user