diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-09-17 23:44:07 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-09-17 23:44:07 -0400 |
commit | 0f11ffa3b992e3f777b96dfe46d4274bfca0dcc8 (patch) | |
tree | 8167147ff11f5c9c7aaf1008555248c9c1cd7850 /cmd2/utils.py | |
parent | c50def1eff00f7f44adcb911d215ace65d16aa8a (diff) | |
parent | d348f09cf848a566a43b30e04aa6d3adbc4de8bc (diff) | |
download | cmd2-git-0f11ffa3b992e3f777b96dfe46d4274bfca0dcc8.tar.gz |
Merge branch 'master' into 2.0
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 b89d57bb..cd716083 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -13,8 +13,9 @@ import re import subprocess import sys import threading + import unicodedata -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 @@ -433,10 +434,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: @@ -631,7 +637,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 @@ -972,11 +978,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` @@ -985,10 +992,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. |