35 lines
893 B
Python
35 lines
893 B
Python
from abc import ABCMeta, abstractmethod
|
|
from typing import Type
|
|
|
|
from yaml import safe_load
|
|
|
|
from kissconfig.config import ConfigClass
|
|
|
|
|
|
class EmptyConfig (ValueError):
|
|
pass
|
|
|
|
|
|
class Loader (metaclass=ABCMeta):
|
|
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:
|
|
pass
|
|
|
|
def from_file(self, config_class: Type[ConfigClass], path: str) -> ConfigClass:
|
|
data = self.parse_file(path)
|
|
if not data:
|
|
raise EmptyConfig("Configuration file seems to be empty")
|
|
if self.namespace is not None:
|
|
data = data.get(self.namespace, {})
|
|
return config_class.from_dict(data)
|
|
|
|
|
|
class YamlLoader (Loader):
|
|
def parse_file(self, path: str) -> dict:
|
|
return safe_load(path)
|