The main program without error handling.
(args: argparse.Namespace, env: Environment)
| 168 | |
| 169 | |
| 170 | def program(args: argparse.Namespace, env: Environment) -> ExitStatus: |
| 171 | """ |
| 172 | The main program without error handling. |
| 173 | |
| 174 | """ |
| 175 | # TODO: Refactor and drastically simplify, especially so that the separator logic is elsewhere. |
| 176 | exit_status = ExitStatus.SUCCESS |
| 177 | downloader = None |
| 178 | initial_request: Optional[requests.PreparedRequest] = None |
| 179 | final_response: Optional[requests.Response] = None |
| 180 | processing_options = ProcessingOptions.from_raw_args(args) |
| 181 | |
| 182 | def separate(): |
| 183 | getattr(env.stdout, 'buffer', env.stdout).write(MESSAGE_SEPARATOR_BYTES) |
| 184 | |
| 185 | def request_body_read_callback(chunk: bytes): |
| 186 | should_pipe_to_stdout = bool( |
| 187 | # Request body output desired |
| 188 | OUT_REQ_BODY in args.output_options |
| 189 | # & not `.read()` already pre-request (e.g., for compression) |
| 190 | and initial_request |
| 191 | # & non-EOF chunk |
| 192 | and chunk |
| 193 | ) |
| 194 | if should_pipe_to_stdout: |
| 195 | return write_raw_data( |
| 196 | env, |
| 197 | chunk, |
| 198 | processing_options=processing_options, |
| 199 | headers=initial_request.headers |
| 200 | ) |
| 201 | |
| 202 | try: |
| 203 | if args.download: |
| 204 | args.follow = True # --download implies --follow. |
| 205 | downloader = Downloader(env, output_file=args.output_file, resume=args.download_resume) |
| 206 | downloader.pre_request(args.headers) |
| 207 | messages = collect_messages(env, args=args, |
| 208 | request_body_read_callback=request_body_read_callback) |
| 209 | force_separator = False |
| 210 | prev_with_body = False |
| 211 | |
| 212 | # Process messages as they’re generated |
| 213 | for message in messages: |
| 214 | output_options = OutputOptions.from_message(message, args.output_options) |
| 215 | |
| 216 | do_write_body = output_options.body |
| 217 | if prev_with_body and output_options.any() and (force_separator or not env.stdout_isatty): |
| 218 | # Separate after a previous message with body, if needed. See test_tokens.py. |
| 219 | separate() |
| 220 | force_separator = False |
| 221 | if output_options.kind is RequestsMessageKind.REQUEST: |
| 222 | if not initial_request: |
| 223 | initial_request = message |
| 224 | if output_options.body: |
| 225 | is_streamed_upload = not isinstance(message.body, (str, bytes)) |
| 226 | do_write_body = not is_streamed_upload |
| 227 | force_separator = is_streamed_upload and env.stdout_isatty |
nothing calls this directly
no test coverage detected