File: filtering.py

package info (click to toggle)
python-dynaconf 3.1.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,116 kB
  • sloc: python: 12,959; makefile: 4
file content (17 lines) | stat: -rw-r--r-- 532 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from dynaconf.utils import upperfy


class PrefixFilter:
    def __init__(self, prefix):
        if not isinstance(prefix, str):
            raise TypeError("`SETTINGS_FILE_PREFIX` must be str")
        self.prefix = "{}_".format(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
        }