当前位置:   article > 正文

python 读取.env配置文件_python .env

python .env

python 读取.env配置文件

  • 目录结构

    project/
    ├─config
    │  └─__init__.py
    |  └─base.py
    |  └─conf.py
    ├─.env
    ├─.env.example
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • conf.py

    #!/usr/bin/python3
    # -*- coding:utf-8 -*-
    # @Author         : Charlie Zhang
    # @Email          : charlie.zhang@wiwide.com
    # @Time           : 2021/7/20 16:40
    # @Version        : 1.0
    # @File           : conf.py
    # @Software       : PyCharm
    
    import os
    import typing
    from collections.abc import MutableMapping
    from pathlib import Path
    
    
    class undefined:
        pass
    
    
    class EnvironError(Exception):
        pass
    
    
    class Environ(MutableMapping):
        def __init__(self, environ: typing.MutableMapping = os.environ):
            self._environ = environ
            self._has_been_read = set()  # type: typing.Set[typing.Any]
    
        def __getitem__(self, key: typing.Any) -> typing.Any:
            self._has_been_read.add(key)
            return self._environ.__getitem__(key)
    
        def __setitem__(self, key: typing.Any, value: typing.Any) -> None:
            if key in self._has_been_read:
                raise EnvironError(
                    f"Attempting to set environ['{key}'], but the value has already been read."
                )
            self._environ.__setitem__(key, value)
    
        def __delitem__(self, key: typing.Any) -> None:
            if key in self._has_been_read:
                raise EnvironError(
                    f"Attempting to delete environ['{key}'], but the value has already been read."
                )
            self._environ.__delitem__(key)
    
        def __iter__(self) -> typing.Iterator:
            return iter(self._environ)
    
        def __len__(self) -> int:
            return len(self._environ)
    
    
    environ = Environ()
    
    
    class Config:
        def __init__(
                self,
                env_file: typing.Union[str, Path] = None,
                environ: typing.Mapping[str, str] = environ,
        ) -> None:
            self.environ = environ
            self.file_values = {}  # type: typing.Dict[str, str]
            if env_file is not None and os.path.isfile(env_file):
                self.file_values = self._read_file(env_file)
    
        def __call__(
                self, key: str, cast: typing.Callable = None, default: typing.Any = undefined,
        ) -> typing.Any:
            return self.get(key, cast, default)
    
        def get(
                self, key: str, cast: typing.Callable = None, default: typing.Any = undefined,
        ) -> typing.Any:
            if key in self.environ:
                value = self.environ[key]
                return self._perform_cast(key, value, cast)
            if key in self.file_values:
                value = self.file_values[key]
                return self._perform_cast(key, value, cast)
            if default is not undefined:
                return self._perform_cast(key, default, cast)
            raise KeyError(f"Config '{key}' is missing, and has no default.")
    
        def _read_file(self, file_name: typing.Union[str, Path]) -> typing.Dict[str, str]:
            file_values = {}  # type: typing.Dict[str, str]
            with open(file_name) as input_file:
                for line in input_file.readlines():
                    line = line.strip()
                    if "=" in line and not line.startswith("#"):
                        key, value = line.split("=", 1)
                        key = key.strip()
                        value = value.strip().strip("\"'")
                        file_values[key] = value
            return file_values
    
        def _perform_cast(
                self, key: str, value: typing.Any, cast: typing.Callable = None,
        ) -> typing.Any:
            if cast is None or value is None:
                return value
            elif cast is bool and isinstance(value, str):
                mapping = {"true": True, "1": True, "false": False, "0": False}
                value = value.lower()
                if value not in mapping:
                    raise ValueError(
                        f"Config '{key}' has value '{value}'. Not a valid bool."
                    )
                return mapping[value]
            try:
                return cast(value)
            except (TypeError, ValueError):
                raise ValueError(
                    f"Config '{key}' has value '{value}'. Not a valid {cast.__name__}."
                )
    
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    '
    运行
  • base.py

    import os
    from .conf import Config
    
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    config = Config(os.path.join(BASE_DIR, ".env"))
    KAFKA_HOSTS: str = config('KAFKA_HOSTS', cast=str, default='192.168.4.54:9092')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • init.py

    from .base import *
    
    
    • 1
    • 2
  • .env

    KAFKA_HOSTS = ""
    
    • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/959114
推荐阅读
相关标签
  

闽ICP备14008679号