diff --git a/jaraco/text/__init__.py b/jaraco/text/__init__.py index 42666e9..bd8f86f 100644 --- a/jaraco/text/__init__.py +++ b/jaraco/text/__init__.py @@ -1,7 +1,9 @@ from __future__ import annotations import functools +import io import itertools +import os import re import sys import textwrap @@ -13,6 +15,8 @@ Protocol, SupportsIndex, TypeVar, + Union, + cast, overload, ) @@ -31,6 +35,9 @@ _T_co = TypeVar("_T_co", covariant=True) # Same as builtins._GetItemIterable from typeshed _GetItemIterable: TypeAlias = SupportsGetItem[int, _T_co] + Openable: TypeAlias = FileDescriptorOrPath +else: + Openable = Union[str, bytes, os.PathLike, int] _T = TypeVar("_T") @@ -688,9 +695,15 @@ def join_continuation(lines: _GetItemIterable[str]) -> Generator[str]: yield item +# https://docs.python.org/3/library/io.html#io.TextIOBase.newlines +NewlineSpec: TypeAlias = Union[str, tuple[str, ...], None] + + +@functools.singledispatch def read_newlines( - filename: FileDescriptorOrPath, limit: int | None = 1024 -) -> str | tuple[str, ...] | None: + filename: Union[Openable, io.TextIOWrapper], # noqa: UP007 # singledispatch uses the annotation at runtime (python 3.9) + limit: int | None = 1024, +) -> NewlineSpec: r""" >>> tmp_path = getfixture('tmp_path') >>> filename = tmp_path / 'out.txt' @@ -704,9 +717,21 @@ def read_newlines( >>> read_newlines(filename) ('\r', '\n', '\r\n') """ + if sys.version_info >= (3, 10): + assert isinstance(filename, Openable) + else: # pragma: no cover + filename = cast(Openable, filename) with open(filename, encoding='utf-8') as fp: - fp.read(limit) - return fp.newlines + return read_newlines(fp, limit=limit) + + +@read_newlines.register +def _( + filename: io.TextIOWrapper, + limit: Union[int, None] = 1024, # noqa: UP007 # singledispatch uses the annotation at runtime (python 3.9) +) -> NewlineSpec: + filename.read(limit) + return filename.newlines def lines_from(input: Traversable) -> Generator[str]: diff --git a/jaraco/text/show-newlines.py b/jaraco/text/show-newlines.py index e3d1c67..2a91f7b 100644 --- a/jaraco/text/show-newlines.py +++ b/jaraco/text/show-newlines.py @@ -1,18 +1,13 @@ from __future__ import annotations -from typing import TYPE_CHECKING - -import autocommand import inflect +import typer from more_itertools import always_iterable import jaraco.text -if TYPE_CHECKING: - from _typeshed import FileDescriptorOrPath - -def report_newlines(filename: FileDescriptorOrPath) -> None: +def report_newlines(input: typer.FileText) -> None: r""" Report the newlines in the indicated file. @@ -26,7 +21,7 @@ def report_newlines(filename: FileDescriptorOrPath) -> None: >>> report_newlines(filename) newlines are ('\n', '\r\n') """ - newlines = jaraco.text.read_newlines(filename) + newlines = jaraco.text.read_newlines(input) count = len(tuple(always_iterable(newlines))) engine = inflect.engine() print( @@ -37,4 +32,4 @@ def report_newlines(filename: FileDescriptorOrPath) -> None: ) -autocommand.autocommand(__name__)(report_newlines) +__name__ == '__main__' and typer.run(report_newlines) # type: ignore[func-returns-value] diff --git a/jaraco/text/strip-prefix.py b/jaraco/text/strip-prefix.py index 3ca85a9..e43cdd1 100644 --- a/jaraco/text/strip-prefix.py +++ b/jaraco/text/strip-prefix.py @@ -1,6 +1,6 @@ import sys -import autocommand +import typer from jaraco.text import Stripper @@ -18,4 +18,4 @@ def strip_prefix() -> None: sys.stdout.writelines(Stripper.strip_prefix(sys.stdin).lines) -autocommand.autocommand(__name__)(strip_prefix) +__name__ == '__main__' and typer.run(strip_prefix) # type: ignore[func-returns-value] diff --git a/newsfragments/25.feature.rst b/newsfragments/25.feature.rst new file mode 100644 index 0000000..65c51a8 --- /dev/null +++ b/newsfragments/25.feature.rst @@ -0,0 +1 @@ +Replaced ``autocommand`` usage with ``Typer`` -- by :user:`Avasam` diff --git a/pyproject.toml b/pyproject.toml index bb42d09..b4e0a95 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,10 +23,10 @@ classifiers = [ requires-python = ">=3.9" license = "MIT" dependencies = [ - "jaraco.functools", "jaraco.context >= 4.1", - "autocommand", + "jaraco.functools", "more_itertools", + "typer-slim", ] dynamic = ["version"]