11
11
import subprocess
12
12
import sys
13
13
import threading
14
+
14
15
import unicodedata
15
16
from enum import Enum
16
- from typing import Any , Callable , Dict , Iterable , List , Optional , TextIO , Union
17
+ from typing import Any , Callable , Dict , IO , Iterable , List , Optional , TextIO , Type , Union
17
18
18
19
from . import constants
19
20
@@ -470,10 +471,15 @@ def getbytes(self) -> bytes:
470
471
"""Get the internal contents as bytes"""
471
472
return bytes (self .buffer .byte_buf )
472
473
473
- def read (self ) -> str :
474
+ def read (self , size : Optional [ int ] = - 1 ) -> str :
474
475
"""Read from the internal contents as a str and then clear them out"""
475
- result = self .getvalue ()
476
- self .clear ()
476
+ if size is None or size == - 1 :
477
+ result = self .getvalue ()
478
+ self .clear ()
479
+ else :
480
+ result = self .buffer .byte_buf [:size ].decode (encoding = self .encoding , errors = self .errors )
481
+ self .buffer .byte_buf = self .buffer .byte_buf [size :]
482
+
477
483
return result
478
484
479
485
def readbytes (self ) -> bytes :
@@ -668,7 +674,7 @@ def __exit__(self, *args) -> None:
668
674
669
675
class RedirectionSavedState :
670
676
"""Created by each command to store information required to restore state after redirection"""
671
- def __init__ (self , self_stdout : Union [StdSim , TextIO ] , sys_stdout : Union [StdSim , TextIO ],
677
+ def __init__ (self , self_stdout : Union [StdSim , IO [ str ]] , sys_stdout : Union [StdSim , IO [ str ] ],
672
678
pipe_proc_reader : Optional [ProcReader ], saved_redirecting : bool ) -> None :
673
679
"""
674
680
RedirectionSavedState initializer
@@ -1025,11 +1031,12 @@ def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None
1025
1031
1026
1032
:Example:
1027
1033
1034
+ >>> import cmd2
1028
1035
>>> class MyApp(cmd2.Cmd):
1029
1036
>>> def do_echo(self, arglist):
1030
1037
>>> self.poutput(' '.join(arglist)
1031
1038
>>>
1032
- >>> utils.categorize(do_echo, "Text Processing")
1039
+ >>> cmd2. utils.categorize(do_echo, "Text Processing")
1033
1040
1034
1041
For an alternative approach to categorizing commands using a decorator, see
1035
1042
:func:`~cmd2.decorators.with_category`
@@ -1044,7 +1051,7 @@ def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None
1044
1051
setattr (func , constants .CMD_ATTR_HELP_CATEGORY , category )
1045
1052
1046
1053
1047
- def get_defining_class (meth ):
1054
+ def get_defining_class (meth ) -> Type :
1048
1055
"""
1049
1056
Attempts to resolve the class that defined a method.
1050
1057
0 commit comments