Skip to content

Commit 6e26f91

Browse files
author
Philip Guo
committed
committed john's updates to generate_json_trace.py script
1 parent 167e51e commit 6e26f91

1 file changed

Lines changed: 11 additions & 18 deletions

File tree

‎v3/generate_json_trace.py‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
# Generates a JSON trace that is compatible with the js/pytutor.js frontend
22

3-
import optparse
4-
5-
CUMULATIVE_MODE = False
6-
7-
COMPACT = False
8-
if COMPACT:
9-
INDENT_LEVEL=None
10-
else:
11-
INDENT_LEVEL=2
12-
13-
143
import sys, pg_logger, json
15-
4+
from optparse import OptionParser
165

176
# To make regression tests work consistently across platforms,
187
# standardize display of floats to 3 significant figures
@@ -21,27 +10,31 @@
2110
# http://stackoverflow.com/questions/1447287/format-floats-with-standard-json-module
2211
json.encoder.FLOAT_REPR = lambda f: ('%.3f' % f)
2312

24-
2513
def json_finalizer(input_code, output_trace):
2614
ret = dict(code=input_code, trace=output_trace)
2715
json_output = json.dumps(ret, indent=INDENT_LEVEL)
2816
print(json_output)
2917

30-
3118
def js_var_finalizer(input_code, output_trace):
3219
global JS_VARNAME
3320
ret = dict(code=input_code, trace=output_trace)
3421
json_output = json.dumps(ret, indent=None)
3522
print("var %s = %s;" % (JS_VARNAME, json_output))
3623

37-
38-
parser = optparse.OptionParser()
24+
parser = OptionParser(usage="Generate JSON trace for pytutor")
25+
parser.add_option('-c', '--cumulative', default=False, action='store_true',
26+
help='output cumulative trace.')
27+
parser.add_option('-o', '--compact', default=False, action='store_true',
28+
help='output compact trace.')
3929
parser.add_option("--create_jsvar", dest="js_varname",
4030
help="Create a JavaScript variable out of the trace")
4131
(options, args) = parser.parse_args()
32+
INDENT_LEVEL = None if options.compact else 2
33+
34+
fin = sys.stdin if args[0] == "-" else open(args[0])
4235

4336
if options.js_varname:
4437
JS_VARNAME = options.js_varname
45-
pg_logger.exec_script_str(open(args[0]).read(), CUMULATIVE_MODE, js_var_finalizer)
38+
pg_logger.exec_script_str(fin.read(), options.cumulative, js_var_finalizer)
4639
else:
47-
pg_logger.exec_script_str(open(args[0]).read(), CUMULATIVE_MODE, json_finalizer)
40+
pg_logger.exec_script_str(fin.read(), options.cumulative, json_finalizer)

0 commit comments

Comments
 (0)