MCPcopy Create free account
hub / github.com/theskumar/python-dotenv / DotEnv

Class DotEnv

src/dotenv/main.py:34–113  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32
33
34class DotEnv:
35 def __init__(
36 self,
37 dotenv_path: Optional[StrPath],
38 stream: Optional[IO[str]] = None,
39 verbose: bool = False,
40 encoding: Optional[str] = None,
41 interpolate: bool = True,
42 override: bool = True,
43 ) -> None:
44 self.dotenv_path: Optional[StrPath] = dotenv_path
45 self.stream: Optional[IO[str]] = stream
46 self._dict: Optional[Dict[str, Optional[str]]] = None
47 self.verbose: bool = verbose
48 self.encoding: Optional[str] = encoding
49 self.interpolate: bool = interpolate
50 self.override: bool = override
51
52 @contextmanager
53 def _get_stream(self) -> Iterator[IO[str]]:
54 if self.dotenv_path and os.path.isfile(self.dotenv_path):
55 with open(self.dotenv_path, encoding=self.encoding) as stream:
56 yield stream
57 elif self.stream is not None:
58 yield self.stream
59 else:
60 if self.verbose:
61 logger.info(
62 "Python-dotenv could not find configuration file %s.",
63 self.dotenv_path or '.env',
64 )
65 yield io.StringIO('')
66
67 def dict(self) -> Dict[str, Optional[str]]:
68 """Return dotenv as dict"""
69 if self._dict:
70 return self._dict
71
72 raw_values = self.parse()
73
74 if self.interpolate:
75 self._dict = OrderedDict(resolve_variables(raw_values, override=self.override))
76 else:
77 self._dict = OrderedDict(raw_values)
78
79 return self._dict
80
81 def parse(self) -> Iterator[Tuple[str, Optional[str]]]:
82 with self._get_stream() as stream:
83 for mapping in with_warn_for_invalid_lines(parse_stream(stream)):
84 if mapping.key is not None:
85 yield mapping.key, mapping.value
86
87 def set_as_environment_variables(self) -> bool:
88 """
89 Load the current dotenv as system environment variable.
90 """
91 if not self.dict():

Callers 3

get_keyFunction · 0.85
load_dotenvFunction · 0.85
dotenv_valuesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected