From 872da20feba57f42dde204da01dc48c4c87e1b54 Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Thu, 10 Sep 2020 09:15:05 -0400 Subject: Changes default category to be heritable by default - meaning that subclasses will inherit the parent class's default category. Adds optional flag to disable heritability. --- cmd2/utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index a2b1c854..d396fb6a 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -1038,7 +1038,10 @@ 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): -- cgit v1.2.1 From 92b8a38d66d255027a0440c45582d319f2694aab Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Tue, 8 Sep 2020 16:01:04 -0400 Subject: Minor type hinting fixes. --- cmd2/utils.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index d396fb6a..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` @@ -1044,7 +1051,7 @@ def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None 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. -- cgit v1.2.1