| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""The artifact writer objects.""" |
| 3 |
|
| 4 |
import abc |
| 5 |
import json |
| 6 |
import yaml |
| 7 |
|
| 8 |
|
| 9 |
class BaseArtifactsWriter(object): |
| 10 |
"""Artifacts writer interface.""" |
| 11 |
|
| 12 |
# Note that redundant-returns-doc is broken for # pylint 1.7.x for |
| 13 |
# abstract methods. |
| 14 |
# pylint: disable=redundant-returns-doc |
| 15 |
|
| 16 |
@abc.abstractmethod |
| 17 |
def FormatArtifacts(self, artifacts): |
| 18 |
"""Formats artifacts to desired output format. |
| 19 |
|
| 20 |
Args: |
| 21 |
artifacts (list[ArtifactDefinition]): artifact definitions. |
| 22 |
|
| 23 |
Returns: |
| 24 |
str: formatted string of artifact definition. |
| 25 |
""" |
| 26 |
|
| 27 |
@abc.abstractmethod |
| 28 |
def WriteArtifactsFile(self, artifacts, filename): |
| 29 |
"""Writes artifact definitions to a file. |
| 30 |
|
| 31 |
Args: |
| 32 |
artifacts (list[ArtifactDefinition]): artifact definitions to be written. |
| 33 |
filename (str): name of the file to write artifacts to. |
| 34 |
""" |
| 35 |
|
| 36 |
|
| 37 |
class ArtifactWriter(BaseArtifactsWriter): |
| 38 |
"""File artifacts writer.""" |
| 39 |
|
| 40 |
# Note that redundant-returns-doc is broken for # pylint 1.7.x for |
| 41 |
# abstract methods. |
| 42 |
# pylint: disable=redundant-returns-doc |
| 43 |
|
| 44 |
@abc.abstractmethod |
| 45 |
def FormatArtifacts(self, artifacts): |
| 46 |
"""Formats artifacts to desired output format. |
| 47 |
|
| 48 |
Args: |
| 49 |
artifacts (ArtifactDefinition|list[ArtifactDefinition]): artifact |
| 50 |
definitions. |
| 51 |
|
| 52 |
Returns: |
| 53 |
str: formatted string of artifact definition. |
| 54 |
""" |
| 55 |
|
| 56 |
def WriteArtifactsFile(self, artifacts, filename): |
| 57 |
"""Writes artifact definitions to a file. |
| 58 |
|
| 59 |
Args: |
| 60 |
artifacts (list[ArtifactDefinition]): artifact definitions to be written. |
| 61 |
filename (str): name of the file to write artifacts to. |
| 62 |
""" |
| 63 |
with open(filename, 'w', encoding='utf-8') as file_object: |
| 64 |
file_object.write(self.FormatArtifacts(artifacts)) |
| 65 |
|
| 66 |
|
| 67 |
class JsonArtifactsWriter(ArtifactWriter): |
| 68 |
"""JSON artifacts writer interface.""" |
| 69 |
|
| 70 |
def FormatArtifacts(self, artifacts): |
| 71 |
"""Formats artifacts to desired output format. |
| 72 |
|
| 73 |
Args: |
| 74 |
artifacts (list[ArtifactDefinition]): artifact definitions. |
| 75 |
|
| 76 |
Returns: |
| 77 |
str: formatted string of artifact definition. |
| 78 |
""" |
| 79 |
artifact_definitions = [artifact.AsDict() for artifact in artifacts] |
| 80 |
json_data = json.dumps(artifact_definitions) |
| 81 |
return json_data |
| 82 |
|
| 83 |
|
| 84 |
class YamlArtifactsWriter(ArtifactWriter): |
| 85 |
"""YAML artifacts writer interface.""" |
| 86 |
|
| 87 |
def FormatArtifacts(self, artifacts): |
| 88 |
"""Formats artifacts to desired output format. |
| 89 |
|
| 90 |
Args: |
| 91 |
artifacts (list[ArtifactDefinition]): artifact definitions. |
| 92 |
|
| 93 |
Returns: |
| 94 |
str: formatted string of artifact definition. |
| 95 |
""" |
| 96 |
# TODO: improve output formatting of yaml |
| 97 |
artifact_definitions = [artifact.AsDict() for artifact in artifacts] |
| 98 |
yaml_data = yaml.safe_dump_all(artifact_definitions) |
| 99 |
return yaml_data |