-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
121 lines (105 loc) · 3.36 KB
/
app.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
#!/usr/bin/env python
import os
import sys
import readline
import json
from django.utils.termcolors import colorize
from levels import levels
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "books.settings")
def dump_gameinfo(**kwargs):
"""
1. Assume that this file always exists and has valid json
2. Update the loaded json with kwargs and rewrite the file
"""
with open('.gameinfo.json', 'r') as f:
game_info = json.loads(f.read())
if 'username' not in game_info:
game_info['username'] = raw_input("Please enter your name: ")
with open('.gameinfo.json', 'w') as f:
game_info.update(kwargs)
f.write(json.dumps(game_info))
def print_models():
print colorize("Here is the models.py.\n-----------\n", fg="green")
print open("authors/models.py", "r").read()
print colorize("--------\n", fg="green")
def print_help():
print colorize("""
q: quits the game
h: print this help
m: prints the models.py
""", fg="green")
def game_play():
"Imports all models. Runs the game"
from authors.models import Book, Author, Publisher
print_help()
game_info = {}
try:
with open('.gameinfo.json', 'r') as f:
game_info = json.loads(f.read())
level = game_info['level']
username = game_info['username']
except IOError:
with open('.gameinfo.json', 'w') as f:
f.write('{}')
level = 1
dump_gameinfo(level=1)
level_changed = True
while True:
try:
level_dict = levels[level]
if "setup" in level_dict:
globals().update(level_dict["setup"]())
except IndexError:
print "Great! You are a queryset Champion!"
if raw_input("Do you want to play again? [yn] ") == 'y':
level = 1
dump_gameinfo(level=1)
continue
else:
break
if level_changed:
print colorize(level_dict.get('greet'), fg="blue")
print colorize(level_dict.get('question'), fg="green")
inp = raw_input(">>> ")
if inp == 'q':
break
elif inp in ['h', 'help']:
print_help()
continue
elif inp in ['m', 'models']:
print_models()
continue
elif inp.startswith("jump"):
level_changed = True
level = int(inp.split(" ")[-1])
dump_gameinfo(level=level)
continue
elif inp == "debug":
print locals()
print globals()
import pdb
pdb.set_trace()
continue
try:
print inp
qs = eval(inp)
except Exception, e:
print e
else:
try:
passed = level_dict['test'](qs)
except Exception, e:
passed = False
if passed:
level_changed = True
print colorize(level_dict['goodbye'], fg="red")
level += 1
dump_gameinfo(level=level)
else:
level_changed = False
print colorize("Try again", fg="blue")
if __name__ == "__main__":
readline.parse_and_bind('tab: complete')
readline.parse_and_bind('set editing-mode emacs')
game_play()