Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/allure_commons/logger.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
| author | shellac |
|---|---|
| date | Mon, 22 Mar 2021 18:12:50 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4f3585e2f14b |
|---|---|
| 1 import errno | |
| 2 import io | |
| 3 import os | |
| 4 import sys | |
| 5 import json | |
| 6 import uuid | |
| 7 import shutil | |
| 8 from six import text_type | |
| 9 from attr import asdict | |
| 10 from allure_commons import hookimpl | |
| 11 | |
| 12 INDENT = 4 | |
| 13 | |
| 14 | |
| 15 class AllureFileLogger(object): | |
| 16 | |
| 17 def __init__(self, report_dir, clean=False): | |
| 18 self._report_dir = report_dir | |
| 19 | |
| 20 try: | |
| 21 os.makedirs(report_dir) | |
| 22 except OSError as e: | |
| 23 if e.errno != errno.EEXIST: | |
| 24 raise | |
| 25 elif clean: | |
| 26 for f in os.listdir(report_dir): | |
| 27 f = os.path.join(report_dir, f) | |
| 28 if os.path.isfile(f): | |
| 29 os.unlink(f) | |
| 30 | |
| 31 def _report_item(self, item): | |
| 32 indent = INDENT if os.environ.get("ALLURE_INDENT_OUTPUT") else None | |
| 33 filename = item.file_pattern.format(prefix=uuid.uuid4()) | |
| 34 data = asdict(item, filter=lambda attr, value: not (type(value) != bool and not bool(value))) | |
| 35 with io.open(os.path.join(self._report_dir, filename), 'w', encoding='utf8') as json_file: | |
| 36 if sys.version_info.major < 3: | |
| 37 json_file.write( | |
| 38 unicode(json.dumps(data, indent=indent, ensure_ascii=False, encoding='utf8'))) # noqa: F821 | |
| 39 else: | |
| 40 json.dump(data, json_file, indent=indent, ensure_ascii=False) | |
| 41 | |
| 42 @hookimpl | |
| 43 def report_result(self, result): | |
| 44 self._report_item(result) | |
| 45 | |
| 46 @hookimpl | |
| 47 def report_container(self, container): | |
| 48 self._report_item(container) | |
| 49 | |
| 50 @hookimpl | |
| 51 def report_attached_file(self, source, file_name): | |
| 52 destination = os.path.join(self._report_dir, file_name) | |
| 53 shutil.copy2(source, destination) | |
| 54 | |
| 55 @hookimpl | |
| 56 def report_attached_data(self, body, file_name): | |
| 57 destination = os.path.join(self._report_dir, file_name) | |
| 58 with open(destination, 'wb') as attached_file: | |
| 59 if isinstance(body, text_type): | |
| 60 attached_file.write(body.encode('utf-8')) | |
| 61 else: | |
| 62 attached_file.write(body) | |
| 63 | |
| 64 | |
| 65 class AllureMemoryLogger(object): | |
| 66 | |
| 67 def __init__(self): | |
| 68 self.test_cases = [] | |
| 69 self.test_containers = [] | |
| 70 self.attachments = {} | |
| 71 | |
| 72 @hookimpl | |
| 73 def report_result(self, result): | |
| 74 data = asdict(result, filter=lambda attr, value: not (type(value) != bool and not bool(value))) | |
| 75 self.test_cases.append(data) | |
| 76 | |
| 77 @hookimpl | |
| 78 def report_container(self, container): | |
| 79 data = asdict(container, filter=lambda attr, value: not (type(value) != bool and not bool(value))) | |
| 80 self.test_containers.append(data) | |
| 81 | |
| 82 @hookimpl | |
| 83 def report_attached_file(self, source, file_name): | |
| 84 pass | |
| 85 | |
| 86 @hookimpl | |
| 87 def report_attached_data(self, body, file_name): | |
| 88 self.attachments[file_name] = body |
