Fix issues
This commit is contained in:
parent
d6a01702b7
commit
c48319c5da
19
example/config.yaml
Normal file
19
example/config.yaml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
db:
|
||||||
|
host: 127.0.0.1
|
||||||
|
username: test
|
||||||
|
password: 123456
|
||||||
|
|
||||||
|
groups:
|
||||||
|
- name: admin
|
||||||
|
priority: 100
|
||||||
|
permissions:
|
||||||
|
- ban_user
|
||||||
|
- ro_user
|
||||||
|
- invite_user
|
||||||
|
- promote_user
|
||||||
|
|
||||||
|
- name: moderator
|
||||||
|
priority: 50
|
||||||
|
permissions:
|
||||||
|
- invite_user
|
||||||
|
- ro_user
|
||||||
28
example/simple.py
Normal file
28
example/simple.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from kissconfig import load_config, ConfigClass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Database (ConfigClass):
|
||||||
|
host: str
|
||||||
|
username: str
|
||||||
|
password: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Group (ConfigClass):
|
||||||
|
name: str
|
||||||
|
priority: int
|
||||||
|
permissions: list[str]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Config (ConfigClass):
|
||||||
|
db: Database
|
||||||
|
groups: list[Group]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
config = load_config(Config, "config.yaml")
|
||||||
|
print(config)
|
||||||
@ -2,20 +2,25 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
from typing import Type
|
from typing import Type, Optional
|
||||||
|
|
||||||
from kissconfig.config import ConfigClass
|
from kissconfig.config import ConfigClass
|
||||||
from kissconfig.loader import Loader, YamlLoader
|
from kissconfig.loader import Loader, YamlLoader
|
||||||
|
|
||||||
|
|
||||||
def load_config(main_config: Type[ConfigClass], loader: Loader = YamlLoader()):
|
def load_config(class_: Type[ConfigClass],
|
||||||
if not os.path.exists(config_path):
|
path: str,
|
||||||
logging.warning(f"Unable to locate app config ({config_path})!")
|
loader: Loader = YamlLoader(),
|
||||||
if not os.path.exists(example_config_path):
|
example_path: Optional[str] = None):
|
||||||
logging.critical(f"Unable to locate example config ({example_config_path})")
|
if os.path.exists(path):
|
||||||
sys.exit(1)
|
return loader.from_file(class_, path)
|
||||||
else:
|
|
||||||
shutil.copy2(example_config_path, config_path)
|
logging.warning(f"Unable to locate app config ({path})!")
|
||||||
|
if example_path:
|
||||||
|
if os.path.exists(example_path):
|
||||||
|
shutil.copy2(example_path, path)
|
||||||
logging.warning(f"Actual app config (%s) created from example (%s), don't forget to update it!",
|
logging.warning(f"Actual app config (%s) created from example (%s), don't forget to update it!",
|
||||||
config_path, example_config_path)
|
path, example_path)
|
||||||
return main_config.from_file(config_path, config_namespace)
|
else:
|
||||||
|
logging.critical(f"Unable to locate example config ({example_path})")
|
||||||
|
sys.exit(1)
|
||||||
|
|||||||
@ -34,13 +34,3 @@ class ConfigClass:
|
|||||||
raise ValueError(f"{cls.__name__}.{f.name} is not configured!")
|
raise ValueError(f"{cls.__name__}.{f.name} is not configured!")
|
||||||
kwargs[f.name] = ConfigClass.parse(data[f.name], hints[f.name])
|
kwargs[f.name] = ConfigClass.parse(data[f.name], hints[f.name])
|
||||||
return cls(**kwargs)
|
return cls(**kwargs)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_file(cls, filepath: str, config_namespace: str = ""):
|
|
||||||
with open(filepath) as f:
|
|
||||||
data = safe_load(f)
|
|
||||||
if data is None:
|
|
||||||
raise ValueError("")
|
|
||||||
if config_namespace:
|
|
||||||
data = data.get(config_namespace, {})
|
|
||||||
return cls.from_dict(data)
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
from abc import ABCMeta, abstractmethod
|
from abc import ABCMeta, abstractmethod
|
||||||
from typing import Type
|
from typing import Type, Optional
|
||||||
|
|
||||||
from yaml import safe_load
|
import yaml
|
||||||
|
|
||||||
from kissconfig.config import ConfigClass
|
from kissconfig.config import ConfigClass
|
||||||
|
|
||||||
@ -11,10 +11,8 @@ class EmptyConfig (ValueError):
|
|||||||
|
|
||||||
|
|
||||||
class Loader (metaclass=ABCMeta):
|
class Loader (metaclass=ABCMeta):
|
||||||
def __init__(self, path: str = None,
|
def __init__(self, namespace: Optional[str] = None):
|
||||||
example_path: str = None):
|
self.namespace = namespace
|
||||||
self.path = path
|
|
||||||
self.example_path = example_path
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def parse_file(self, path: str) -> dict:
|
def parse_file(self, path: str) -> dict:
|
||||||
@ -31,4 +29,5 @@ class Loader (metaclass=ABCMeta):
|
|||||||
|
|
||||||
class YamlLoader (Loader):
|
class YamlLoader (Loader):
|
||||||
def parse_file(self, path: str) -> dict:
|
def parse_file(self, path: str) -> dict:
|
||||||
return safe_load(path)
|
with open(path) as f:
|
||||||
|
return yaml.safe_load(f)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user