99
1010from in2lambda .api .question import Question
1111
12- MINIMAL_TEMPLATE = "minimal_template_question.json"
12+ MINIMAL_QUESTION_TEMPLATE = "minimal_template_question.json"
13+ MINIMAL_SET_TEMPLATE = "minimal_template_set.json"
1314
1415
1516def converter (
16- template : dict [str , Any ], ListQuestions : list [Question ], output_dir : str
17+ question_template : dict [ str , Any ], set_template : dict [str , Any ], ListQuestions : list [Question ], output_dir : str
1718) -> None :
1819 """Turns a list of question objects into Lambda Feedback JSON.
1920
2021 Args:
21- template: The loaded JSON from the minimal template (it needs to be in sync).
22+ question_template: The loaded JSON from the minimal question template (it needs to be in sync).
23+ set_template: The loaded JSON from the minimal set template (it needs to be in sync).
2224 ListQuestions: A list of question objects.
2325 output_dir: The absolute path for where to produced the final JSON/zip files.
2426 """
@@ -33,8 +35,14 @@ def converter(
3335 output_image = os .path .join (output_question , "media" )
3436 os .makedirs (output_image , exist_ok = True )
3537
38+ # create the set file
39+ with open (f"{ output_question } /set_a_simple_example.json" , "w" ) as file :
40+ json .dump (set_template , file )
41+ shutil .make_archive (output_question , "zip" , output_question )
42+
43+
3644 for i in range (len (ListQuestions )):
37- output = deepcopy (template )
45+ output = deepcopy (question_template )
3846
3947 output ["orderNumber" ] = i # order number starts at 0
4048 # add title to the question file
@@ -53,15 +61,15 @@ def converter(
5361 ListQuestions [i ].parts [0 ].worked_solution
5462 )
5563 for j in range (1 , len (ListQuestions [i ].parts )):
56- output ["parts" ].append (deepcopy (template ["parts" ][0 ]))
64+ output ["parts" ].append (deepcopy (question_template ["parts" ][0 ]))
5765 output ["parts" ][j ]["content" ] = ListQuestions [i ].parts [j ].text
5866 output ["parts" ][j ]["orderNumber" ] = j
5967 output ["parts" ][j ]["workedSolution" ]["content" ] = (
6068 ListQuestions [i ].parts [j ].worked_solution
6169 )
6270
6371 # Output file
64- filename = "question_" + str (i + 1 )
72+ filename = "question_" + str (i ). zfill ( 3 ) + "_" + output [ 'title' ]. replace ( " " , "_" )
6573
6674 # write questions into directory
6775 with open (f"{ output_question } /{ filename } .json" , "w" ) as file :
@@ -74,8 +82,8 @@ def converter(
7482 ) # converts computer path into python path
7583 shutil .copy (image_path , output_image ) # copies image into the directory
7684
77- # output zip file in destination folder
78- shutil .make_archive (output_question , "zip" , output_question )
85+ # output zip file in destination folder
86+ shutil .make_archive (output_question , "zip" , output_question )
7987
8088
8189def main (questions : list [Question ], output_dir : str ) -> None :
@@ -88,13 +96,16 @@ def main(questions: list[Question], output_dir: str) -> None:
8896 output_dir: Where to output the final Lambda Feedback JSON/ZIP files.
8997 """
9098 # Use path so minimal template can be found regardless of where the user is running python from.
91- with open (Path (__file__ ).with_name (MINIMAL_TEMPLATE ), "r" ) as file :
92- template = json .load (file )
99+ with open (Path (__file__ ).with_name (MINIMAL_QUESTION_TEMPLATE ), "r" ) as file :
100+ question_template = json .load (file )
101+
102+ with open (Path (__file__ ).with_name (MINIMAL_SET_TEMPLATE ), "r" ) as file :
103+ set_template = json .load (file )
93104
94105 # check if directory exists in file
95106 if os .path .isdir (output_dir ):
96107 try :
97108 shutil .rmtree (output_dir )
98109 except OSError as e :
99110 print ("Error: %s : %s" % (output_dir , e .strerror ))
100- converter (template , questions , output_dir )
111+ converter (question_template , set_template , questions , output_dir )
0 commit comments