Compare commits

..

No commits in common. "b71bd5b24bc19e925db239612b46683aa951ca59" and "c87b0b7d1631931f08d94c4ca58797f5a4b37136" have entirely different histories.

6 changed files with 28 additions and 85 deletions

View File

@ -1,19 +0,0 @@
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

View File

@ -1,28 +0,0 @@
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)

View File

@ -2,25 +2,20 @@ import os
import shutil
import logging
import sys
from typing import Type, Optional
from typing import Type
from kissconfig.config import ConfigClass
from kissconfig.loader import Loader, YamlLoader
def load_config(class_: Type[ConfigClass],
path: str,
loader: Loader = YamlLoader(),
example_path: Optional[str] = None):
if os.path.exists(path):
return loader.from_file(class_, 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!",
path, example_path)
else:
logging.critical(f"Unable to locate example config ({example_path})")
def load_config(main_config: Type[ConfigClass], loader: Loader = YamlLoader()):
if not os.path.exists(config_path):
logging.warning(f"Unable to locate app config ({config_path})!")
if not os.path.exists(example_config_path):
logging.critical(f"Unable to locate example config ({example_config_path})")
sys.exit(1)
else:
shutil.copy2(example_config_path, config_path)
logging.warning(f"Actual app config (%s) created from example (%s), don't forget to update it!",
config_path, example_config_path)
return main_config.from_file(config_path, config_namespace)

View File

@ -34,3 +34,13 @@ class ConfigClass:
raise ValueError(f"{cls.__name__}.{f.name} is not configured!")
kwargs[f.name] = ConfigClass.parse(data[f.name], hints[f.name])
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)

View File

@ -1,7 +1,7 @@
from abc import ABCMeta, abstractmethod
from typing import Type, Optional
from typing import Type
import yaml
from yaml import safe_load
from kissconfig.config import ConfigClass
@ -11,8 +11,10 @@ class EmptyConfig (ValueError):
class Loader (metaclass=ABCMeta):
def __init__(self, namespace: Optional[str] = None):
self.namespace = namespace
def __init__(self, path: str = None,
example_path: str = None):
self.path = path
self.example_path = example_path
@abstractmethod
def parse_file(self, path: str) -> dict:
@ -29,5 +31,4 @@ class Loader (metaclass=ABCMeta):
class YamlLoader (Loader):
def parse_file(self, path: str) -> dict:
with open(path) as f:
return yaml.safe_load(f)
return safe_load(path)

View File

@ -1,16 +0,0 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "kissconfig"
version = "0.1.0"
authors = [{name="Ilya Bezrukov", email="bezrukoff888@gmail.com" }]
description = "Config with dataclasses and KISS"
readme = "README.md"
dependencies = ["pyyaml"]
requires-python = ">=3.10"
[project.urls]
"Homepage" = "https://git.ilbz.ru/brinza/kissconfig"
"Bug Tracker" = "https://git.ilbz.ru/brinza/kissconfig/issues"