-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
executable file
·133 lines (105 loc) · 3.76 KB
/
example.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
import philosophy
from philosophy.exceptions import *
import sys
import time
import argparse
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def print_bold(msg):
print(bcolors.BOLD, msg, bcolors.ENDC, sep='')
def print_err(msg):
print(bcolors.FAIL, bcolors.BOLD, msg, bcolors.ENDC,
sep='', file=sys.stderr)
def print_log(msg):
print(bcolors.OKGREEN, msg, bcolors.ENDC, sep='')
def getargs():
parser = argparse.ArgumentParser(description='Play The Philosophy Game')
parser.add_argument('start', action='store', type=str,
metavar='start', nargs='?',
help='the initial Wikipedia pagename to start with')
parser.add_argument('-e', '--end', action='store', default='Philosophy',
type=str, metavar='end', dest='end',
help='Wikipedia pagename to terminate at (default: \'Philosophy\')')
parser.add_argument('-i', '--infinite', action='store_true',
help="""don't stop execution until a loop is found or
a valid link cannot be found""", dest='infinite')
parser.add_argument('-t', '--times', action='store', dest='times',
default=1, type=int, metavar='times',
help="""run the script this many times, selecting a random
page every time except the first (default: 1)
(anything less than 1 is infinity)""")
return parser.parse_args()
def process(names, args, times=1):
raised = False
start_time = time.time()
try:
link_count = -1
for s in names:
if s == args.end and not args.infinite:
print_bold(s)
else:
print(s)
link_count += 1
except ConnectionError:
print_err('Network error, please check your connection')
sys.exit(1)
except MediaWikiError as e:
print_err('Error: {}: {}'.format(
e.errors['code'],
e.errors['info']))
raised = True
except LoopException as e:
print_log('---\n{}, quitting...'.format(e))
print_log('Visited {} link(s), got a loop, taking {} seconds'.format(
link_count,
round(time.time() - start_time, 4)))
raised = True
except InvalidPageNameError as e:
print_err(e)
raised = True
except LinkNotFoundError as e:
print_err(e)
print_log('---')
print_log(('Visited {} link(s), could not find appropriate link'
+ ' in last link, taking {} seconds').format(
link_count,
round(time.time() - start_time, 4)))
raised = True
if not raised:
print_log('---')
print_log('Took {} hop(s) and {} seconds'.format(
link_count,
round(time.time() - start_time, 4)))
if times == args.times:
return
# New line for separation
print()
names = philosophy.trace(end=args.end, infinite=args.infinite)
process(names, args, times=times+1)
def main(args):
start = args.start
end = args.end
infinite = args.infinite
if start == '':
start = None
if args.end == '':
end = 'Philosophy'
names = philosophy.trace(page=start, end=end, infinite=infinite)
process(names, args)
if __name__ == '__main__':
try:
args = getargs()
if not sys.stdout.isatty():
print_bold = print_log = print_err = print
main(args)
except KeyboardInterrupt:
print_err('Script interrupted')
sys.exit(1)