1+ from __future__ import annotations
2+
13import argparse
24import contextlib
35import pathlib
46import sys
5- import time
67from typing import BinaryIO
78
89import progressbar
@@ -57,13 +58,6 @@ def size_to_bytes(size_str: str) -> int:
5758def create_argument_parser () -> argparse .ArgumentParser :
5859 '''
5960 Create the argument parser for the `progressbar` command.
60-
61- >>> parser = create_argument_parser()
62- >>> parser.parse_args(['-p', '-t', '-e', '-r', '-a', '-b', '-8', '-T', '-A', '-F', '-n', '-q', 'input', '-o', 'output'])
63- Namespace(average_rate=True, bytes=True, eta=True, fineta=False, format=None, height=None, input=['input'], interval=None, last_written=None, line_mode=False, name=None, numeric=True, output='output', progress=True, quiet=True, rate=True, rate_limit=None, remote=None, size=None, stop_at_size=False, sync=False, timer=True, wait=False, watchfd=None, width=None)
64-
65- Returns:
66- argparse.ArgumentParser: The argument parser for the `progressbar` command.
6761 '''
6862
6963 parser = argparse .ArgumentParser (
@@ -72,87 +66,191 @@ def create_argument_parser() -> argparse.ArgumentParser:
7266
7367 Note that this is a Python implementation of the original `pv` command
7468 that is functional but not yet feature complete.
75- ''' )
69+ '''
70+ )
7671
7772 # Display switches
78- parser .add_argument ('-p' , '--progress' , action = 'store_true' ,
79- help = 'Turn the progress bar on.' )
80- parser .add_argument ('-t' , '--timer' , action = 'store_true' ,
81- help = 'Turn the timer on.' )
82- parser .add_argument ('-e' , '--eta' , action = 'store_true' ,
83- help = 'Turn the ETA timer on.' )
84- parser .add_argument ('-I' , '--fineta' , action = 'store_true' ,
85- help = 'Display the ETA as local time of arrival.' )
86- parser .add_argument ('-r' , '--rate' , action = 'store_true' ,
87- help = 'Turn the rate counter on.' )
88- parser .add_argument ('-a' , '--average-rate' , action = 'store_true' ,
89- help = 'Turn the average rate counter on.' )
90- parser .add_argument ('-b' , '--bytes' , action = 'store_true' ,
91- help = 'Turn the total byte counter on.' )
92- parser .add_argument ('-8' , '--bits' , action = 'store_true' ,
93- help = 'Display total bits instead of bytes.' )
94- parser .add_argument ('-T' , '--buffer-percent' , action = 'store_true' ,
95- help = 'Turn on the transfer buffer percentage display.' )
96- parser .add_argument ('-A' , '--last-written' , type = int ,
97- help = 'Show the last NUM bytes written.' )
98- parser .add_argument ('-F' , '--format' , type = str ,
99- help = 'Use the format string FORMAT for output format.' )
100- parser .add_argument ('-n' , '--numeric' , action = 'store_true' ,
101- help = 'Numeric output.' )
102- parser .add_argument ('-q' , '--quiet' , action = 'store_true' , help = 'No output.' )
73+ parser .add_argument (
74+ '-p' ,
75+ '--progress' ,
76+ action = 'store_true' ,
77+ help = 'Turn the progress bar on.' ,
78+ )
79+ parser .add_argument (
80+ '-t' , '--timer' , action = 'store_true' , help = 'Turn the timer on.'
81+ )
82+ parser .add_argument (
83+ '-e' , '--eta' , action = 'store_true' , help = 'Turn the ETA timer on.'
84+ )
85+ parser .add_argument (
86+ '-I' ,
87+ '--fineta' ,
88+ action = 'store_true' ,
89+ help = 'Display the ETA as local time of arrival.' ,
90+ )
91+ parser .add_argument (
92+ '-r' , '--rate' , action = 'store_true' , help = 'Turn the rate counter on.'
93+ )
94+ parser .add_argument (
95+ '-a' ,
96+ '--average-rate' ,
97+ action = 'store_true' ,
98+ help = 'Turn the average rate counter on.' ,
99+ )
100+ parser .add_argument (
101+ '-b' ,
102+ '--bytes' ,
103+ action = 'store_true' ,
104+ help = 'Turn the total byte counter on.' ,
105+ )
106+ parser .add_argument (
107+ '-8' ,
108+ '--bits' ,
109+ action = 'store_true' ,
110+ help = 'Display total bits instead of bytes.' ,
111+ )
112+ parser .add_argument (
113+ '-T' ,
114+ '--buffer-percent' ,
115+ action = 'store_true' ,
116+ help = 'Turn on the transfer buffer percentage display.' ,
117+ )
118+ parser .add_argument (
119+ '-A' ,
120+ '--last-written' ,
121+ type = int ,
122+ help = 'Show the last NUM bytes written.' ,
123+ )
124+ parser .add_argument (
125+ '-F' ,
126+ '--format' ,
127+ type = str ,
128+ help = 'Use the format string FORMAT for output format.' ,
129+ )
130+ parser .add_argument (
131+ '-n' , '--numeric' , action = 'store_true' , help = 'Numeric output.'
132+ )
133+ parser .add_argument (
134+ '-q' , '--quiet' , action = 'store_true' , help = 'No output.'
135+ )
103136
104137 # Output modifiers
105- parser .add_argument ('-W' , '--wait' , action = 'store_true' ,
106- help = 'Wait until the first byte has been transferred.' )
138+ parser .add_argument (
139+ '-W' ,
140+ '--wait' ,
141+ action = 'store_true' ,
142+ help = 'Wait until the first byte has been transferred.' ,
143+ )
107144 parser .add_argument ('-D' , '--delay-start' , type = float , help = 'Delay start.' )
108- parser .add_argument ('-s' , '--size' , type = str ,
109- help = 'Assume total data size is SIZE.' )
110- parser .add_argument ('-l' , '--line-mode' , action = 'store_true' ,
111- help = 'Count lines instead of bytes.' )
112- parser .add_argument ('-0' , '--null' , action = 'store_true' ,
113- help = 'Count lines terminated with a zero byte.' )
114- parser .add_argument ('-i' , '--interval' , type = float ,
115- help = 'Interval between updates.' )
116- parser .add_argument ('-m' , '--average-rate-window' , type = int ,
117- help = 'Window for average rate calculation.' )
118- parser .add_argument ('-w' , '--width' , type = int ,
119- help = 'Assume terminal is WIDTH characters wide.' )
120- parser .add_argument ('-H' , '--height' , type = int ,
121- help = 'Assume terminal is HEIGHT rows high.' )
122- parser .add_argument ('-N' , '--name' , type = str ,
123- help = 'Prefix output information with NAME.' )
124- parser .add_argument ('-f' , '--force' , action = 'store_true' ,
125- help = 'Force output.' )
126- parser .add_argument ('-c' , '--cursor' , action = 'store_true' ,
127- help = 'Use cursor positioning escape sequences.' )
145+ parser .add_argument (
146+ '-s' , '--size' , type = str , help = 'Assume total data size is SIZE.'
147+ )
148+ parser .add_argument (
149+ '-l' ,
150+ '--line-mode' ,
151+ action = 'store_true' ,
152+ help = 'Count lines instead of bytes.' ,
153+ )
154+ parser .add_argument (
155+ '-0' ,
156+ '--null' ,
157+ action = 'store_true' ,
158+ help = 'Count lines terminated with a zero byte.' ,
159+ )
160+ parser .add_argument (
161+ '-i' , '--interval' , type = float , help = 'Interval between updates.'
162+ )
163+ parser .add_argument (
164+ '-m' ,
165+ '--average-rate-window' ,
166+ type = int ,
167+ help = 'Window for average rate calculation.' ,
168+ )
169+ parser .add_argument (
170+ '-w' ,
171+ '--width' ,
172+ type = int ,
173+ help = 'Assume terminal is WIDTH characters wide.' ,
174+ )
175+ parser .add_argument (
176+ '-H' , '--height' , type = int , help = 'Assume terminal is HEIGHT rows high.'
177+ )
178+ parser .add_argument (
179+ '-N' , '--name' , type = str , help = 'Prefix output information with NAME.'
180+ )
181+ parser .add_argument (
182+ '-f' , '--force' , action = 'store_true' , help = 'Force output.'
183+ )
184+ parser .add_argument (
185+ '-c' ,
186+ '--cursor' ,
187+ action = 'store_true' ,
188+ help = 'Use cursor positioning escape sequences.' ,
189+ )
128190
129191 # Data transfer modifiers
130- parser .add_argument ('-L' , '--rate-limit' , type = str ,
131- help = 'Limit transfer to RATE bytes per second.' )
132- parser .add_argument ('-B' , '--buffer-size' , type = str ,
133- help = 'Use transfer buffer size of BYTES.' )
134- parser .add_argument ('-C' , '--no-splice' , action = 'store_true' ,
135- help = 'Never use splice.' )
136- parser .add_argument ('-E' , '--skip-errors' , action = 'store_true' ,
137- help = 'Ignore read errors.' )
138- parser .add_argument ('-Z' , '--error-skip-block' , type = str ,
139- help = 'Skip block size when ignoring errors.' )
140- parser .add_argument ('-S' , '--stop-at-size' , action = 'store_true' ,
141- help = 'Stop transferring after SIZE bytes.' )
142- parser .add_argument ('-Y' , '--sync' , action = 'store_true' ,
143- help = 'Synchronise buffer caches to disk after writes.' )
144- parser .add_argument ('-K' , '--direct-io' , action = 'store_true' ,
145- help = 'Set O_DIRECT flag on all inputs/outputs.' )
146- parser .add_argument ('-X' , '--discard' , action = 'store_true' ,
147- help = 'Discard input data instead of transferring it.' )
148- parser .add_argument ('-d' , '--watchfd' , type = str ,
149- help = 'Watch file descriptor of process.' )
150- parser .add_argument ('-R' , '--remote' , type = int ,
151- help = 'Remote control another running instance of pv.' )
192+ parser .add_argument (
193+ '-L' ,
194+ '--rate-limit' ,
195+ type = str ,
196+ help = 'Limit transfer to RATE bytes per second.' ,
197+ )
198+ parser .add_argument (
199+ '-B' ,
200+ '--buffer-size' ,
201+ type = str ,
202+ help = 'Use transfer buffer size of BYTES.' ,
203+ )
204+ parser .add_argument (
205+ '-C' , '--no-splice' , action = 'store_true' , help = 'Never use splice.'
206+ )
207+ parser .add_argument (
208+ '-E' , '--skip-errors' , action = 'store_true' , help = 'Ignore read errors.'
209+ )
210+ parser .add_argument (
211+ '-Z' ,
212+ '--error-skip-block' ,
213+ type = str ,
214+ help = 'Skip block size when ignoring errors.' ,
215+ )
216+ parser .add_argument (
217+ '-S' ,
218+ '--stop-at-size' ,
219+ action = 'store_true' ,
220+ help = 'Stop transferring after SIZE bytes.' ,
221+ )
222+ parser .add_argument (
223+ '-Y' ,
224+ '--sync' ,
225+ action = 'store_true' ,
226+ help = 'Synchronise buffer caches to disk after writes.' ,
227+ )
228+ parser .add_argument (
229+ '-K' ,
230+ '--direct-io' ,
231+ action = 'store_true' ,
232+ help = 'Set O_DIRECT flag on all inputs/outputs.' ,
233+ )
234+ parser .add_argument (
235+ '-X' ,
236+ '--discard' ,
237+ action = 'store_true' ,
238+ help = 'Discard input data instead of transferring it.' ,
239+ )
240+ parser .add_argument (
241+ '-d' , '--watchfd' , type = str , help = 'Watch file descriptor of process.'
242+ )
243+ parser .add_argument (
244+ '-R' ,
245+ '--remote' ,
246+ type = int ,
247+ help = 'Remote control another running instance of pv.' ,
248+ )
152249
153250 # General options
154- parser .add_argument ('-P' , '--pidfile' , type = pathlib .Path ,
155- help = 'Save process ID in FILE.' )
251+ parser .add_argument (
252+ '-P' , '--pidfile' , type = pathlib .Path , help = 'Save process ID in FILE.'
253+ )
156254 parser .add_argument (
157255 'input' ,
158256 help = 'Input file path. Uses stdin if not specified.' ,
@@ -163,12 +261,13 @@ def create_argument_parser() -> argparse.ArgumentParser:
163261 '-o' ,
164262 '--output' ,
165263 default = '-' ,
166- help = 'Output file path. Uses stdout if not specified.' )
264+ help = 'Output file path. Uses stdout if not specified.' ,
265+ )
167266
168267 return parser
169268
170269
171- def main (argv : list [str ] = sys . argv [ 1 :]):
270+ def main (argv : list [str ] | None = None ): # noqa: C901
172271 '''
173272 Main function for the `progressbar` command.
174273 '''
@@ -180,7 +279,8 @@ def main(argv: list[str] = sys.argv[1:]):
180279 with contextlib .ExitStack () as stack :
181280 if args .output and args .output != '-' :
182281 output_stream = stack .enter_context (
183- open (args .output , 'w' + binary_mode ))
282+ open (args .output , 'w' + binary_mode )
283+ )
184284 else :
185285 if args .line_mode :
186286 output_stream = sys .stdout
@@ -246,16 +346,18 @@ def main(argv: list[str] = sys.argv[1:]):
246346 )
247347
248348 # Data processing and updating the progress bar
249- buffer_size = size_to_bytes (
250- args .buffer_size ) if args .buffer_size else 1024
349+ buffer_size = (
350+ size_to_bytes (args .buffer_size ) if args .buffer_size else 1024
351+ )
251352 total_transferred = 0
252353
253354 bar .start ()
254355 with contextlib .suppress (KeyboardInterrupt ):
255356 for input_path in input_paths :
256357 if isinstance (input_path , pathlib .Path ):
257358 input_stream = stack .enter_context (
258- input_path .open ('r' + binary_mode ))
359+ input_path .open ('r' + binary_mode )
360+ )
259361 else :
260362 input_stream = input_path
261363
0 commit comments