-
Notifications
You must be signed in to change notification settings - Fork 1
/
fortune.py
111 lines (85 loc) · 2.71 KB
/
fortune.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import codecs
import random
import re
import sys
from optparse import OptionParser
# ---------------------------------------------------------------------------
# Exports
# ---------------------------------------------------------------------------
__all__ = ["main", "get_random_fortune"]
# Info about the module
__version__ = "1.1.0"
__author__ = "Brian M. Clapper"
__email__ = "[email protected]"
__url__ = "http://software.clapper.org/fortune/"
__copyright__ = "2008-2019 Brian M. Clapper"
__license__ = "BSD-style license"
# ---------------------------------------------------------------------------
# Functions
# ---------------------------------------------------------------------------
def _random_int(start, end):
try:
# Use SystemRandom, if it's available, since it's likely to have
# more entropy.
r = random.SystemRandom()
except BaseException:
r = random
return r.randint(start, end)
def _read_fortunes(fortune_file):
with codecs.open(fortune_file, mode="r", encoding="utf-8") as f:
contents = f.read()
lines = [line.rstrip() for line in contents.split("\n")]
delim = re.compile(r"^%$")
fortunes = []
cur = []
def save_if_nonempty(buf):
fortune = "\n".join(buf)
if fortune.strip():
fortunes.append(fortune)
for line in lines:
if delim.match(line):
save_if_nonempty(cur)
cur = []
continue
cur.append(line)
if cur:
save_if_nonempty(cur)
return fortunes
def get_random_fortune(fortune_file):
fortunes = list(_read_fortunes(fortune_file))
randomRecord = _random_int(0, len(fortunes) - 1)
return fortunes[randomRecord]
def main():
usage = "Usage: %prog [OPTIONS] [fortune_file]"
arg_parser = OptionParser(usage=usage)
arg_parser.add_option(
"-V",
"--version",
action="store_true",
dest="show_version",
help="Show version and exit.",
)
arg_parser.epilog = (
"If fortune_file is omitted, fortune looks at the "
"FORTUNE_FILE environment variable for the path."
)
options, args = arg_parser.parse_args(sys.argv)
if len(args) == 2:
fortune_file = args[1]
else:
try:
fortune_file = "notes.txt"
except KeyError:
print("Missing fortune file.", file=sys.stderr)
print(usage, file=sys.stderr)
sys.exit(1)
try:
if options.show_version:
print("fortune, version {}".format(__version__))
else:
print(get_random_fortune(fortune_file))
except ValueError as msg:
print(msg, file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()