1
1
import itertools
2
2
import sys
3
3
from io import BufferedReader
4
- from typing import BinaryIO , Generator
4
+ from typing import Generator , Iterable
5
5
6
6
from .. import core
7
7
8
8
9
- def squeeze_blank_lines (io : BinaryIO ) -> Generator [bytes ]:
9
+ def squeeze_blank_lines (stream : Iterable [ bytes ] ) -> Generator [bytes ]:
10
10
was_blank = False
11
11
12
- for line in io :
12
+ for line in stream :
13
13
is_blank = len (line ) < 2
14
14
15
15
if was_blank and is_blank :
@@ -19,14 +19,14 @@ def squeeze_blank_lines(io: BinaryIO) -> Generator[bytes]:
19
19
was_blank = is_blank
20
20
21
21
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 ):
24
24
yield f"{ i + 1 :>6} " .encode ()
25
25
26
26
27
- def number_nonblank_lines (io : BinaryIO ) -> Generator [bytes ]:
27
+ def number_nonblank_lines (stream : Iterable [ bytes ] ) -> Generator [bytes ]:
28
28
i = 1
29
- for line in io :
29
+ for line in stream :
30
30
if len (line ) > 1 :
31
31
yield f"{ i :>6} " .encode ()
32
32
i += 1
@@ -62,16 +62,16 @@ def format_chars(
62
62
yield n .to_bytes ()
63
63
64
64
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 :
67
67
yield b"" .join (format_chars (line , * args ))
68
68
69
69
70
- def cat_io (opts , io : BinaryIO ) -> None :
70
+ def cat_io (opts , stream : Iterable [ bytes ] ) -> None :
71
71
if opts .squeeze_blank :
72
- io = squeeze_blank_lines (io )
72
+ stream = squeeze_blank_lines (stream )
73
73
74
- io1 , io2 = itertools .tee (io , 2 )
74
+ io1 , io2 = itertools .tee (stream , 2 )
75
75
gen1 , gen2 = None , None
76
76
77
77
if opts .number_nonblank :
@@ -95,7 +95,7 @@ def cat_io(opts, io: BinaryIO) -> None:
95
95
96
96
97
97
parser = core .ExtendedOptionParser (
98
- usage = ( "%prog [OPTION]... [FILE]..." ,) ,
98
+ usage = "%prog [OPTION]... [FILE]..." ,
99
99
description = "Concatenate each FILE to standard output." ,
100
100
)
101
101
@@ -134,7 +134,7 @@ def cat_io(opts, io: BinaryIO) -> None:
134
134
135
135
136
136
@core .command (parser )
137
- def python_userland_cat (opts , args ):
137
+ def python_userland_cat (opts , args : list [ str ] ):
138
138
if opts .show_all :
139
139
opts .show_ends = True
140
140
opts .show_tabs = True
@@ -146,26 +146,26 @@ def python_userland_cat(opts, args):
146
146
opts .show_tabs = True
147
147
opts .show_nonprinting = True
148
148
149
- generators : list [Generator [bytes ]] = []
149
+ streams : list [Iterable [bytes ]] = []
150
150
failed = False
151
151
152
152
for name in args or ["-" ]:
153
153
if name == "-" :
154
- generators .append (core .readlines_stdin_raw ())
154
+ streams .append (core .readlines_stdin_raw ())
155
155
else :
156
156
try :
157
- generators .append (open (name , "rb" ))
157
+ streams .append (open (name , "rb" ))
158
158
except OSError as e :
159
159
failed = True
160
160
core .perror (e )
161
161
162
162
try :
163
- cat_io (opts , itertools .chain (* generators ))
163
+ cat_io (opts , itertools .chain (* streams ))
164
164
except KeyboardInterrupt :
165
165
print ()
166
166
return 130
167
167
finally :
168
- for gen in generators :
168
+ for gen in streams :
169
169
# Close opened files other than stdin.
170
170
if isinstance (gen , BufferedReader ):
171
171
gen .close ()
0 commit comments