1
Fork 0
mirror of https://github.com/Steffo99/cfig.git synced 2024-11-21 23:44:21 +00:00

🔧 Split sources into their own namespace package

This commit is contained in:
Steffo 2022-04-19 01:32:10 +02:00
parent 317043cc97
commit d6b2c9b15c
Signed by: steffo
GPG key ID: 6965406171929D01
5 changed files with 59 additions and 46 deletions

View file

@ -95,8 +95,6 @@ Configuration
# noinspection PyUnresolvedReferences
from .config import *
# noinspection PyUnresolvedReferences
from .sources import *
# noinspection PyUnresolvedReferences
from .errors import *
# noinspection PyUnresolvedReferences
from .customtyping import *

View file

@ -8,7 +8,9 @@ import logging
import collections
from . import errors
from . import customtyping as ct
from . import sources as s
from cfig.sources.base import Source
from cfig.sources.env import EnvironmentSource
from cfig.sources.envfile import EnvironmentFileSource
log = logging.getLogger(__name__)
@ -19,8 +21,8 @@ class Configuration:
"""
DEFAULT_SOURCES = [
s.EnvironmentSource(),
s.EnvironmentFileSource(),
EnvironmentSource(),
EnvironmentFileSource(),
]
"""
The sources used in :meth:`__init__` if no other source is specified.
@ -73,14 +75,14 @@ class Configuration:
log.debug(f"Unresolving: {item!r}")
del item.__wrapped__
def __init__(self, *, sources: t.Optional[t.Collection[s.Source]] = None):
def __init__(self, *, sources: t.Optional[t.Collection[Source]] = None):
"""
Create a new :class:`Configuration`.
"""
log.debug(f"Initializing a new {self.__class__.__qualname__} object...")
self.sources: t.Collection[s.Source] = sources or self.DEFAULT_SOURCES
self.sources: t.Collection[Source] = sources or self.DEFAULT_SOURCES
"""
Collection of sources to use for values of this configuration.
"""

23
cfig/sources/base.py Normal file
View file

@ -0,0 +1,23 @@
import abc
import typing as t
class Source(metaclass=abc.ABCMeta):
"""
A source of values to be tapped by configurations.
**Abstract class.** Cannot be instantiated. Should be inherited from other source classes.
Other packages can add more sources directly to the :mod:`cfig.sources` namespace package.
"""
@abc.abstractmethod
def get(self, key: str) -> t.Optional[str]:
"""
Get the value with the given key from the source.
"""
__all__ = (
"Source",
)

View file

@ -1,18 +1,6 @@
import typing as t
import abc
import os
class Source(metaclass=abc.ABCMeta):
"""
A source of values to be tapped by configurations.
"""
@abc.abstractmethod
def get(self, key: str) -> t.Optional[str]:
"""
Get the value with the given key from the source
"""
import typing as t
from cfig.sources.base import Source
class EnvironmentSource(Source):
@ -49,28 +37,3 @@ class EnvironmentSource(Source):
key = self._process_key(key)
return self.environment.get(key)
class EnvironmentFileSource(EnvironmentSource):
"""
A source which gets values from files at paths specified in environment variables.
"""
def __init__(self, *, prefix: str = "", suffix: str = "_FILE", environment=os.environ):
super().__init__(prefix=prefix, suffix=suffix, environment=environment)
def get(self, key: str) -> t.Optional[str]:
path = super().get(key)
if path is None:
return None
try:
with open(path, "r") as file:
return file.read()
except FileNotFoundError:
return None
__all__ = (
"Source",
"EnvironmentSource",
"EnvironmentFileSource",
)

27
cfig/sources/envfile.py Normal file
View file

@ -0,0 +1,27 @@
import os
import typing as t
from cfig.sources.env import EnvironmentSource
class EnvironmentFileSource(EnvironmentSource):
"""
A source which gets values from files at paths specified in environment variables.
"""
def __init__(self, *, prefix: str = "", suffix: str = "_FILE", environment=os.environ):
super().__init__(prefix=prefix, suffix=suffix, environment=environment)
def get(self, key: str) -> t.Optional[str]:
path = super().get(key)
if path is None:
return None
try:
with open(path, "r") as file:
return file.read()
except FileNotFoundError:
return None
__all__ = (
"EnvironmentFileSource",
)