bot-base/config/FileSystem.py

28 lines
840 B
Python
Raw Normal View History

2019-08-10 17:58:54 +02:00
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()