Skip to content

Commit

Permalink
Add global option for overriding the default verbosity level (#34)
Browse files Browse the repository at this point in the history
For example, setting:

    [tool.poe]
    verbosity = -1

Will make all commands quiet unless --verbose is passed.

Authored-by: Artemis21 <artemisdev21@gmail.com>
  • Loading branch information
Artemis21 authored Aug 22, 2021
1 parent 893e22d commit 582f5a6
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 5 deletions.
16 changes: 16 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,22 @@ You can also specify an env file (with bashlike syntax) to load for all tasks li
[tool.poe]
envfile = ".env"
Default command verbosity
-------------------------

You can configure the verbosity level for poe commands by passing `--quiet` or
`--verbose` on the CLI. If you want to change the default verbosity level for
all commands, you can use the :toml:`tool.poe.verbose` option in pyproject.toml
like so:

.. code-block:: toml
[tool.poe]
verbosity = -1
:toml:`-1` is equivalent to :bash:`--quiet` and :toml:`1` is equivalent to
:bash:`--verbose`. :toml:`0` is the default.

Run poe from anywhere
---------------------

Expand Down
2 changes: 1 addition & 1 deletion poethepoet/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.11.0b3"
__version__ = "0.11.0b4"
2 changes: 2 additions & 0 deletions poethepoet/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def __call__(self, cli_args: Sequence[str]) -> int:
self.print_help(error=error)
return 1

self.ui.set_default_verbosity(self.config.verbosity)

if self.ui["help"]:
self.print_help()
return 0
Expand Down
10 changes: 10 additions & 0 deletions poethepoet/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PoeConfig:
"env": dict,
"envfile": str,
"executor": dict,
"verbosity": int,
}

def __init__(
Expand Down Expand Up @@ -56,6 +57,10 @@ def global_env(self) -> Dict[str, Union[str, Dict[str, str]]]:
def global_envfile(self) -> Optional[str]:
return self._poe.get("envfile")

@property
def verbosity(self) -> int:
return self._poe.get("verbosity", 0)

@property
def project(self) -> Any:
return self._project
Expand Down Expand Up @@ -134,6 +139,11 @@ def validate(self):
if error is None:
continue
raise PoeException(error)
# Validate default verbosity.
if self.verbosity < -1 or self.verbosity > 1:
raise PoeException(
f"Invalid value for option `verbosity`: {self.verbosity!r}. Should be between -1 and 1."
)

def find_pyproject_toml(self, target_dir: Optional[str] = None) -> Path:
"""
Expand Down
11 changes: 8 additions & 3 deletions poethepoet/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,13 @@ def build_parser(self) -> argparse.ArgumentParser:
def parse_args(self, cli_args: Sequence[str]):
self.parser = self.build_parser()
self.args = self.parser.parse_args(cli_args)
self.verbosity: int = self["verbosity"] or 0
self._color.with_colors(self.args.ansi)

def set_default_verbosity(self, default_verbosity: int):
if not self.verbosity:
self.verbosity = default_verbosity

def print_help(
self,
tasks: Optional[Mapping[str, Tuple[str, Sequence[Tuple[str, str]]]]] = None,
Expand All @@ -140,7 +145,7 @@ def print_help(
# TODO: See if this can be done nicely with a custom HelpFormatter

# Ignore verbosity mode if help flag is set
verbosity = 0 if self["help"] else self["verbosity"]
verbosity = 0 if self["help"] else self.verbosity

result: List[Union[str, Sequence[str]]] = []
if verbosity >= 0:
Expand Down Expand Up @@ -221,7 +226,7 @@ def _padr(text: str, width: int):
return text + " " * (width - len(text))

def print_msg(self, message: str, verbosity=0, end="\n"):
if verbosity <= self["verbosity"]:
if verbosity <= self.verbosity:
self._print(message, end=end)

def print_error(self, error: Exception):
Expand All @@ -230,7 +235,7 @@ def print_error(self, error: Exception):
self._print(f"<error> From: {error.cause} </error>") # type: ignore

def print_version(self):
if self["verbosity"] >= 0:
if self.verbosity >= 0:
result = f"Poe the poet - version: <em>{__version__}</em>\n"
else:
result = f"{__version__}\n"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "poethepoet"
version = "0.11.0b3"
version = "0.11.0b4"
description = "A task runner that works well with poetry."
authors = ["Nat Noordanus <n@natn.me>"]
readme = "README.rst"
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def scripts_project_path():
return PROJECT_ROOT.joinpath("tests", "fixtures", "scripts_project")


@pytest.fixture
def verbosity_default_project_path():
return PROJECT_ROOT.joinpath("tests", "fixtures", "verbosity_default")


@pytest.fixture(scope="function")
def temp_file(tmp_path):
# not using NamedTemporaryFile here because it doesn't work on windows
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/verbosity_default/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tool.poe]
verbosity = -1

[tool.poe.tasks]
test = "echo Hello there!"
14 changes: 14 additions & 0 deletions tests/test_poe_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@ def test_setting_global_env_vars(run_poe_subproc, is_windows):
assert result.capture == f"Poe => echo from EARTH to\nPoe => travel[1]\n"
assert result.stdout == f"from EARTH to\nMARS\n"
assert result.stderr == ""


def test_setting_default_verbosity(run_poe_subproc, verbosity_default_project_path):
result = run_poe_subproc("test", cwd=verbosity_default_project_path,)
assert result.capture == ""
assert result.stdout == "Hello there!\n"
assert result.stderr == ""


def test_override_default_verbosity(run_poe_subproc, verbosity_default_project_path):
result = run_poe_subproc("-v", "test", cwd=verbosity_default_project_path,)
assert result.capture == "Poe => echo Hello there!\n"
assert result.stdout == "Hello there!\n"
assert result.stderr == ""

0 comments on commit 582f5a6

Please sign in to comment.