File: subproc.py

package info (click to toggle)
urwid 2.6.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,236 kB
  • sloc: python: 29,290; javascript: 382; sh: 34; makefile: 22
file content (42 lines) | stat: -rwxr-xr-x 1,077 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python

from __future__ import annotations

import os
import subprocess
import sys

import urwid

factor_me = 362923067964327863989661926737477737673859044111968554257667
run_me = os.path.join(os.path.dirname(sys.argv[0]), "subproc2.py")

output_widget = urwid.Text(f"Factors of {factor_me:d}:\n")
edit_widget = urwid.Edit("Type anything or press enter to exit:")
frame_widget = urwid.Frame(
    header=edit_widget,
    body=urwid.Filler(output_widget, valign=urwid.BOTTOM),
    focus_part="header",
)


def exit_on_enter(key: str | tuple[str, int, int, int]) -> None:
    if key == "enter":
        raise urwid.ExitMainLoop()


loop = urwid.MainLoop(frame_widget, unhandled_input=exit_on_enter)


def received_output(data: bytes) -> bool:
    output_widget.set_text(output_widget.text + data.decode("utf8"))
    return True


write_fd = loop.watch_pipe(received_output)
with subprocess.Popen(  # noqa: S603
    ["python", "-u", run_me, str(factor_me)],  # noqa: S607  # Example can be insecure
    stdout=write_fd,
    close_fds=True,
) as proc:
    loop.run()