@@ -40,27 +40,18 @@ class bcolors:
4040 UNDERLINE = '\033 [4m'
4141
4242
43- def printerr (msg ):
43+ def print_error (msg ):
4444 return print (f"{ bcolors .FAIL } Error : { msg } { bcolors .ENDC } " )
4545
46+ def print_warning (msg ):
47+ return print (f"{ bcolors .WARNING } Warning : { msg } { bcolors .ENDC } " )
4648
4749class Question :
4850 def __init__ (self , title , choice , is_answer ):
4951 self .title = title
5052 self .choice = choice
5153 self .is_answer = is_answer
5254
53- def FromJSON (json_question ):
54- # Question : Titre, Choix, Bonne Reponse
55- # From JSON Question : Titre, Choix[4]:[Reponse_1,True],[Reponse_2,False],[Reponse_3,False],[Reponse_4,False]
56- is_answer = None
57- choice = []
58- for i in range (0 , len (json_question ['choix' ])):
59- choice .append (json_question ['choix' ][i ][0 ])
60- if json_question ['choix' ][i ][1 ] == True :
61- is_answer = i + 1
62- return Question (json_question ['titre' ], choice , is_answer )
63-
6455 def ask (self , index ):
6556 print (f"QUESTION { index } " )
6657 print (" " + self .title )
@@ -126,16 +117,20 @@ def __new__(cls, json_questionnaire, file_path):
126117 try :
127118 validate (json_questionnaire , cls .schema )
128119 except jsonschema .exceptions .ValidationError :
129- printerr (f"Incompatible Json schema in file { file_path } " )
120+ print_error (f"Incompatible Json schema in file { file_path } " )
130121 return None
131122 else :
132123 # 'titre' or 'questions' are required properties
133124 # all the types are already verified into the schema validator but the len=0 may also identify an error
134125 if len (json_questionnaire .get ('titre' )) <= 0 :
135- printerr (f"The title of the quizz is missing in file { file_path } " )
126+ print_error (f"The title of the quizz is missing and mandatory in file { file_path } " )
136127 return None
137128 else :
138- return super ().__new__ (cls )
129+ if len (json_questionnaire .get ('questions' )) <= 0 :
130+ print_error (f"The questions of the quizz are missing and mandatory in file { file_path } " )
131+ return None
132+ else :
133+ return super ().__new__ (cls )
139134
140135 def __init__ (self , json_questionnaire , file_path ):
141136 '''Initiate a Questionnaire after __new__ checked the required settings'''
@@ -149,14 +144,22 @@ def __init__(self, json_questionnaire, file_path):
149144 self .questions = []
150145 for idx , json_question in enumerate (json_questionnaire ['questions' ]):
151146 if len (json_question ['titre' ]) <= 0 : # This is not critical but the question won't be added
152- printerr (f"Skipped question : nothing to ask in question { idx } file { file_path } " )
147+ print_warning (f"Skipped question : nothing to ask in question { idx } file { file_path } " )
153148 else :
154- good_answers = [choix [0 ] for choix in json_question ['choix' ] if choix [1 ] == True ]
155- if len (good_answers ) != 1 : # This is not critical but the question won't be added
156- printerr (f"Skipped question : no one or more than one good answer "
157- f"in question { idx } file { file_path } " )
149+ # Print a warning message if one answer of the question is empty
150+ # Must be done before to check the boolean because the good answer may have an empty string
151+
152+ for idc , choice in enumerate (json_question ['choix' ]):
153+ if len (choice [0 ]) <= 0 :
154+ print_warning (f"Skipped answer : answer is empty in question { idx } file { file_path } " )
155+ json_question ['choix' ].pop (idc )
156+
157+ good_answer = [(choice [0 ], idc ) for idc , choice in enumerate (json_question ['choix' ]) if choice [1 ]]
158+ if len (good_answer ) != 1 : # This is not critical but the question won't be added
159+ print_warning (f"Skipped question : no one or more than one good answers in question { idx } file { file_path } " )
158160 else :
159- self .questions .append (Question .FromJSON (json_question ))
161+ answers = [choice [0 ] for choice in json_question ['choix' ]]
162+ self .questions .append (Question (json_question ['titre' ], answers , good_answer [0 ][1 ]+ 1 ))
160163
161164 def run (self ):
162165 score = 0
@@ -186,15 +189,15 @@ def load_json_argv(sysargv):
186189 json_data = None
187190
188191 if len (sysargv ) != 2 :
189- printerr (
192+ print_error (
190193 f"Error in argument, should add a single .json questionnaire. Example : { os .path .basename (sys .argv [0 ])} questionnaire_to_read.json" )
191194 return None , None
192195
193196 sysargv_path = sysargv [1 ]
194197
195198 root , extension = os .path .splitext (sysargv_path )
196199 if extension != ".json" :
197- printerr (
200+ print_error (
198201 f"Incorrect extension, add a .json questionnaire. Example : { os .path .basename (sys .argv [0 ])} questionnaire_to_read.json" )
199202 return None , sysargv_path
200203
@@ -209,40 +212,37 @@ def load_json_data_from_URL(URL_path):
209212 try :
210213 response = requests .get (URL_path )
211214 except :
212- printerr (f"Invalid URL at { URL_path } " )
215+ print_error (f"Invalid URL at { URL_path } " )
213216 return None
214217 else :
215218 if response .status_code == 404 :
216- printerr (f"File not found at URL { URL_path } " )
219+ print_error (f"File not found at URL { URL_path } " )
217220 return None
218221 else :
219222 try :
220223 json_data = json .loads (response .text ) # Decode JSON String
221224 except :
222- printerr (f"Incompatible or no data in JSON file at URL { URL_path } " )
225+ print_error (f"Incompatible or no data in JSON file at URL { URL_path } " )
223226 return None
224227 return json_data
225228
226229def load_json_data_from_file (file_path ):
227230 try : # Opening of the JSON file
228231 json_file = open (file_path , "r" )
229232 except OSError :
230- printerr (f"Invalid permissions or path to { file_path } " )
233+ print_error (f"Invalid permissions or path to { file_path } " )
231234 return None
232235 else :
233236 with json_file : # Handle the memory needed for the file
234237 try :
235238 json_data = json .load (json_file ) # Read File and Decode JSON String
236239 except json .decoder .JSONDecodeError :
237- printerr (f"Incompatible or no data in JSON file { file_path } " )
240+ print_error (f"Incompatible or no data in JSON file { file_path } " )
238241 return None
239242 return json_data
240243
241244def main ():
242245 json_data , file_path = load_json_argv (sys .argv )
243- print (">" ,json_data )
244- print (">" ,file_path )
245- print (">" ,os .path .basename (sys .argv [1 ]))
246246
247247 if json_data :
248248 questionnaire = Questionnaire (json_data , file_path )
0 commit comments