Skip to content

Commit 234fc86

Browse files
Tweak stdout fix and apply pre-commits
1 parent 4eccd3a commit 234fc86

4 files changed

Lines changed: 53 additions & 20 deletions

File tree

src/usethis/_config.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1+
from collections.abc import Generator
2+
from contextlib import contextmanager
3+
14
import typer
25
from pydantic import BaseModel
3-
from contextlib import contextmanager
4-
from typing import Generator
6+
57

68
class UsethisConfig(BaseModel):
79
"""Global-state for command options which affect low level behaviour."""
810

911
offline: bool
1012
quiet: bool
11-
frozen: bool = False
13+
frozen: bool = False
1214

1315
@contextmanager
1416
def set(
15-
self, *, offline: bool | None = None, quiet: bool | None = None, frozen: bool | None = None
17+
self,
18+
*,
19+
offline: bool | None = None,
20+
quiet: bool | None = None,
21+
frozen: bool | None = None,
1622
) -> Generator[None, None, None]:
1723
"""Temporarily change command options."""
1824
old_offline = self.offline
@@ -38,7 +44,9 @@ def set(
3844
_OFFLINE_DEFAULT = False
3945
_QUIET_DEFAULT = False
4046

41-
usethis_config = UsethisConfig(offline=_OFFLINE_DEFAULT, quiet=_QUIET_DEFAULT, frozen=False)
47+
usethis_config = UsethisConfig(
48+
offline=_OFFLINE_DEFAULT, quiet=_QUIET_DEFAULT, frozen=False
49+
)
4250

4351
offline_opt = typer.Option(_OFFLINE_DEFAULT, "--offline", help="Disable network access")
4452
quiet_opt = typer.Option(_QUIET_DEFAULT, "--quiet", help="Suppress output")

src/usethis/_console.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1+
import codecs
12
import sys
2-
sys.stdout.reconfigure(encoding='utf-8')
33

44
from rich.console import Console
55

66
from usethis._config import usethis_config
77

8+
# Unicode support - but we need to be able to write bytes
9+
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.buffer)
10+
811
console = Console()
912

1013

1114
def tick_print(msg: str | Exception) -> None:
1215
msg = str(msg)
13-
14-
if not usethis_config.quiet:
15-
console.print(f"{'✔'.encode('utf-8', 'ignore').decode('utf-8')} {msg}", style="green")
1616

17+
if not usethis_config.quiet:
18+
console.print(
19+
f"{'✔'.encode('utf-8', 'ignore').decode('utf-8')} {msg}", style="green"
20+
)
1721

1822

1923
def box_print(msg: str | Exception) -> None:

src/usethis/_integrations/uv/call.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
from usethis._config import usethis_config
12
from usethis._integrations.pyproject.io_ import read_pyproject_toml_from_path
23
from usethis._integrations.uv.errors import UVSubprocessFailedError
34
from usethis._subprocess import SubprocessFailedError, call_subprocess
4-
from usethis._config import usethis_config
5+
56

67
def call_uv_subprocess(args: list[str]) -> str:
78
"""Run a subprocess using the uv command-line tool.

src/usethis/_interface/tool.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
False, "--remove", help="Remove the tool instead of adding it."
2323
)
2424

25-
frozen_opt = typer.Option(
26-
False,"--frozen", help="Use the frozen dependencies."
27-
)
25+
frozen_opt = typer.Option(False, "--frozen", help="Use the frozen dependencies.")
26+
2827

2928
@app.command(help="Use the coverage code coverage measurement tool.")
3029
def coverage(
31-
remove: bool = remove_opt, offline: bool = offline_opt, quiet: bool = quiet_opt, frozen: bool = frozen_opt
30+
remove: bool = remove_opt,
31+
offline: bool = offline_opt,
32+
quiet: bool = quiet_opt,
33+
frozen: bool = frozen_opt,
3234
) -> None:
3335
with usethis_config.set(offline=offline, quiet=quiet, frozen=frozen):
3436
_run_tool(use_coverage, remove=remove)
@@ -38,7 +40,10 @@ def coverage(
3840
help="Use the deptry linter: avoid missing or superfluous dependency declarations."
3941
)
4042
def deptry(
41-
remove: bool = remove_opt, offline: bool = offline_opt, quiet: bool = quiet_opt, frozen: bool = frozen_opt
43+
remove: bool = remove_opt,
44+
offline: bool = offline_opt,
45+
quiet: bool = quiet_opt,
46+
frozen: bool = frozen_opt,
4247
) -> None:
4348
with usethis_config.set(offline=offline, quiet=quiet, frozen=frozen):
4449
_run_tool(use_deptry, remove=remove)
@@ -48,7 +53,10 @@ def deptry(
4853
help="Use the pre-commit framework to manage and maintain pre-commit hooks."
4954
)
5055
def pre_commit(
51-
remove: bool = remove_opt, offline: bool = offline_opt, quiet: bool = quiet_opt, frozen: bool = frozen_opt
56+
remove: bool = remove_opt,
57+
offline: bool = offline_opt,
58+
quiet: bool = quiet_opt,
59+
frozen: bool = frozen_opt,
5260
) -> None:
5361
with usethis_config.set(offline=offline, quiet=quiet, frozen=frozen):
5462
_run_tool(use_pre_commit, remove=remove)
@@ -58,15 +66,21 @@ def pre_commit(
5866
help="Use the pyproject-fmt linter: opinionated formatting of 'pyproject.toml' files."
5967
)
6068
def pyproject_fmt(
61-
remove: bool = remove_opt, offline: bool = offline_opt, quiet: bool = quiet_opt, frozen: bool = frozen_opt
69+
remove: bool = remove_opt,
70+
offline: bool = offline_opt,
71+
quiet: bool = quiet_opt,
72+
frozen: bool = frozen_opt,
6273
) -> None:
6374
with usethis_config.set(offline=offline, quiet=quiet, frozen=frozen):
6475
_run_tool(use_pyproject_fmt, remove=remove)
6576

6677

6778
@app.command(help="Use the pytest testing framework.")
6879
def pytest(
69-
remove: bool = remove_opt, offline: bool = offline_opt, quiet: bool = quiet_opt, frozen: bool = frozen_opt
80+
remove: bool = remove_opt,
81+
offline: bool = offline_opt,
82+
quiet: bool = quiet_opt,
83+
frozen: bool = frozen_opt,
7084
) -> None:
7185
with usethis_config.set(offline=offline, quiet=quiet, frozen=frozen):
7286
_run_tool(use_pytest, remove=remove)
@@ -77,15 +91,21 @@ def pytest(
7791
help="Use a requirements.txt file exported from the uv lockfile.",
7892
)
7993
def requirements_txt(
80-
remove: bool = remove_opt, offline: bool = offline_opt, quiet: bool = quiet_opt, frozen: bool = frozen_opt
94+
remove: bool = remove_opt,
95+
offline: bool = offline_opt,
96+
quiet: bool = quiet_opt,
97+
frozen: bool = frozen_opt,
8198
) -> None:
8299
with usethis_config.set(offline=offline, quiet=quiet, frozen=frozen):
83100
_run_tool(use_requirements_txt, remove=remove)
84101

85102

86103
@app.command(help="Use Ruff: an extremely fast Python linter and code formatter.")
87104
def ruff(
88-
remove: bool = remove_opt, offline: bool = offline_opt, quiet: bool = quiet_opt, frozen: bool = frozen_opt
105+
remove: bool = remove_opt,
106+
offline: bool = offline_opt,
107+
quiet: bool = quiet_opt,
108+
frozen: bool = frozen_opt,
89109
) -> None:
90110
with usethis_config.set(offline=offline, quiet=quiet, frozen=frozen):
91111
_run_tool(use_ruff, remove=remove)

0 commit comments

Comments
 (0)