Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verbose parameter to functions #479

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@
logger = logging.getLogger(__name__)


def with_warn_for_invalid_lines(mappings: Iterator[Binding]) -> Iterator[Binding]:
def with_warn_for_invalid_lines(mappings: Iterator[Binding], verbose: bool = True) -> Iterator[Binding]:
"""
The function `with_warn_for_invalid_lines` iterates over a collection of `Binding` objects and
yields each object, while also logging a warning message if the object has an error and the
`verbose` flag is set to `True`.

Parameters:
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
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
Returns:
Iterator[Binding]: An iterator of `Binding` objects, each representing a line in a file being parsed
"""
for mapping in mappings:
if mapping.error:
if mapping.error and verbose:
logger.warning(
"Python-dotenv could not parse statement starting at line %s",
mapping.original.line,
Expand Down Expand Up @@ -80,7 +91,7 @@ def dict(self) -> Dict[str, Optional[str]]:

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

Expand Down Expand Up @@ -116,14 +127,15 @@ def get(self, key: str) -> Optional[str]:
def get_key(
dotenv_path: StrPath,
key_to_get: str,
verbose: bool = True,
encoding: Optional[str] = "utf-8",
) -> Optional[str]:
"""
Get the value of a given key from the given .env.

Returns `None` if the key isn't found or doesn't have a value.
"""
return DotEnv(dotenv_path, verbose=True, encoding=encoding).get(key_to_get)
return DotEnv(dotenv_path, verbose=verbose, encoding=encoding).get(key_to_get)


@contextmanager
Expand All @@ -150,6 +162,7 @@ def set_key(
value_to_set: str,
quote_mode: str = "always",
export: bool = False,
verbose: bool = True,
encoding: Optional[str] = "utf-8",
) -> Tuple[Optional[bool], str, str]:
"""
Expand Down Expand Up @@ -178,7 +191,7 @@ def set_key(
with rewrite(dotenv_path, encoding=encoding) as (source, dest):
replaced = False
missing_newline = False
for mapping in with_warn_for_invalid_lines(parse_stream(source)):
for mapping in with_warn_for_invalid_lines(parse_stream(source), verbose=verbose):
if mapping.key == key_to_set:
dest.write(line_out)
replaced = True
Expand All @@ -197,6 +210,7 @@ def unset_key(
dotenv_path: StrPath,
key_to_unset: str,
quote_mode: str = "always",
verbose: bool = True,
encoding: Optional[str] = "utf-8",
) -> Tuple[Optional[bool], str]:
"""
Expand All @@ -211,7 +225,7 @@ def unset_key(

removed = False
with rewrite(dotenv_path, encoding=encoding) as (source, dest):
for mapping in with_warn_for_invalid_lines(parse_stream(source)):
for mapping in with_warn_for_invalid_lines(parse_stream(source), verbose=verbose):
if mapping.key == key_to_unset:
removed = True
else:
Expand Down