-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
63 lines (46 loc) · 1.32 KB
/
game.py
File metadata and controls
63 lines (46 loc) · 1.32 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
#!/usr/bin/env python
# coding: utf-8
SEP = '\n<!-- GAME -->\n'
def get_between_sep(fname, sep):
with open(fname, 'rb') as f:
contents = f.read()
parts = contents.split(sep)
assert len(parts) == 3
return parts
table = {
' ': '[<img width="20" src="src/pussy.jpg">][ohai]',
'.': '[<img width="20" src="src/dick.jpg">][onoe]',
}
def make_game_iter(iter):
for in_line in iter:
in_line = in_line.rstrip('\n')
out_line = ' '.join(table[char] for char in in_line)
yield out_line + ' '
yield '[ohai]: http://www.berkeley.edu/'
yield '[onoe]: http://www.stanford.edu/%%30%30'
def make_game(fname):
return '\n'.join(make_game_iter(open(fname, 'rb')))
def main(game_fname, doc_fname):
doc_parts = get_between_sep(doc_fname, SEP)
game = make_game(game_fname)
doc_parts[1] = game
contents = SEP.join(doc_parts)
with open(doc_fname, 'wb') as f:
f.write(contents)
return 0
##
class ProgramError(Exception):
pass
def run():
from sys import argv, stderr
try:
exit(main(*argv[1:]) or 0)
except ProgramError, exc:
print >> stderr, exc
except TypeError, exc:
if exc.message.startswith("main() takes"):
print >> stderr, exc
else:
raise
if __name__ == '__main__':
run()