Skip to content

Commit e7cf4d2

Browse files
committed
Add verbose parameter to functions
Changed: - `sey_key` and `get_key` have now verbose parameter. Default verbose was True for `get_key`, so `sey_key` and `get_key` have True by default. - `sey_key` use `with_warn_for_invalid_lines` so I added a verbose parameter to it which is by default True. - `parse` was also using `with_warn_for_invalid_lines`, it will by default be using `self.verbose` Resolve theskumar#467
1 parent 593813e commit e7cf4d2

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/dotenv/main.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,20 @@
2121
logger = logging.getLogger(__name__)
2222

2323

24-
def with_warn_for_invalid_lines(mappings: Iterator[Binding]) -> Iterator[Binding]:
24+
def with_warn_for_invalid_lines(mappings: Iterator[Binding], verbose: bool = True) -> Iterator[Binding]:
25+
"""
26+
The function `with_warn_for_invalid_lines` iterates over a collection of `Binding` objects and
27+
yields each object, while also logging a warning message if the object has an error and the
28+
`verbose` flag is set to `True`.
29+
30+
Parameters:
31+
mappings: An iterator of `Binding` objects. Each `Binding` object represents a line in a file being parsed. It contains information about the line number, the original line content, and any parsing errors encountered
32+
verbose: Whether or not to display warnings for invalid lines. If set to True, warnings will be displayed. If set to False, warnings will be suppressed, defaults to True
33+
Returns:
34+
Iterator[Binding]: An iterator of `Binding` objects, each representing a line in a file being parsed
35+
"""
2536
for mapping in mappings:
26-
if mapping.error:
37+
if mapping.error and verbose:
2738
logger.warning(
2839
"Python-dotenv could not parse statement starting at line %s",
2940
mapping.original.line,
@@ -80,7 +91,7 @@ def dict(self) -> Dict[str, Optional[str]]:
8091

8192
def parse(self) -> Iterator[Tuple[str, Optional[str]]]:
8293
with self._get_stream() as stream:
83-
for mapping in with_warn_for_invalid_lines(parse_stream(stream)):
94+
for mapping in with_warn_for_invalid_lines(parse_stream(stream), verbose=self.verbose):
8495
if mapping.key is not None:
8596
yield mapping.key, mapping.value
8697

@@ -116,14 +127,15 @@ def get(self, key: str) -> Optional[str]:
116127
def get_key(
117128
dotenv_path: StrPath,
118129
key_to_get: str,
130+
verbose: bool = True,
119131
encoding: Optional[str] = "utf-8",
120132
) -> Optional[str]:
121133
"""
122134
Get the value of a given key from the given .env.
123135
124136
Returns `None` if the key isn't found or doesn't have a value.
125137
"""
126-
return DotEnv(dotenv_path, verbose=True, encoding=encoding).get(key_to_get)
138+
return DotEnv(dotenv_path, verbose=verbose, encoding=encoding).get(key_to_get)
127139

128140

129141
@contextmanager
@@ -150,6 +162,7 @@ def set_key(
150162
value_to_set: str,
151163
quote_mode: str = "always",
152164
export: bool = False,
165+
verbose: bool = True,
153166
encoding: Optional[str] = "utf-8",
154167
) -> Tuple[Optional[bool], str, str]:
155168
"""
@@ -178,7 +191,7 @@ def set_key(
178191
with rewrite(dotenv_path, encoding=encoding) as (source, dest):
179192
replaced = False
180193
missing_newline = False
181-
for mapping in with_warn_for_invalid_lines(parse_stream(source)):
194+
for mapping in with_warn_for_invalid_lines(parse_stream(source), verbose=verbose):
182195
if mapping.key == key_to_set:
183196
dest.write(line_out)
184197
replaced = True
@@ -197,6 +210,7 @@ def unset_key(
197210
dotenv_path: StrPath,
198211
key_to_unset: str,
199212
quote_mode: str = "always",
213+
verbose: bool = True,
200214
encoding: Optional[str] = "utf-8",
201215
) -> Tuple[Optional[bool], str]:
202216
"""
@@ -211,7 +225,7 @@ def unset_key(
211225

212226
removed = False
213227
with rewrite(dotenv_path, encoding=encoding) as (source, dest):
214-
for mapping in with_warn_for_invalid_lines(parse_stream(source)):
228+
for mapping in with_warn_for_invalid_lines(parse_stream(source), verbose=verbose):
215229
if mapping.key == key_to_unset:
216230
removed = True
217231
else:

0 commit comments

Comments
 (0)