MCPcopy Index your code
hub / github.com/theskumar/python-dotenv / DotEnv

Class DotEnv

src/dotenv/main.py:42–122  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 3

get_keyFunction · 0.85
load_dotenvFunction · 0.85
dotenv_valuesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…