commit: 96f632b5818d38937a57f2a9bd165055c573fc73
Author: Brian Harring <ferringb <AT> gmail <DOT> com>
AuthorDate: Sat Nov 22 13:03:19 2025 +0000
Commit: Brian Harring <ferringb <AT> gmail <DOT> com>
CommitDate: Sat Nov 22 17:11:27 2025 +0000
URL:
https://gitweb.gentoo.org/proj/pkgcore/snakeoil.git/commit/?id=96f632b5
chore: fix ABC of Formatter and add annotations.
Formatter base class requires .stream, so just force it.
I need to go in and just collapse that ontology, the annotations
are layer violations are painful AF due to the history of how
that evolved and tooling back then being insufficient to do this
sort of refactor safely. This will be addressed via #113.
Signed-off-by: Brian Harring <ferringb <AT> gmail.com>
src/snakeoil/cli/tool.py | 3 +++
src/snakeoil/formatters.py | 9 ++++++---
2 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/src/snakeoil/cli/tool.py b/src/snakeoil/cli/tool.py
index 18b2f89..3dea6d8 100644
--- a/src/snakeoil/cli/tool.py
+++ b/src/snakeoil/cli/tool.py
@@ -17,6 +17,9 @@ from .exceptions import ExitException, find_user_exception
class Tool:
"""Abstraction for commandline tools."""
+ out: formatters.Formatter
+ err: formatters.Formatter
+
def __init__(self, parser, outfile=None, errfile=None):
"""Initialize the utility to run.
diff --git a/src/snakeoil/formatters.py b/src/snakeoil/formatters.py
index ee1840f..1f8d2e8 100644
--- a/src/snakeoil/formatters.py
+++ b/src/snakeoil/formatters.py
@@ -1,5 +1,6 @@
"""Classes wrapping a file-like object to do fancy output on it."""
+import abc
import errno
import io
import locale
@@ -32,7 +33,7 @@ class StreamClosed(KeyboardInterrupt):
# pylint: disable=C0103
-class Formatter:
+class Formatter(abc.ABC):
"""Abstract formatter base class.
The types of most of the instance attributes is undefined (depends
@@ -46,10 +47,12 @@ class Formatter:
(defaults to on).
"""
- def __init__(self):
+ def __init__(self, stream: typing.IO):
self.autoline = True
self.wrap = False
+ self.stream = stream
+ @abc.abstractmethod
def write(
self,
*args: typing.Union[None, str, typing.Callable[["Formatter"], None]],
@@ -146,7 +149,7 @@ class PlainTextFormatter(Formatter):
:param width: maximum line width (defaults to 79).
:param encoding: encoding unicode strings are converted to.
"""
- super().__init__()
+ super().__init__(stream)
# We used to require a TextIOWrapper on py3. We still accept
# one, guess the encoding from it and grab its underlying
# bytestream.