diff options
author | romangraef <romangraef@loves.dicksinhisan.us> | 2018-07-06 10:35:54 +0200 |
---|---|---|
committer | romangraef <romangraef@loves.dicksinhisan.us> | 2018-07-06 10:35:54 +0200 |
commit | cbb9f2c3622ec96caf4ec9e58e84c8fbd3f45d23 (patch) | |
tree | bfe6aa26d7cab12334ae79b353ba2ec365f2b582 /configlib/model.py | |
download | configlib-cbb9f2c3622ec96caf4ec9e58e84c8fbd3f45d23.tar.gz configlib-cbb9f2c3622ec96caf4ec9e58e84c8fbd3f45d23.tar.bz2 configlib-cbb9f2c3622ec96caf4ec9e58e84c8fbd3f45d23.zip |
Initial commit
Diffstat (limited to 'configlib/model.py')
-rw-r--r-- | configlib/model.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/configlib/model.py b/configlib/model.py new file mode 100644 index 0000000..6d79464 --- /dev/null +++ b/configlib/model.py @@ -0,0 +1,41 @@ +import json +from abc import abstractmethod, ABC +from typing import TypeVar, Type, Union, AnyStr, TextIO + +_T = TypeVar('_T', bound='Config') + + +class InvalidConfigTypingException(Exception): + pass + + +class ConfigValueMissingException(Exception): + pass + + +class Config(ABC): + + @classmethod + @abstractmethod + def get_name(cls) -> str: + pass + + @classmethod + @abstractmethod + def parse_dict(cls: Type[_T], data: dict) -> _T: + pass + + @classmethod + def load(cls: Type[_T], file: Union[AnyStr, TextIO]) -> _T: + if hasattr(file, 'read'): + return cls.loads(file.read()) + with open(file) as fp: + return cls.load(fp) + + @classmethod + def loads(cls: Type[_T], text: str) -> _T: + return cls.parse_dict(json.loads(text)) + + @classmethod + def get_instance(cls: Type[_T]) -> _T: + return cls |