1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
from __future__ import annotations
from dynaconf.utils import upperfy
class PrefixFilter:
"""Filter environment variables by prefix.
Examples:
Please see [Prefix filtering][prefix-filtering] in the advanced usage
section.
"""
def __init__(self, prefix: str):
"""Initialises the PrefixFilter class.
Args:
prefix: The prefix to filter on.
"""
if not isinstance(prefix, str):
raise TypeError("`SETTINGS_FILE_PREFIX` must be str")
self.prefix = f"{upperfy(prefix)}_"
def __call__(self, data):
"""Filter incoming data by prefix."""
len_prefix = len(self.prefix)
return {
upperfy(key[len_prefix:]): value
for key, value in data.items()
if upperfy(key[:len_prefix]) == self.prefix
}
|