|
1 | 1 | from __future__ import print_function |
2 | 2 | import base64 |
3 | 3 | import gzip |
| 4 | +import re |
4 | 5 | import os |
5 | 6 | import io |
6 | 7 | from io import BytesIO |
@@ -33,44 +34,62 @@ def __init__(self, name, comment): |
33 | 34 |
|
34 | 35 |
|
35 | 36 | class Annotation: |
36 | | - def __init__(self, name='unknown', level='info', description='', |
37 | | - file_path=None, mime_type='text/plain'): |
| 37 | + def __init__(self, name='unknown', level='info', description=''): |
38 | 38 | self.name = name |
39 | 39 | self.level = level |
40 | 40 | self.description = description |
41 | | - self.mimeType = mime_type |
42 | | - self.file_path = file_path |
| 41 | + self.mimeType = None |
| 42 | + self.file_path = None |
| 43 | + self.link_file = False |
| 44 | + self.gzip_data = None |
43 | 45 | self.comments = [] |
44 | 46 |
|
45 | 47 | def add_comment(self, name, comment): |
46 | 48 | comment = AnnotationComment(name, comment) |
47 | 49 | self.comments.append(comment) |
48 | 50 |
|
| 51 | + def add_file_annotation(self, file_path=None, mime_type='text/plain'): |
| 52 | + self.file_path = file_path |
| 53 | + self.mimeType = mime_type |
| 54 | + if file_path is not None: |
| 55 | + if not os.path.isfile(self.file_path): |
| 56 | + ta = Annotation(self.file_path, level='error') |
| 57 | + ta.description = 'File: ' + self.file_path + ' not found.' |
| 58 | + self.comments.append(ta) |
| 59 | + else: |
| 60 | + with io.open(self.file_path, 'rb') as inFile: |
| 61 | + out = BytesIO() |
| 62 | + with gzip.GzipFile(fileobj=out, mode="wb") as f: |
| 63 | + f.writelines(inFile) |
| 64 | + f.close() |
| 65 | + self.gzip_data = out.getvalue() |
| 66 | + |
| 67 | + def add_link_annotation(self, file_path=None): |
| 68 | + 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('\\', '/') |
| 72 | + else: |
| 73 | + ta = Annotation(self.file_path, level='error') |
| 74 | + ta.description = 'Invalid network file path given:' + file_path |
| 75 | + self.comments.append(ta) |
| 76 | + |
49 | 77 | def write_xml(self, parent_element, dom): |
50 | 78 | annotation = dom.createElement("annotation") |
51 | 79 | annotation.setAttribute("description", self.description) |
52 | 80 | annotation.setAttribute("level", self.level) |
53 | 81 | annotation.setAttribute("name", self.name) |
54 | 82 |
|
55 | 83 | if self.file_path is not None: |
56 | | - if not os.path.isfile(self.file_path): |
57 | | - ta = Annotation(self.file_path, level='error') |
58 | | - ta.description = 'File: ' + self.file_path + ' not found.' |
59 | | - for com in self.comments: |
60 | | - ta.comments.append(com) |
61 | | - ta.write_xml(parent_element, dom) |
62 | | - return |
| 84 | + if self.link_file: |
| 85 | + annotation.setAttribute("link_file", "true") |
| 86 | + annotation.setAttribute("file", self.file_path) |
63 | 87 | else: |
64 | 88 | annotation.setAttribute("link_file", "false") |
65 | | - annotation.setAttribute("file", "file://" + self.file_path) |
| 89 | + annotation.setAttribute("file", self.file_path) |
66 | 90 | annotation.setAttribute("mime_type", self.mimeType) |
67 | | - with io.open(self.file_path, 'rb') as inFile: |
68 | | - out = BytesIO() |
69 | | - with gzip.GzipFile(fileobj=out, mode="wb") as f: |
70 | | - f.writelines(inFile) |
71 | | - f.close() |
72 | | - gzip_data = out.getvalue() |
73 | | - b64_data = base64.b64encode(gzip_data) |
| 91 | + if self.gzip_data is not None: |
| 92 | + b64_data = base64.b64encode(self.gzip_data) |
74 | 93 | b64_data_string = b64_data.decode() |
75 | 94 | cdata = dom.createCDATASection(b64_data_string) |
76 | 95 | annotation.appendChild(cdata) |
@@ -132,7 +151,14 @@ def add_custom_data(self, name, value): |
132 | 151 |
|
133 | 152 | def add_file_annotation(self, name, level='info', description='', |
134 | 153 | file_path=None, mime_type='text/plain'): |
135 | | - fa = Annotation(name, level, description, file_path, mime_type) |
| 154 | + fa = Annotation(name, level, description) |
| 155 | + fa.add_file_annotation(file_path, mime_type) |
| 156 | + self.annotations.append(fa) |
| 157 | + return fa |
| 158 | + |
| 159 | + def add_link_annotation(self, level='info', description='', file_path=None): |
| 160 | + fa = Annotation(file_path, level, description) |
| 161 | + fa.add_link_annotation(file_path) |
136 | 162 | self.annotations.append(fa) |
137 | 163 | return fa |
138 | 164 |
|
@@ -178,7 +204,14 @@ def add_custom_data(self, name, value): |
178 | 204 |
|
179 | 205 | def add_file_annotation(self, name, level='info', description='', |
180 | 206 | file_path=None, mime_type='text/plain'): |
181 | | - fa = Annotation(name, level, description, file_path, mime_type) |
| 207 | + fa = Annotation(name, level, description) |
| 208 | + fa.add_file_annotation(file_path, mime_type) |
| 209 | + self.annotations.append(fa) |
| 210 | + return fa |
| 211 | + |
| 212 | + def add_link_annotation(self, level='info', description='', file_path=None): |
| 213 | + fa = Annotation(file_path, level, description) |
| 214 | + fa.add_link_annotation(file_path) |
182 | 215 | self.annotations.append(fa) |
183 | 216 | return fa |
184 | 217 |
|
|
0 commit comments