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:
parent
317043cc97
commit
d6b2c9b15c
5 changed files with 59 additions and 46 deletions
|
@ -95,8 +95,6 @@ Configuration
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
from .config import *
|
from .config import *
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
from .sources import *
|
|
||||||
# noinspection PyUnresolvedReferences
|
|
||||||
from .errors import *
|
from .errors import *
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
from .customtyping import *
|
from .customtyping import *
|
||||||
|
|
|
@ -8,7 +8,9 @@ import logging
|
||||||
import collections
|
import collections
|
||||||
from . import errors
|
from . import errors
|
||||||
from . import customtyping as ct
|
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__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -19,8 +21,8 @@ class Configuration:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
DEFAULT_SOURCES = [
|
DEFAULT_SOURCES = [
|
||||||
s.EnvironmentSource(),
|
EnvironmentSource(),
|
||||||
s.EnvironmentFileSource(),
|
EnvironmentFileSource(),
|
||||||
]
|
]
|
||||||
"""
|
"""
|
||||||
The sources used in :meth:`__init__` if no other source is specified.
|
The sources used in :meth:`__init__` if no other source is specified.
|
||||||
|
@ -73,14 +75,14 @@ class Configuration:
|
||||||
log.debug(f"Unresolving: {item!r}")
|
log.debug(f"Unresolving: {item!r}")
|
||||||
del item.__wrapped__
|
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`.
|
Create a new :class:`Configuration`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
log.debug(f"Initializing a new {self.__class__.__qualname__} object...")
|
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.
|
Collection of sources to use for values of this configuration.
|
||||||
"""
|
"""
|
||||||
|
|
23
cfig/sources/base.py
Normal file
23
cfig/sources/base.py
Normal 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",
|
||||||
|
)
|
|
@ -1,18 +1,6 @@
|
||||||
import typing as t
|
|
||||||
import abc
|
|
||||||
import os
|
import os
|
||||||
|
import typing as t
|
||||||
|
from cfig.sources.base import Source
|
||||||
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
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class EnvironmentSource(Source):
|
class EnvironmentSource(Source):
|
||||||
|
@ -49,28 +37,3 @@ class EnvironmentSource(Source):
|
||||||
key = self._process_key(key)
|
key = self._process_key(key)
|
||||||
return self.environment.get(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
27
cfig/sources/envfile.py
Normal 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",
|
||||||
|
)
|
Loading…
Reference in a new issue