11import itertools
22import sys
33from io import BufferedReader
4- from typing import BinaryIO , Generator
4+ from typing import Generator , Iterable
55
66from .. import core
77
88
9- def squeeze_blank_lines (io : BinaryIO ) -> Generator [bytes ]:
9+ def squeeze_blank_lines (stream : Iterable [ bytes ] ) -> Generator [bytes ]:
1010 was_blank = False
1111
12- for line in io :
12+ for line in stream :
1313 is_blank = len (line ) < 2
1414
1515 if was_blank and is_blank :
@@ -19,14 +19,14 @@ def squeeze_blank_lines(io: BinaryIO) -> Generator[bytes]:
1919 was_blank = is_blank
2020
2121
22- def number_lines (io : BinaryIO ) -> Generator [bytes ]:
23- for i , _ in enumerate (io ):
22+ def number_lines (stream : Iterable [ bytes ] ) -> Generator [bytes ]:
23+ for i , _ in enumerate (stream ):
2424 yield f"{ i + 1 :>6} " .encode ()
2525
2626
27- def number_nonblank_lines (io : BinaryIO ) -> Generator [bytes ]:
27+ def number_nonblank_lines (stream : Iterable [ bytes ] ) -> Generator [bytes ]:
2828 i = 1
29- for line in io :
29+ for line in stream :
3030 if len (line ) > 1 :
3131 yield f"{ i :>6} " .encode ()
3232 i += 1
@@ -62,16 +62,16 @@ def format_chars(
6262 yield n .to_bytes ()
6363
6464
65- def format_lines (io : BinaryIO , * args ) -> Generator [bytes ]:
66- for line in io :
65+ def format_lines (stream : Iterable [ bytes ] , * args ) -> Generator [bytes ]:
66+ for line in stream :
6767 yield b"" .join (format_chars (line , * args ))
6868
6969
70- def cat_io (opts , io : BinaryIO ) -> None :
70+ def cat_io (opts , stream : Iterable [ bytes ] ) -> None :
7171 if opts .squeeze_blank :
72- io = squeeze_blank_lines (io )
72+ stream = squeeze_blank_lines (stream )
7373
74- io1 , io2 = itertools .tee (io , 2 )
74+ io1 , io2 = itertools .tee (stream , 2 )
7575 gen1 , gen2 = None , None
7676
7777 if opts .number_nonblank :
@@ -95,7 +95,7 @@ def cat_io(opts, io: BinaryIO) -> None:
9595
9696
9797parser = core .ExtendedOptionParser (
98- usage = ( "%prog [OPTION]... [FILE]..." ,) ,
98+ usage = "%prog [OPTION]... [FILE]..." ,
9999 description = "Concatenate each FILE to standard output." ,
100100)
101101
@@ -134,7 +134,7 @@ def cat_io(opts, io: BinaryIO) -> None:
134134
135135
136136@core .command (parser )
137- def python_userland_cat (opts , args ):
137+ def python_userland_cat (opts , args : list [ str ] ):
138138 if opts .show_all :
139139 opts .show_ends = True
140140 opts .show_tabs = True
@@ -146,26 +146,26 @@ def python_userland_cat(opts, args):
146146 opts .show_tabs = True
147147 opts .show_nonprinting = True
148148
149- generators : list [Generator [bytes ]] = []
149+ streams : list [Iterable [bytes ]] = []
150150 failed = False
151151
152152 for name in args or ["-" ]:
153153 if name == "-" :
154- generators .append (core .readlines_stdin_raw ())
154+ streams .append (core .readlines_stdin_raw ())
155155 else :
156156 try :
157- generators .append (open (name , "rb" ))
157+ streams .append (open (name , "rb" ))
158158 except OSError as e :
159159 failed = True
160160 core .perror (e )
161161
162162 try :
163- cat_io (opts , itertools .chain (* generators ))
163+ cat_io (opts , itertools .chain (* streams ))
164164 except KeyboardInterrupt :
165165 print ()
166166 return 130
167167 finally :
168- for gen in generators :
168+ for gen in streams :
169169 # Close opened files other than stdin.
170170 if isinstance (gen , BufferedReader ):
171171 gen .close ()
0 commit comments