Voila des config toutes jolies

This commit is contained in:
Fomys 2019-08-10 17:58:54 +02:00
parent 63bfb3e0f8
commit bf21ebe41a
Signed by: fomys
GPG Key ID: 1ECA046A9615ABA0
7 changed files with 95 additions and 38 deletions

4
.gitignore vendored
View File

@ -64,8 +64,10 @@ target/
.swp.*
temp.zip
config/*
.idea/*
venv/*
data/*
*.yml
*.json

View File

@ -8,16 +8,15 @@ name = "pypi"
[packages]
packaging = "*"
discord-py = {extras = ["voice"],git = "https://github.com/Rapptz/discord.py",ref = "84c1eac62a775a37b03bd0971b221b0c50724630"}
aiohttp = "*"
aiofiles = "*"
lupa = "*"
aiofile = "*"
pyyaml = "*"
"discord.py" = {version = "*", extras = ["voice",]}
[dev-packages]
[requires]
python_version = "3.7"

28
config/FileSystem.py Normal file
View File

@ -0,0 +1,28 @@
import os
from aiofile import AIOFile, Reader, Writer
import yaml
from config.base import Config
class FSConfig(Config):
def __init__(self, path="config.json", *args, **kwargs):
super().__init__(*args, **kwargs)
self.path = path
os.makedirs(os.path.dirname(path), exist_ok=True)
open(path, "a").close()
async def _load(self):
content = ""
async with AIOFile(self.path, "r") as afp:
reader = Reader(afp, chunk_size=8)
async for chunk in reader:
content+=chunk
self.config = yaml.load(content, Loader=yaml.BaseLoader)
async def _save(self):
content = yaml.dump(self.config)
async with AIOFile(self.path, "w") as afp:
writer = Writer(afp)
await writer(content)
await afp.fsync()

42
config/base.py Normal file
View File

@ -0,0 +1,42 @@
import asyncio
class Config:
def __init__(self, config: dict = None, parent = None, name: str = None):
if config is None:
config = {}
self.config = config
self.parent = parent
self.cache = []
if self.parent:
self.parent = parent
self.name = name
self.parent.config[self.name] = self.config
async def _save(self):
if self.parent:
self.parent.save()
def save(self):
loop = asyncio.get_event_loop()
asyncio.ensure_future(self._save(), loop=loop)
async def _load(self):
if self.parent:
self.parent.load()
self.config = self.parent.config[self.name]
def load(self):
loop = asyncio.get_event_loop()
asyncio.ensure_future(self._load(), loop=loop)
def __getitem__(self, item):
return self.config.get(item)
def __setitem__(self, key, value):
if self.parent:
self.parent[self.name][key] = value
self.config = self.parent[self.name]
else:
self.config[key] = value
self.save()

39
main.py
View File

@ -16,6 +16,7 @@ from typing import Dict
import discord
from packaging.version import Version
from config.FileSystem import FSConfig
from errors import IncompatibleModule
__version__ = "0.1.0"
@ -228,30 +229,12 @@ class LBI(discord.Client):
self.ready = False
# Content: {"module_name": {"module": imported module, "class": initialized class}}
self.modules = {}
self.config = {
"modules": ["modules"],
"prefix": "%",
"owners": [],
}
self.load_config()
self.config = FSConfig(path="data/config.yml")
self.config["modules"] = self.config["modules"] if self.config["modules"] is not None else ["modules","errors"]
self.config["prefix"] = self.config["prefix"] or "%"
self.config["owners"] = self.config["owners"] or []
self.load_modules()
def load_config(self, config_file="config/config.json"):
if os.path.exists(config_file):
with open(config_file, 'rt') as f:
config = json.load(f)
self.config.update(config)
self.info("Config successfully loaded.")
else:
with open(config_file, 'w') as f:
json.dump(self.config, f)
self.info("Config successfully created.")
def save_config(self, config_file="config/config.json"):
with open(config_file, "w") as f:
json.dump(self.config, f)
self.info("Config successfully saved.")
@modules_edit
def load_modules(self):
self.info("Starts to load modules...")
@ -290,6 +273,7 @@ class LBI(discord.Client):
if dep not in self.modules.keys():
if dep != "base":
self.load_module(dep)
if MODULES[module].type == "python":
try:
self.info("Start loading module {module}...".format(module=module))
@ -299,11 +283,13 @@ class LBI(discord.Client):
self.modules.update({module: {"imported": imported, "initialized_class": initialized_class}})
self.info("Module {module} successfully imported.".format(module=module))
initialized_class.dispatch("load")
if module not in self.config["modules"]:
self.config["modules"].append(module)
self.save_config()
self.config.save()
except AttributeError as e:
self.error("Module {module} doesn't have MainClass.".format(module=module))
raise
return e
return 0
elif MODULES[module].type == "lua":
@ -316,7 +302,6 @@ class LBI(discord.Client):
initialized_class.dispatch("load")
if module not in self.config["modules"]:
self.config["modules"].append(module)
self.save_config()
return 0
@modules_edit
@ -325,7 +310,7 @@ class LBI(discord.Client):
try:
if module in self.config["modules"]:
self.config["modules"].remove(module)
self.save_config()
self.config.save()
self.unload_all()
self.load_modules()
except KeyError as e:
@ -415,7 +400,7 @@ class ClientById:
return None
client1 = LBI()
client1 = LBI(max_messages=500000)
class Communication(asyncio.Protocol):
@ -448,7 +433,7 @@ communication = Communication(client1)
async def start_bot():
await client1.start(os.environ.get("DISCORD_TOKEN"), max_messages=500000)
await client1.start(os.environ.get("DISCORD_TOKEN"))
print(os.path.join("/tmp", os.path.dirname(os.path.realpath(__file__))) + ".sock")

View File

@ -7,6 +7,7 @@ from typing import List
import discord
from config.base import Config
from storage import FSStorage, FSObjects
import storage.path as path
@ -36,6 +37,7 @@ class BaseClass:
self.client = client
self.storage = FSStorage(path.join(self.client.base_path, self.name))
self.objects = FSObjects(self.storage)
self.config = Config(parent=self.client.config, name="mod-"+self.name)
# Non necessaire car géré par fsstorage
#if not self.storage.isdir(path.join("storage", self.name)):
# self.storage.makedirs(path.join("storage", self.name), exist_ok=True)

View File

@ -5,6 +5,7 @@ import traceback
import collections
import discord
from config.base import Config
from modules.base import BaseClassPython
@ -25,12 +26,10 @@ class MainClass(BaseClassPython):
def __init__(self, client):
super().__init__(client)
self.config["dev_chan"] = self.config["dev_chan"] or []
self.config["meme"] = [""]
self.config["icon"] = ""
self.errorsDeque = None
self.development_chan_id = []
self.memes = [
"",
]
self.icon = ""
async def on_ready(self):
if self.objects.save_exists('errorsDeque'):
@ -67,7 +66,7 @@ class MainClass(BaseClassPython):
self.errorsDeque.append(message_list)
except:
pass
for chanid in self.development_chan_id:
for chanid in self.config["dev_chan"]:
try:
await self.client.get_channel(chanid).send(
embed=embed.set_footer(text="Ce message ne s'autodétruira pas.", icon_url=self.icon))