-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrandom_cow.py
More file actions
64 lines (53 loc) · 1.71 KB
/
random_cow.py
File metadata and controls
64 lines (53 loc) · 1.71 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3 -O
from random import choice
from subprocess import PIPE, Popen, run
from sys import exit as sysexit
# Paths to external executables
COWSAY: str = "/usr/games/cowsay"
FORTUNE: str = "/usr/games/fortune"
def get_cows() -> list[str]:
"""Return a list of available cowsay cow names, in random order."""
try:
output: str = run(
[COWSAY, "-l"],
capture_output=True,
check=True,
).stdout.decode("utf-8")
# The first line is a header ending with ':', cows follow after the colon.
cows: list[str] = output.split(":", 1)[1].split()
except Exception as ex:
print(f"Unable to get cow list: {ex}")
sysexit(1)
return cows
def get_fortune() -> str:
"""Return a random fortune string."""
try:
fortune: str = run(
[FORTUNE, "-a"], capture_output=True, check=True
).stdout.decode("utf-8")
return fortune
except Exception as ex:
print(f"Unable to get fortune: {ex}")
sysexit(1)
return ""
def print_fortune(fortune: str, cowlist: list[str]) -> None:
"""Print the fortune spoken by a randomly selected cow."""
try:
cow: str = choice(cowlist)
cow_pipe: Popen[str] = Popen(
[COWSAY, "-f", cow],
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
text=True,
)
cow_fortune: str
cow_fortune, _ = cow_pipe.communicate(input=fortune)
print(cow_fortune)
except Exception as ex:
print(f"Unable to print fortune: {ex}")
sysexit(1)
if __name__ == "__main__":
cowlist: list[str] = get_cows()
fortune: str = get_fortune()
print_fortune(fortune, cowlist)