Skip to content

Commit 635c9af

Browse files
authored
Merge pull request #25 from Avasam/replace-autocommand-with-typer
Replaced ``autocommand`` usage with ``Typer``
2 parents 6734c8a + 15b61de commit 635c9af

5 files changed

Lines changed: 38 additions & 17 deletions

File tree

jaraco/text/__init__.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

33
import functools
4+
import io
45
import itertools
6+
import os
57
import re
68
import sys
79
import textwrap
@@ -13,6 +15,8 @@
1315
Protocol,
1416
SupportsIndex,
1517
TypeVar,
18+
Union,
19+
cast,
1620
overload,
1721
)
1822

@@ -31,6 +35,9 @@
3135
_T_co = TypeVar("_T_co", covariant=True)
3236
# Same as builtins._GetItemIterable from typeshed
3337
_GetItemIterable: TypeAlias = SupportsGetItem[int, _T_co]
38+
Openable: TypeAlias = FileDescriptorOrPath
39+
else:
40+
Openable = Union[str, bytes, os.PathLike, int]
3441

3542
_T = TypeVar("_T")
3643

@@ -688,9 +695,15 @@ def join_continuation(lines: _GetItemIterable[str]) -> Generator[str]:
688695
yield item
689696

690697

698+
# https://docs.python.org/3/library/io.html#io.TextIOBase.newlines
699+
NewlineSpec: TypeAlias = Union[str, tuple[str, ...], None]
700+
701+
702+
@functools.singledispatch
691703
def read_newlines(
692-
filename: FileDescriptorOrPath, limit: int | None = 1024
693-
) -> str | tuple[str, ...] | None:
704+
filename: Union[Openable, io.TextIOWrapper], # noqa: UP007 # singledispatch uses the annotation at runtime (python 3.9)
705+
limit: int | None = 1024,
706+
) -> NewlineSpec:
694707
r"""
695708
>>> tmp_path = getfixture('tmp_path')
696709
>>> filename = tmp_path / 'out.txt'
@@ -704,9 +717,21 @@ def read_newlines(
704717
>>> read_newlines(filename)
705718
('\r', '\n', '\r\n')
706719
"""
720+
if sys.version_info >= (3, 10):
721+
assert isinstance(filename, Openable)
722+
else: # pragma: no cover
723+
filename = cast(Openable, filename)
707724
with open(filename, encoding='utf-8') as fp:
708-
fp.read(limit)
709-
return fp.newlines
725+
return read_newlines(fp, limit=limit)
726+
727+
728+
@read_newlines.register
729+
def _(
730+
filename: io.TextIOWrapper,
731+
limit: Union[int, None] = 1024, # noqa: UP007 # singledispatch uses the annotation at runtime (python 3.9)
732+
) -> NewlineSpec:
733+
filename.read(limit)
734+
return filename.newlines
710735

711736

712737
def lines_from(input: Traversable) -> Generator[str]:

jaraco/text/show-newlines.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
4-
5-
import autocommand
63
import inflect
4+
import typer
75
from more_itertools import always_iterable
86

97
import jaraco.text
108

11-
if TYPE_CHECKING:
12-
from _typeshed import FileDescriptorOrPath
13-
149

15-
def report_newlines(filename: FileDescriptorOrPath) -> None:
10+
def report_newlines(input: typer.FileText) -> None:
1611
r"""
1712
Report the newlines in the indicated file.
1813
@@ -26,7 +21,7 @@ def report_newlines(filename: FileDescriptorOrPath) -> None:
2621
>>> report_newlines(filename)
2722
newlines are ('\n', '\r\n')
2823
"""
29-
newlines = jaraco.text.read_newlines(filename)
24+
newlines = jaraco.text.read_newlines(input)
3025
count = len(tuple(always_iterable(newlines)))
3126
engine = inflect.engine()
3227
print(
@@ -37,4 +32,4 @@ def report_newlines(filename: FileDescriptorOrPath) -> None:
3732
)
3833

3934

40-
autocommand.autocommand(__name__)(report_newlines)
35+
__name__ == '__main__' and typer.run(report_newlines) # type: ignore[func-returns-value]

jaraco/text/strip-prefix.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22

3-
import autocommand
3+
import typer
44

55
from jaraco.text import Stripper
66

@@ -18,4 +18,4 @@ def strip_prefix() -> None:
1818
sys.stdout.writelines(Stripper.strip_prefix(sys.stdin).lines)
1919

2020

21-
autocommand.autocommand(__name__)(strip_prefix)
21+
__name__ == '__main__' and typer.run(strip_prefix) # type: ignore[func-returns-value]

newsfragments/25.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replaced ``autocommand`` usage with ``Typer`` -- by :user:`Avasam`

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ classifiers = [
2323
requires-python = ">=3.9"
2424
license = "MIT"
2525
dependencies = [
26-
"jaraco.functools",
2726
"jaraco.context >= 4.1",
28-
"autocommand",
27+
"jaraco.functools",
2928
"more_itertools",
29+
"typer-slim",
3030
]
3131
dynamic = ["version"]
3232

0 commit comments

Comments
 (0)