Skip to content
This repository was archived by the owner on Sep 25, 2021. It is now read-only.

Commit 3a6810f

Browse files
committed
directory contents are now re-scanned when quiz page is reloaded
1 parent e17a186 commit 3a6810f

File tree

2 files changed

+82
-40
lines changed

2 files changed

+82
-40
lines changed

giftplayer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import html
55

66

7-
__version__ = '0.1.8'
7+
__version__ = '0.1.9'
88

99

1010
__doc__ = """Render/serve given GIFT script as HTML.

giftplayer/web_player.py

Lines changed: 81 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -72,46 +72,63 @@
7272
FOOT_DIR = FOOT_ANS
7373

7474

75+
class GiftScriptInfo:
76+
def __init__(self):
77+
self.stdin_cache = None
78+
self.script_file = None
79+
self.dir = None
80+
81+
7582
# constant values, page contents (assigned before flask app instance is created)
76-
GIFT_SCRIPT_DIR = [None]
77-
GIFT_SCRIPTS = []
78-
STDIN_CACHE = [None]
83+
GIFT_SCRIPT_INFO = GiftScriptInfo()
7984
SHUFFLE_FUNC = [None]
8085

8186
app = Flask(__name__)
8287

8388

8489
@app.route('/', methods=['GET'])
8590
def root():
86-
if len(GIFT_SCRIPTS) == 1:
87-
return redirect("/" + quote(GIFT_SCRIPTS[0]))
88-
89-
buf = [HEAD_DIR]
90-
for s in GIFT_SCRIPTS:
91-
buf.append("<a href=%s>%s</a><br />" % (quote(s), s))
92-
buf.append(FOOT_DIR)
93-
return ''.join(buf)
94-
95-
96-
def _read_quiz_script(giftscript_unquoted):
97-
gift_script_path = path.join(GIFT_SCRIPT_DIR[0], giftscript_unquoted)
98-
if gift_script_path == '-':
99-
if STDIN_CACHE[0] is not None:
100-
lines = STDIN_CACHE[0]
101-
else:
102-
lines = STDIN_CACHE[0] = sys.stdin.readlines()
91+
if GIFT_SCRIPT_INFO.stdin_cache:
92+
return redirect("/-")
93+
elif GIFT_SCRIPT_INFO.script_file:
94+
return redirect("/" + quote(GIFT_SCRIPT_INFO.script_file))
95+
elif GIFT_SCRIPT_INFO.dir:
96+
scripts = [f for f in os.listdir(GIFT_SCRIPT_INFO.dir) if f.endswith('.gift')]
97+
scripts.sort()
98+
buf = [HEAD_DIR]
99+
buf.append("<b>directory: %s</b><br />" % quote(GIFT_SCRIPT_INFO.dir))
100+
buf.append("<table>")
101+
c = 0
102+
items_in_row = 5
103+
for s in scripts:
104+
if c % items_in_row == 0:
105+
buf.append("<tr>")
106+
buf.append("<td><a href=%s>%s</a></td>" % (quote(s), s))
107+
c += 1
108+
if c % items_in_row == 0:
109+
buf.append("</tr>")
110+
if c % items_in_row != 0:
111+
buf.append("</tr>")
112+
buf.append("</table>")
113+
buf.append(FOOT_DIR)
114+
return '\n'.join(buf)
103115
else:
104-
with open(gift_script_path, 'r', encoding='utf-8') as f:
105-
lines = f.readlines()
116+
assert False
117+
106118

119+
def _read_quiz_script(giftscript_unquoted, cache=None):
120+
if cache:
121+
lines = cache
122+
else:
123+
with open(giftscript_unquoted, 'r', encoding='utf-8') as f:
124+
lines = f.readlines()
107125
ast = gift_parse(lines, merge_empty_line=False)
108126
# print(ast)
109-
110127
return ast
111128

112129

113-
def _quiz(giftscript_unquoted):
114-
ast = _read_quiz_script(giftscript_unquoted)
130+
def _quiz(giftscript_unquoted, cache=None):
131+
ast = _read_quiz_script(giftscript_unquoted, cache=cache)
115132
ast = html_escape_node_body_strs(ast)
116133
answer_table = build_quiz_answer(ast)
117134
html = build_form_content(ast, shuffle_func=SHUFFLE_FUNC[0], length_hint=answer_table)
@@ -123,17 +140,44 @@ def _quiz(giftscript_unquoted):
123140
@app.route('/<giftscript>', methods=['GET'])
124141
def quiz(giftscript):
125142
giftscript_unquoted = unquote(giftscript)
126-
if giftscript_unquoted not in GIFT_SCRIPTS:
127-
abort(404)
128-
return _quiz(giftscript_unquoted)
143+
if GIFT_SCRIPT_INFO.stdin_cache:
144+
if giftscript_unquoted == '-':
145+
return _quiz(giftscript_unquoted, cache=GIFT_SCRIPT_INFO.stdin_cache)
146+
elif GIFT_SCRIPT_INFO.script_file:
147+
if giftscript_unquoted == GIFT_SCRIPT_INFO.script_file:
148+
return _quiz(giftscript_unquoted)
149+
elif GIFT_SCRIPT_INFO.dir:
150+
scripts = [f for f in os.listdir(GIFT_SCRIPT_INFO.dir) if f.endswith('.gift')]
151+
scripts.sort()
152+
if giftscript_unquoted in scripts:
153+
return _quiz(path.join(GIFT_SCRIPT_INFO.dir, giftscript_unquoted))
154+
abort(404)
155+
156+
157+
def _answer_table(giftscript_unquoted, cache=None):
158+
ast = _read_quiz_script(giftscript_unquoted, cache=cache)
159+
ast = html_escape_node_body_strs(ast)
160+
answer_table = build_quiz_answer(ast)
161+
return answer_table
129162

130163

131164
@app.route('/<giftscript>/submit_answer', methods=['POST'])
132165
def submit_answer(giftscript):
133166
giftscript_unquoted = unquote(giftscript)
134-
ast = _read_quiz_script(giftscript_unquoted)
135-
ast = html_escape_node_body_strs(ast)
136-
answer_table = build_quiz_answer(ast)
167+
answer_table = None
168+
if GIFT_SCRIPT_INFO.stdin_cache:
169+
if giftscript_unquoted == '-':
170+
answer_table = _answer_table(giftscript_unquoted, cache=GIFT_SCRIPT_INFO.stdin_cache)
171+
elif GIFT_SCRIPT_INFO.script_file:
172+
if giftscript_unquoted == GIFT_SCRIPT_INFO.script_file:
173+
answer_table = _answer_table(giftscript_unquoted)
174+
elif GIFT_SCRIPT_INFO.dir:
175+
scripts = [f for f in os.listdir(GIFT_SCRIPT_INFO.dir) if f.endswith('.gift')]
176+
scripts.sort()
177+
if giftscript_unquoted in scripts:
178+
answer_table = _answer_table(path.join(GIFT_SCRIPT_INFO.dir, giftscript_unquoted))
179+
if answer_table is None:
180+
abort(404)
137181

138182
quiz_keys = list(answer_table.keys())
139183
quiz_keys.sort()
@@ -153,16 +197,14 @@ def submit_answer(giftscript):
153197

154198
def entrypoint(gift_script, shuffle, port=5000):
155199
if gift_script == '-':
156-
GIFT_SCRIPT_DIR[0] = ''
157-
GIFT_SCRIPTS.append(gift_script)
200+
GIFT_SCRIPT_INFO.stdin_cache = sys.stdin.readlines()
158201
elif path.isfile(gift_script):
159-
GIFT_SCRIPT_DIR[0] = path.split(gift_script)[0]
160-
GIFT_SCRIPTS.append(gift_script)
202+
GIFT_SCRIPT_INFO.script_file = gift_script
161203
elif path.isdir(gift_script):
162-
GIFT_SCRIPT_DIR[0] = gift_script
163-
GIFT_SCRIPTS.extend(f for f in os.listdir(gift_script) if f.endswith('.gift'))
164-
GIFT_SCRIPTS.sort()
165-
if len(GIFT_SCRIPTS) == 0:
204+
GIFT_SCRIPT_INFO.dir = gift_script
205+
scripts = [f for f in os.listdir(gift_script) if f.endswith('.gift')]
206+
scripts.sort()
207+
if not scripts:
166208
sys.exit("error: no .gift file found in directory: %s" % repr(gift_script))
167209
else:
168210
sys.exit("error: no such file/directory found: %s" % repr(gift_script))

0 commit comments

Comments
 (0)