Skip to content
Merged
Show file tree
Hide file tree
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
33 changes: 29 additions & 4 deletions jaraco/text/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import functools
import io
import itertools
import os
import re
import sys
import textwrap
Expand All @@ -13,6 +15,8 @@
Protocol,
SupportsIndex,
TypeVar,
Union,
cast,
overload,
)

Expand All @@ -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")

Expand Down Expand Up @@ -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'
Expand All @@ -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)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively:

Suggested change
filename = cast(Openable, filename)
assert isinstance(filename, (str, bytes, os.PathLike, int))

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]:
Expand Down
13 changes: 4 additions & 9 deletions jaraco/text/show-newlines.py
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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(
Expand All @@ -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]
4 changes: 2 additions & 2 deletions jaraco/text/strip-prefix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

import autocommand
import typer

from jaraco.text import Stripper

Expand All @@ -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]
Comment thread
Avasam marked this conversation as resolved.
1 change: 1 addition & 0 deletions newsfragments/25.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replaced ``autocommand`` usage with ``Typer`` -- by :user:`Avasam`
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down