diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-09-17 20:09:40 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-09-17 20:09:40 -0400 |
commit | ed3f9094c013cce618571a8718c76f5e0dbaba3d (patch) | |
tree | bfb7bbce00bad232a643c550f95b01831f7c0dc0 /cmd2/utils.py | |
parent | cf9516e5d25df5e4e330f75ab0f800ba8b4217c8 (diff) | |
parent | 92b8a38d66d255027a0440c45582d319f2694aab (diff) | |
download | cmd2-git-ed3f9094c013cce618571a8718c76f5e0dbaba3d.tar.gz |
Merge branch 'master' into silence
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r-- | cmd2/utils.py | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index a2b1c854..b6dadf1c 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -11,9 +11,10 @@ import re import subprocess import sys import threading + import unicodedata from enum import Enum -from typing import Any, Callable, Dict, Iterable, List, Optional, TextIO, Union +from typing import Any, Callable, Dict, IO, Iterable, List, Optional, TextIO, Type, Union from . import constants @@ -470,10 +471,15 @@ class StdSim: """Get the internal contents as bytes""" return bytes(self.buffer.byte_buf) - def read(self) -> str: + def read(self, size: Optional[int] = -1) -> str: """Read from the internal contents as a str and then clear them out""" - result = self.getvalue() - self.clear() + if size is None or size == -1: + result = self.getvalue() + self.clear() + else: + result = self.buffer.byte_buf[:size].decode(encoding=self.encoding, errors=self.errors) + self.buffer.byte_buf = self.buffer.byte_buf[size:] + return result def readbytes(self) -> bytes: @@ -668,7 +674,7 @@ class ContextFlag: class RedirectionSavedState: """Created by each command to store information required to restore state after redirection""" - def __init__(self, self_stdout: Union[StdSim, TextIO], sys_stdout: Union[StdSim, TextIO], + def __init__(self, self_stdout: Union[StdSim, IO[str]], sys_stdout: Union[StdSim, IO[str]], pipe_proc_reader: Optional[ProcReader], saved_redirecting: bool) -> None: """ RedirectionSavedState initializer @@ -1025,11 +1031,12 @@ def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None :Example: + >>> import cmd2 >>> class MyApp(cmd2.Cmd): >>> def do_echo(self, arglist): >>> self.poutput(' '.join(arglist) >>> - >>> utils.categorize(do_echo, "Text Processing") + >>> cmd2.utils.categorize(do_echo, "Text Processing") For an alternative approach to categorizing commands using a decorator, see :func:`~cmd2.decorators.with_category` @@ -1038,10 +1045,13 @@ def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None for item in func: setattr(item, constants.CMD_ATTR_HELP_CATEGORY, category) else: - setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category) + if inspect.ismethod(func): + setattr(func.__func__, constants.CMD_ATTR_HELP_CATEGORY, category) + else: + setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category) -def get_defining_class(meth): +def get_defining_class(meth) -> Type: """ Attempts to resolve the class that defined a method. |