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

Commit 0def61e

Browse files
committed
fix web player was not able to show script file which not in the current directory
1 parent 3a6810f commit 0def61e

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
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.9'
7+
__version__ = '0.2.0'
88

99

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

giftplayer/web_player.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import os.path as path
33
import random
44
import sys
5-
from urllib.parse import quote, unquote
5+
from urllib.parse import unquote
6+
import urllib.parse
67

78
from flask import Flask, request, redirect, url_for, abort
89

@@ -12,6 +13,10 @@
1213
from .answer_scorer import build_quiz_answer, parse_form_content, score_submission
1314

1415

16+
def quote(s):
17+
return urllib.parse.quote(s, safe='')
18+
19+
1520
CSS = """
1621
""" + DEFAULT_CSS + """
1722
<style type="text/css">
@@ -91,7 +96,8 @@ def root():
9196
if GIFT_SCRIPT_INFO.stdin_cache:
9297
return redirect("/-")
9398
elif GIFT_SCRIPT_INFO.script_file:
94-
return redirect("/" + quote(GIFT_SCRIPT_INFO.script_file))
99+
fbody = path.split(GIFT_SCRIPT_INFO.script_file)[1]
100+
return redirect("/" + quote(fbody))
95101
elif GIFT_SCRIPT_INFO.dir:
96102
scripts = [f for f in os.listdir(GIFT_SCRIPT_INFO.dir) if f.endswith('.gift')]
97103
scripts.sort()
@@ -139,14 +145,15 @@ def _quiz(giftscript_unquoted, cache=None):
139145

140146
@app.route('/<giftscript>', methods=['GET'])
141147
def quiz(giftscript):
142-
giftscript_unquoted = unquote(giftscript)
143148
if GIFT_SCRIPT_INFO.stdin_cache:
144-
if giftscript_unquoted == '-':
145-
return _quiz(giftscript_unquoted, cache=GIFT_SCRIPT_INFO.stdin_cache)
149+
if giftscript == '-':
150+
return _quiz(giftscript, cache=GIFT_SCRIPT_INFO.stdin_cache)
146151
elif GIFT_SCRIPT_INFO.script_file:
147-
if giftscript_unquoted == GIFT_SCRIPT_INFO.script_file:
148-
return _quiz(giftscript_unquoted)
152+
fbody = path.split(GIFT_SCRIPT_INFO.script_file)[1]
153+
if giftscript == quote(fbody):
154+
return _quiz(GIFT_SCRIPT_INFO.script_file)
149155
elif GIFT_SCRIPT_INFO.dir:
156+
giftscript_unquoted = unquote(giftscript)
150157
scripts = [f for f in os.listdir(GIFT_SCRIPT_INFO.dir) if f.endswith('.gift')]
151158
scripts.sort()
152159
if giftscript_unquoted in scripts:
@@ -163,15 +170,16 @@ def _answer_table(giftscript_unquoted, cache=None):
163170

164171
@app.route('/<giftscript>/submit_answer', methods=['POST'])
165172
def submit_answer(giftscript):
166-
giftscript_unquoted = unquote(giftscript)
167173
answer_table = None
168174
if GIFT_SCRIPT_INFO.stdin_cache:
169-
if giftscript_unquoted == '-':
170-
answer_table = _answer_table(giftscript_unquoted, cache=GIFT_SCRIPT_INFO.stdin_cache)
175+
if giftscript == '-':
176+
answer_table = _answer_table(giftscript, cache=GIFT_SCRIPT_INFO.stdin_cache)
171177
elif GIFT_SCRIPT_INFO.script_file:
172-
if giftscript_unquoted == GIFT_SCRIPT_INFO.script_file:
173-
answer_table = _answer_table(giftscript_unquoted)
178+
fbody = path.split(GIFT_SCRIPT_INFO.script_file)[1]
179+
if giftscript == quote(fbody):
180+
answer_table = _answer_table(GIFT_SCRIPT_INFO.script_file)
174181
elif GIFT_SCRIPT_INFO.dir:
182+
giftscript_unquoted = unquote(giftscript)
175183
scripts = [f for f in os.listdir(GIFT_SCRIPT_INFO.dir) if f.endswith('.gift')]
176184
scripts.sort()
177185
if giftscript_unquoted in scripts:

0 commit comments

Comments
 (0)