Test
This commit is contained in:
parent
6d3944b76a
commit
13d7036a74
@ -1,34 +1,77 @@
|
|||||||
import asyncio
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Dict, Any, Optional
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
def __init__(self, config: dict = None, parent = None, name: str = None):
|
name: Optional[str]
|
||||||
if config is None:
|
parent: Optional[Config]
|
||||||
config = {}
|
config: Dict[Any, Any]
|
||||||
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):
|
def __init__(self, parent: Config = None, name: str = None):
|
||||||
|
"""Create Config
|
||||||
|
|
||||||
|
:param parent: Parent configuration
|
||||||
|
:param name: Configuration name
|
||||||
|
:type parent: Config
|
||||||
|
:type name: str"""
|
||||||
|
self.parent = parent
|
||||||
|
self.config = dict()
|
||||||
|
self.name = None
|
||||||
|
if self.parent:
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
def init(self, config):
|
||||||
|
"""Load default configuration
|
||||||
|
|
||||||
|
:param config: Default configuration
|
||||||
|
:type config: dict
|
||||||
|
:return: None"""
|
||||||
|
# Load data from config file before initialisation
|
||||||
|
self.load()
|
||||||
|
# Get data from parent
|
||||||
|
if self.parent is not None:
|
||||||
|
self.parent.config[self.name] = self.parent.config.get(self.name) if self.parent.config.get(
|
||||||
|
self.name) is not None else self.config
|
||||||
|
self.config = self.parent.config[self.name]
|
||||||
|
# Set config only if not already defined
|
||||||
|
for k, v in config.items():
|
||||||
|
self.config[k] = self.config.get(k) if self.config.get(k) is not None else v
|
||||||
|
# Save new datas
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
def _save(self):
|
||||||
|
"""Internal function for save
|
||||||
|
|
||||||
|
Must be overridden by all type of config to handle saving"""
|
||||||
|
# Call parent save if necessary
|
||||||
if self.parent:
|
if self.parent:
|
||||||
self.parent.save()
|
self.parent.save()
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
loop = asyncio.get_event_loop()
|
"""Public save function
|
||||||
asyncio.ensure_future(self._save(), loop=loop)
|
|
||||||
|
|
||||||
async def _load(self):
|
Do not override"""
|
||||||
|
self._save()
|
||||||
|
|
||||||
|
def _load(self):
|
||||||
|
"""Internal function for load
|
||||||
|
|
||||||
|
Mus be overridden by all type of config to handle loading"""
|
||||||
|
# Load parent if necessary
|
||||||
if self.parent:
|
if self.parent:
|
||||||
self.parent.load()
|
self.parent.load()
|
||||||
self.config = self.parent.config[self.name]
|
self.config = self.parent.config.get(self.name)
|
||||||
|
# Initialize parent if necessary
|
||||||
|
if self.config is None:
|
||||||
|
self.parent.config[self.name] = {}
|
||||||
|
self.config = {}
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
loop = asyncio.get_event_loop()
|
"""Public load function
|
||||||
asyncio.ensure_future(self._load(), loop=loop)
|
|
||||||
|
Do not override"""
|
||||||
|
self._load()
|
||||||
|
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
return self.config.get(item)
|
return self.config.get(item)
|
||||||
@ -39,4 +82,4 @@ class Config:
|
|||||||
self.config = self.parent[self.name]
|
self.config = self.parent[self.name]
|
||||||
else:
|
else:
|
||||||
self.config[key] = value
|
self.config[key] = value
|
||||||
self.save()
|
self.save()
|
||||||
|
@ -1,28 +1,29 @@
|
|||||||
import os
|
import os
|
||||||
from aiofile import AIOFile, Reader, Writer
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from config.Base import Config
|
from config.Base import Config
|
||||||
|
|
||||||
|
|
||||||
class FSConfig(Config):
|
class FSConfig(Config):
|
||||||
|
path: str
|
||||||
|
|
||||||
def __init__(self, path="config.json", *args, **kwargs):
|
def __init__(self, path="config.json", *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.path = path
|
self.path = path
|
||||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||||
open(path, "a").close()
|
open(path, "a").close()
|
||||||
|
|
||||||
async def _load(self):
|
def _load(self):
|
||||||
content = ""
|
with open(self.path, "r") as file:
|
||||||
async with AIOFile(self.path, "r") as afp:
|
content = file.read()
|
||||||
reader = Reader(afp, chunk_size=8)
|
|
||||||
async for chunk in reader:
|
|
||||||
content+=chunk
|
|
||||||
self.config = yaml.load(content, Loader=yaml.BaseLoader)
|
self.config = yaml.load(content, Loader=yaml.BaseLoader)
|
||||||
|
if self.config is None:
|
||||||
|
self.parent.config[self.name] = {}
|
||||||
|
self.config = {}
|
||||||
|
|
||||||
async def _save(self):
|
def _save(self):
|
||||||
content = yaml.dump(self.config)
|
content = yaml.dump(self.config)
|
||||||
async with AIOFile(self.path, "w") as afp:
|
print(self.config)
|
||||||
writer = Writer(afp)
|
with open(self.path, "w") as file:
|
||||||
await writer(content)
|
file.write(content)
|
||||||
await afp.fsync()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user