1919# - lancer()
2020#
2121import json
22- import ntpath
2322import os
2423import sys
2524
@@ -102,6 +101,7 @@ class Questionnaire:
102101 "default" : [],
103102 "items" : {
104103 "type" : "object" ,
104+ "required" : ["titre" , "choix" ],
105105 "properties" : {
106106 "titre" : {"type" : "string" },
107107 "choix" : {
@@ -121,37 +121,42 @@ class Questionnaire:
121121 }
122122 }
123123
124- def __init__ (self , json_questionnaire , file_path ):
125- '''Test the expected json schema before to initiate'''
126-
124+ def __new__ (cls , json_questionnaire , file_path ):
125+ '''Test the expected json schema before to initiate a Questionnaire'''
127126 try :
128- validate (json_questionnaire , self .schema )
127+ validate (json_questionnaire , cls .schema )
129128 except jsonschema .exceptions .ValidationError :
130129 printerr (f"Incompatible Json schema in file { file_path } " )
130+ return None
131131 else :
132132 # 'titre' or 'questions' are required properties
133133 # all the types are already verified into the schema validator but the len=0 may also identify an error
134134 if len (json_questionnaire .get ('titre' )) <= 0 :
135135 printerr (f"The title of the quizz is missing in file { file_path } " )
136+ return None
137+ else :
138+ return super ().__new__ (cls )
139+
140+ def __init__ (self , json_questionnaire , file_path ):
141+ '''Initiate a Questionnaire after __new__ checked the required settings'''
142+
143+ self .titre = json_questionnaire ['titre' ]
144+
145+ # 'categorie', 'difficulte' : non-critical properties, but better to be completed as unknown if absent
146+ self .categorie = json_questionnaire ['categorie' ] if json_questionnaire .get ('categorie' ) else 'inconnue'
147+ self .difficulte = json_questionnaire ['difficulte' ] if json_questionnaire .get ('difficulte' ) else 'inconnue'
148+
149+ self .questions = []
150+ for idx , json_question in enumerate (json_questionnaire ['questions' ]):
151+ 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 } " )
136153 else :
137- self .titre = json_questionnaire ['titre' ]
138-
139- # 'categorie', 'difficulte' : non-critical properties, but better to be completed as unknown if absent
140- self .categorie = json_questionnaire ['categorie' ] if json_questionnaire .get ('categorie' ) else 'inconnue'
141- self .difficulte = json_questionnaire ['difficulte' ] if json_questionnaire .get (
142- 'difficulte' ) else 'inconnue'
143-
144- self .questions = []
145- for idx , json_question in enumerate (json_questionnaire ['questions' ]):
146- if len (json_question ['titre' ]) <= 0 : # This is not critical but the question won't be added
147- printerr (f"Skipped question : nothing to ask in question { idx } file { file_path } " )
148- else :
149- good_answers = [choix [0 ] for choix in json_question ['choix' ] if choix [1 ] == True ]
150- if len (good_answers ) != 1 : # This is not critical but the question won't be added
151- printerr (f"Skipped question : no one or more than one good answer "
152- f"in question { idx } file { file_path } " )
153- else :
154- self .questions .append (Question .FromJSON (json_question ))
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 } " )
158+ else :
159+ self .questions .append (Question .FromJSON (json_question ))
155160
156161 def run (self ):
157162 score = 0
@@ -182,15 +187,15 @@ def load_json_argv(sysargv):
182187
183188 if len (sysargv ) != 2 :
184189 printerr (
185- f"Error in argument, should add a single .json questionnaire. Example : { ntpath .basename (sys .argv [0 ])} questionnaire_to_read.json" )
190+ f"Error in argument, should add a single .json questionnaire. Example : { os . path .basename (sys .argv [0 ])} questionnaire_to_read.json" )
186191 return None , None
187192
188193 sysargv_path = sysargv [1 ]
189194
190195 root , extension = os .path .splitext (sysargv_path )
191196 if extension != ".json" :
192197 printerr (
193- f"Incorrect extension, add a .json questionnaire. Example : { ntpath .basename (sys .argv [0 ])} questionnaire_to_read.json" )
198+ f"Incorrect extension, add a .json questionnaire. Example : { os . path .basename (sys .argv [0 ])} questionnaire_to_read.json" )
194199 return None , sysargv_path
195200
196201 if validators .url (sysargv_path ): # si le fichier JSON est une URL
@@ -235,9 +240,14 @@ def load_json_data_from_file(file_path):
235240
236241def main ():
237242 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 ]))
238246
239247 if json_data :
240- Questionnaire (json_data , file_path ).run ()
248+ questionnaire = Questionnaire (json_data , file_path )
249+ if questionnaire :
250+ questionnaire .run ()
241251
242252
243253if __name__ == "__main__" :
0 commit comments