11from __future__ import print_function
22import base64
33import gzip
4- import re
54import os
65import io
76from io import BytesIO
109from xml .dom .minidom import parseString
1110
1211
13- def make_file_href_link (file_path ):
14- return "<a href='file://" + file_path .replace ('\\ ' , '/' ) + "'>" + file_path + '</a>'
15-
16-
1712class CustomData :
1813 def __init__ (self , name , value ):
1914 self .name = name
@@ -48,7 +43,7 @@ def add_comment(self, name, comment):
4843 comment = AnnotationComment (name , comment )
4944 self .comments .append (comment )
5045
51- def add_file_annotation (self , file_path = None , mime_type = 'text/plain' ):
46+ def set_file_annotation (self , file_path = None , mime_type = 'text/plain' , string_buffer = None ):
5247 self .file_path = file_path
5348 self .mimeType = mime_type
5449 if file_path is not None :
@@ -63,15 +58,23 @@ def add_file_annotation(self, file_path=None, mime_type='text/plain'):
6358 f .writelines (inFile )
6459 f .close ()
6560 self .gzip_data = out .getvalue ()
66-
67- def add_link_annotation (self , file_path = None ):
61+ elif string_buffer is not None :
62+ byte_string_buffer = string_buffer .encode ()
63+ out = BytesIO ()
64+ with gzip .GzipFile (fileobj = out , mode = "wb" ) as f :
65+ f .write (byte_string_buffer )
66+ f .close ()
67+ self .gzip_data = out .getvalue ()
68+
69+ def set_link_annotation (self , path = None ):
6870 self .link_file = True
69- match_path = re .search (r'\\\w' , file_path )
70- if match_path and file_path :
71- self .file_path = "file://" + file_path .replace ('\\ ' , '/' )
71+ if path .startswith (r'\\' ):
72+ self .file_path = "file://" + path .replace ('\\ ' , '/' )
73+ elif path .startswith (r'https' ) or path .startswith (r'http://' ):
74+ self .file_path = path
7275 else :
7376 self .level = 'error'
74- self .description = 'Invalid network file path given:' + file_path
77+ self .description = 'Invalid path given:' + path
7578
7679 def write_xml (self , parent_element , dom ):
7780 annotation = dom .createElement ("annotation" )
@@ -84,10 +87,11 @@ def write_xml(self, parent_element, dom):
8487 annotation .setAttribute ("link_file" , "true" )
8588 annotation .setAttribute ("file" , self .file_path )
8689 else :
87- annotation .setAttribute ("link_file" , "false" )
8890 annotation .setAttribute ("file" , self .file_path )
89- annotation .setAttribute ("mime_type" , self .mimeType )
90- if self .gzip_data is not None :
91+
92+ if self .gzip_data is not None :
93+ annotation .setAttribute ("link_file" , "false" )
94+ annotation .setAttribute ("mime_type" , self .mimeType )
9195 b64_data = base64 .b64encode (self .gzip_data )
9296 b64_data_string = b64_data .decode ()
9397 cdata = dom .createCDATASection (b64_data_string )
@@ -143,21 +147,23 @@ def add_error_annotation(self, message):
143147 ta = self .add_text_annotation ('Error' , 'error' )
144148 ta .description = message
145149
146- def add_custom_data (self , name , value ):
147- d = CustomData (name , value )
148- self .custom_data .append (d )
149- return d
150-
151150 def add_file_annotation (self , name , level = 'info' , description = '' ,
152151 file_path = None , mime_type = 'text/plain' ):
153152 fa = Annotation (name , level , description )
154- fa .add_file_annotation (file_path , mime_type )
153+ fa .set_file_annotation (file_path , mime_type )
154+ self .annotations .append (fa )
155+ return fa
156+
157+ def add_string_buffer_annotation (self , name , level = 'info' , description = '' ,
158+ string_buffer = None , mime_type = 'text/plain' ):
159+ fa = Annotation (name , level , description )
160+ fa .set_file_annotation (string_buffer = string_buffer , mime_type = 'text/plain' )
155161 self .annotations .append (fa )
156162 return fa
157163
158- def add_link_annotation (self , level = 'info' , description = '' , file_path = None ):
159- fa = Annotation (file_path , level , description )
160- fa .add_link_annotation ( file_path )
164+ def add_link_annotation (self , level = 'info' , description = '' , path = None ):
165+ fa = Annotation (path , level , description )
166+ fa .set_link_annotation ( path )
161167 self .annotations .append (fa )
162168 return fa
163169
@@ -166,6 +172,11 @@ def add_text_annotation(self, name, level='info', description=''):
166172 self .annotations .append (ta )
167173 return ta
168174
175+ def add_custom_metric (self , name , value ):
176+ d = CustomData (name , value )
177+ self .custom_data .append (d )
178+ return d
179+
169180 def add_annotation (self , annotation ):
170181 self .annotations .append (annotation )
171182
@@ -200,21 +211,28 @@ def add_test_suite(self, name):
200211 self .sub_suites [str (name )] = new_suite
201212 return new_suite
202213
203- def add_custom_data (self , name , value ):
214+ def add_custom_metric (self , name , value ):
204215 d = CustomData (name , value )
205216 self .custom_data .append (d )
206217 return d
207218
208219 def add_file_annotation (self , name , level = 'info' , description = '' ,
209220 file_path = None , mime_type = 'text/plain' ):
210221 fa = Annotation (name , level , description )
211- fa .add_file_annotation (file_path , mime_type )
222+ fa .set_file_annotation (file_path , mime_type )
223+ self .annotations .append (fa )
224+ return fa
225+
226+ def add_string_buffer_annotation (self , name , level = 'info' , description = '' ,
227+ string_buffer = None , mime_type = 'text/plain' ):
228+ fa = Annotation (name , level , description )
229+ fa .set_file_annotation (string_buffer = string_buffer , mime_type = 'text/plain' )
212230 self .annotations .append (fa )
213231 return fa
214232
215- def add_link_annotation (self , level = 'info' , description = '' , file_path = None ):
216- fa = Annotation (file_path , level , description )
217- fa .add_link_annotation ( file_path )
233+ def add_link_annotation (self , level = 'info' , description = '' , path = None ):
234+ fa = Annotation (path , level , description )
235+ fa .set_link_annotation ( path )
218236 self .annotations .append (fa )
219237 return fa
220238
0 commit comments