initial commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
from dataclasses import dataclass, field, asdict
|
||||
from json import loads, dump, dumps
|
||||
from typing import TextIO
|
||||
|
||||
config_version = 1
|
||||
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
@dataclass
|
||||
class Logs:
|
||||
path: str = 'logs/'
|
||||
name: str = 'KuroCore'
|
||||
level: str = 'DEBUG'
|
||||
requests: bool = False
|
||||
console: bool = False
|
||||
|
||||
@dataclass
|
||||
class Database:
|
||||
driver: str = 'psql'
|
||||
host: str = ''
|
||||
name: str = ''
|
||||
user: str = 'root'
|
||||
password: str = ''
|
||||
port: int = 5432
|
||||
|
||||
@dataclass
|
||||
class Callback:
|
||||
enabled: bool = False
|
||||
code: str = ''
|
||||
port: int = 9980
|
||||
secret: str = ''
|
||||
|
||||
version: int = config_version
|
||||
tokens: list[str] = field(default_factory=list[str])
|
||||
prefixes: list[str] = field(default_factory=list[str])
|
||||
sentry_dsn: str = ''
|
||||
debug: bool = False
|
||||
logs: Logs = field(default_factory=Logs)
|
||||
database: Database = field(default_factory=Database)
|
||||
callback: Callback = field(default_factory=Callback)
|
||||
|
||||
plugins = []
|
||||
|
||||
@staticmethod
|
||||
def loads(config_str: str):
|
||||
json: dict = loads(config_str)
|
||||
if json.get('version', 0) < config_version:
|
||||
raise Exception('This config was generated for old version! Move config file and try again.')
|
||||
if json.get('version', 0) > config_version:
|
||||
raise Exception('This config was generated for newer version! Move config file and try again.')
|
||||
|
||||
json.update({'callback': Config.Callback(**json['callback'])})
|
||||
json.update({'database': Config.Database(**json['database'])})
|
||||
json.update({'logs': Config.Logs(**json['logs'])})
|
||||
|
||||
return Config(**json)
|
||||
|
||||
@staticmethod
|
||||
def loadp(path: str):
|
||||
try:
|
||||
with open(path, 'r') as f:
|
||||
return Config.loads(f.read())
|
||||
except FileNotFoundError:
|
||||
gen = asdict(Config())
|
||||
with open(path, 'w') as f:
|
||||
dump(gen, f)
|
||||
return Config.loads(dumps(gen))
|
||||
|
||||
@staticmethod
|
||||
def load(file: TextIO):
|
||||
return Config.loads(file.read())
|
||||
|
||||
Reference in New Issue
Block a user