File: filtering.py

package info (click to toggle)
python-dynaconf 3.2.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,900 kB
  • sloc: python: 21,464; sh: 9; makefile: 4
file content (31 lines) | stat: -rw-r--r-- 851 bytes parent folder | download
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
        }