[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 = "*"
|
pytest = "*"
|
||||||
sphinx = "*"
|
sphinx = "*"
|
||||||
sphinx-rtd-theme = "*"
|
sphinx-rtd-theme = "*"
|
||||||
|
watchgod = "*"
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.8"
|
python_version = "3.8"
|
||||||
|
10
Pipfile.lock
generated
10
Pipfile.lock
generated
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"_meta": {
|
"_meta": {
|
||||||
"hash": {
|
"hash": {
|
||||||
"sha256": "56274993c51b422f6d178e8a21bbf669b8b508facb513d6c63f3373ebf707863"
|
"sha256": "267e2db73eae9033f8531715235a968fe4ddd2ca4f09ccc266d5f6da869acb23"
|
||||||
},
|
},
|
||||||
"pipfile-spec": 6,
|
"pipfile-spec": 6,
|
||||||
"requires": {
|
"requires": {
|
||||||
@ -486,6 +486,14 @@
|
|||||||
],
|
],
|
||||||
"version": "==1.25.9"
|
"version": "==1.25.9"
|
||||||
},
|
},
|
||||||
|
"watchgod": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:59700dab7445aa8e6067a5b94f37bae90fc367554549b1ed2e9d0f4f38a90d2a",
|
||||||
|
"sha256:e9cca0ab9c63f17fc85df9fd8bd18156ff00aff04ebe5976cee473f4968c6858"
|
||||||
|
],
|
||||||
|
"index": "pypi",
|
||||||
|
"version": "==0.6"
|
||||||
|
},
|
||||||
"wcwidth": {
|
"wcwidth": {
|
||||||
"hashes": [
|
"hashes": [
|
||||||
"sha256:cafe2186b3c009a04067022ce1dcd79cb38d8d65ee4f4791b8888d6599d1bbe1",
|
"sha256:cafe2186b3c009a04067022ce1dcd79cb38d8d65ee4f4791b8888d6599d1bbe1",
|
||||||
|
@ -45,7 +45,7 @@ class BotBase(discord.Client):
|
|||||||
"modules": [],
|
"modules": [],
|
||||||
"data_folder": data_folder,
|
"data_folder": data_folder,
|
||||||
"modules_folder": modules_folder,
|
"modules_folder": modules_folder,
|
||||||
})
|
}, no_save=True)
|
||||||
|
|
||||||
self.config.load()
|
self.config.load()
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ class Config:
|
|||||||
#: :class:`typing.Type` [:class:`BaseType`]: Current fields
|
#: :class:`typing.Type` [:class:`BaseType`]: Current fields
|
||||||
fields: typing.Dict[str, BaseType]
|
fields: typing.Dict[str, BaseType]
|
||||||
|
|
||||||
def __init__(self, path: str) -> None:
|
def __init__(self, path: typing.Optional[str]) -> None:
|
||||||
"""
|
"""
|
||||||
Create config object
|
Create config object
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ class Config:
|
|||||||
name: type_()
|
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)
|
Set all parameters with values (and override old ones)
|
||||||
|
|
||||||
@ -54,17 +54,21 @@ class Config:
|
|||||||
>>> from config.config_types import factory, Int
|
>>> from config.config_types import factory, Int
|
||||||
>>> config = Config("doctest_config.toml")
|
>>> config = Config("doctest_config.toml")
|
||||||
>>> config.register("my_parameter", factory(Int))
|
>>> 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
|
:type values: dict
|
||||||
:param values: dict of parameters
|
:param values: dict of parameters
|
||||||
"""
|
"""
|
||||||
for k, v in values.items():
|
for k, v in values.items():
|
||||||
try:
|
try:
|
||||||
|
|
||||||
self.fields[k].set(v)
|
self.fields[k].set(v)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# TODO: trouver un moyen de warn
|
# TODO: trouver un moyen de warn
|
||||||
pass
|
pass
|
||||||
|
if not no_save:
|
||||||
|
self.save()
|
||||||
|
|
||||||
def save(self) -> None:
|
def save(self) -> None:
|
||||||
"""
|
"""
|
||||||
@ -75,9 +79,10 @@ class Config:
|
|||||||
>>> from config.config_types import factory, Int
|
>>> from config.config_types import factory, Int
|
||||||
>>> config = Config("doctest_config.toml")
|
>>> config = Config("doctest_config.toml")
|
||||||
>>> config.register("my_parameter", factory(Int))
|
>>> config.register("my_parameter", factory(Int))
|
||||||
>>> config.set({"my_parameter": 3})
|
>>> config.set({"my_parameter": 3}) #doctest: +SKIP
|
||||||
>>> config.save() #doctest: +SKIP
|
>>> config.save() #doctest: +SKIP
|
||||||
"""
|
"""
|
||||||
|
if self.path is not None:
|
||||||
os.makedirs(os.path.dirname(self.path), exist_ok=True)
|
os.makedirs(os.path.dirname(self.path), exist_ok=True)
|
||||||
with open(self.path, 'w') as file:
|
with open(self.path, 'w') as file:
|
||||||
toml.dump({k: v.to_save() for k, v in self.fields.items()}, 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
|
>>> from config.config_types import factory, Int
|
||||||
>>> config = Config("doctest_config.toml")
|
>>> config = Config("doctest_config.toml")
|
||||||
>>> config.register("my_parameter", factory(Int))
|
>>> config.register("my_parameter", factory(Int))
|
||||||
>>> config.set({"my_parameter": 3})
|
>>> config.set({"my_parameter": 3}) #doctest: +SKIP
|
||||||
>>> config.save() #doctest: +SKIP
|
>>> config.save() #doctest: +SKIP
|
||||||
>>> new_config = Config("doctest_config.toml")
|
>>> new_config = Config("doctest_config.toml")
|
||||||
>>> new_config.register("my_parameter", factory(Int))
|
>>> new_config.register("my_parameter", factory(Int))
|
||||||
@ -101,6 +106,7 @@ class Config:
|
|||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
if self.path is not None:
|
||||||
try:
|
try:
|
||||||
with open(self.path, 'r') as file:
|
with open(self.path, 'r') as file:
|
||||||
self.set(toml.load(file))
|
self.set(toml.load(file))
|
||||||
@ -119,8 +125,12 @@ class Config:
|
|||||||
>>> from config.config_types import factory, Int
|
>>> from config.config_types import factory, Int
|
||||||
>>> config = Config("doctest_config.toml")
|
>>> config = Config("doctest_config.toml")
|
||||||
>>> config.register("my_parameter", factory(Int))
|
>>> config.register("my_parameter", factory(Int))
|
||||||
>>> config.set({"my_parameter": 3})
|
>>> config.set({"my_parameter": 3}) #doctest: +SKIP
|
||||||
>>> print(config["my_parameter"])
|
>>> print(config["my_parameter"]) #doctest: +SKIP
|
||||||
3
|
3
|
||||||
"""
|
"""
|
||||||
|
self.load()
|
||||||
return self.fields[item].get()
|
return self.fields[item].get()
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"<Config with fields <{', '.join(f'{k} = {v}' for k, v in self.fields.items())}>>"
|
||||||
|
10
src/main.py
10
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)
|
logging.basicConfig(level=default_level)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
setup_logging()
|
setup_logging()
|
||||||
|
client = BotBase(max_messages=500000, data_folder="datas",
|
||||||
if __name__ == "__main__":
|
modules_folder=os.environ.get("LOCAL_MODULES", "modules"))
|
||||||
client = BotBase(max_messages=500000, data_folder="datas", modules_folder=os.environ.get("LOCAL_MODULES", "modules"))
|
|
||||||
|
|
||||||
async def start_bot():
|
async def start_bot():
|
||||||
await client.start(os.environ.get("DISCORD_TOKEN"))
|
await client.start(os.environ.get("DISCORD_TOKEN"))
|
||||||
@ -33,3 +33,7 @@ if __name__ == "__main__":
|
|||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.create_task(start_bot())
|
loop.create_task(start_bot())
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user