@@ -115,45 +115,55 @@ class CSVWriter:
115115 def __init__ (self ):
116116 self .csv_queue = Queue ()
117117 self .csv_writer = None
118- self .reporting = CsvReporting ()
118+
119+ self .exception_in_test = False
120+ self .failed = False
121+ self .chk_cnt = 0
122+ self .csv_path = ""
123+ self .test_module = None
124+ self .start_time = None
125+ self .current_test = None
126+ self .data = fixate .config .get_config_dict ()
127+ self .data .update (fixate .config .get_plugin_data ("plg_csv" ))
128+ self .exception = None
129+
130+ self ._topics = [
131+ (self .test_start , "Test_Start" ),
132+ (self .test_comparison , "Check" ),
133+ (self .test_exception , "Test_Exception" ),
134+ (self .test_complete , "Test_Complete" ),
135+ (self .sequence_update , "Sequence_Update" ),
136+ (self .sequence_complete , "Sequence_Complete" ),
137+ (self .user_wait_start , "UI_block_start" ),
138+ (self .user_wait_end , "UI_block_end" ),
139+ (self .driver_open , "driver_open" ),
140+ ]
119141
120142 def install (self ):
121- self .csv_writer = ExcThread (
122- target = self ._csv_write , args = (self .csv_queue ,), name = "csv-writer"
123- )
143+ self .csv_writer = ExcThread (target = self ._csv_write , name = "csv-writer" )
124144 self .csv_writer .start ()
125145
146+ for callback , topic in self ._topics :
147+ pub .subscribe (callback , topic )
148+
126149 def uninstall (self ):
150+ for callback , topic in self ._topics :
151+ pub .unsubscribe (callback , topic )
152+
127153 if self .csv_writer :
128154 self .csv_queue .put (None )
129155 self .csv_writer .join ()
130156 self .csv_writer = None
131157
132- def _csv_write (self , cmd_q ):
133- while True :
134- line = cmd_q .get ()
135- if line is None :
136- break # Command send to close csv_writer
137- try :
138- os .makedirs (os .path .dirname (self .reporting .csv_path ))
139- except OSError as e :
140- pass
141- with open (self .reporting .csv_path , "a+" , newline = "" ) as f :
142- writer = csv .writer (f , quoting = csv .QUOTE_MINIMAL )
143- writer .writerow (line )
144-
158+ def ensure_alive (self ):
159+ if self .exception :
160+ raise RuntimeError (
161+ f"Exception in { self .csv_writer .name } thread"
162+ ) from self .exception
145163
146- class CsvReporting :
147- def __init__ (self ):
148- self .exception_in_test = False
149- self .failed = False
150- self .chk_cnt = 0
151- self .csv_path = ""
152- self .test_module = None
153- self .start_time = None
154- self .current_test = None
155- self .data = fixate .config .get_config_dict ()
156- self .data .update (fixate .config .get_plugin_data ("plg_csv" ))
164+ if not self .csv_writer .is_alive ():
165+ # If thread has exited without throwing an exception
166+ raise RuntimeError ("csv-writer thread not active" )
157167
158168 def sequence_update (self , status ):
159169 # Do Start Sequence Reporting
@@ -336,59 +346,26 @@ def extract_test_parameters(test_cls):
336346 keys = sorted (set (test_cls .__dict__ ) - set (comp .__dict__ ))
337347 return [(key , test_cls .__dict__ [key ]) for key in keys ]
338348
349+ def _csv_write (self ):
350+ while True :
351+ line = self .csv_queue .get ()
352+ if line is None :
353+ break # Command send to close csv_writer
354+ try :
355+ os .makedirs (os .path .dirname (self .csv_path ))
356+ except OSError as e :
357+ pass
358+ with open (self .csv_path , "a+" , newline = "" , encoding = "utf-8" ) as f :
359+ writer = csv .writer (f , quoting = csv .QUOTE_MINIMAL )
360+ try :
361+ writer .writerow (line )
362+ except Exception as e :
363+ self .exception = e
364+
339365 def _write_line_to_csv (self , line ):
340366 """
341367 :param line:
342368 single line of data with each column as an element in the list
343369 :return:
344370 """
345- global writer
346- writer .csv_queue .put (line )
347- # try:
348- # os.makedirs(self.csv_dir)
349- # except OSError:
350- # pass
351- # with open(self.csv_path, 'a+', newline='') as f:
352- # writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
353- # writer.writerow(line)
354-
355-
356- writer = None
357-
358-
359- def register_csv ():
360- """
361- :param csv_dir: Base directory for for csv file
362- :param args: Args as parsed into the command line interface
363- :return:
364- """
365- global writer
366- writer = CSVWriter ()
367- writer .install ()
368- pub .subscribe (writer .reporting .test_start , "Test_Start" )
369- pub .subscribe (writer .reporting .test_comparison , "Check" )
370- pub .subscribe (writer .reporting .test_exception , "Test_Exception" )
371- pub .subscribe (writer .reporting .test_complete , "Test_Complete" )
372- pub .subscribe (writer .reporting .sequence_update , "Sequence_Update" )
373- pub .subscribe (writer .reporting .sequence_complete , "Sequence_Complete" )
374- pub .subscribe (writer .reporting .user_wait_start , "UI_block_start" )
375- pub .subscribe (writer .reporting .user_wait_end , "UI_block_end" )
376- pub .subscribe (writer .reporting .driver_open , "driver_open" )
377-
378-
379- def unregister_csv ():
380- """
381- Note, will disable the final result eg. Unit Passed
382- :return:
383- """
384- global writer
385- if writer is not None :
386- pub .unsubscribe (writer .reporting .test_start , "Test_Start" )
387- pub .unsubscribe (writer .reporting .test_comparison , "Check" )
388- pub .unsubscribe (writer .reporting .test_exception , "Test_Exception" )
389- pub .unsubscribe (writer .reporting .test_complete , "Test_Complete" )
390- pub .unsubscribe (writer .reporting .sequence_update , "Sequence_Update" )
391- pub .unsubscribe (writer .reporting .sequence_complete , "Sequence_Complete" )
392- pub .unsubscribe (writer .reporting .user_wait_start , "UI_block_start" )
393- pub .unsubscribe (writer .reporting .user_wait_end , "UI_block_end" )
394- writer .uninstall ()
371+ self .csv_queue .put (line )
0 commit comments