Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ArtifactWriter classes for writing ArtifactDefinitions
- Loading branch information
Showing
6 changed files
with
254 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
"""The Python 2 and 3 compatible type definitions.""" | ||
|
||
import sys | ||
|
||
if sys.version_info[0] < 3: | ||
BYTES_TYPE = str | ||
INTEGER_TYPES = (int, long) | ||
LONG_TYPE = long | ||
STRING_TYPES = (basestring,) | ||
UNICODE_TYPE = unicode | ||
else: | ||
BYTES_TYPE = bytes | ||
INTEGER_TYPES = (int,) | ||
LONG_TYPE = int | ||
STRING_TYPES = (str,) | ||
UNICODE_TYPE = str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# -*- coding: utf-8 -*- | ||
"""The artifact writer objects.""" | ||
|
||
import abc | ||
import json | ||
import yaml | ||
|
||
import artifacts.py2to3 as py2to3 | ||
from artifacts.artifact import ArtifactDefinition | ||
|
||
|
||
class BaseArtifactsWriter(object): | ||
"""Class that implements the artifacts writer interface.""" | ||
|
||
@abc.abstractmethod | ||
def WriteArtifactsFile(self, artifacts, filename): | ||
"""Writes artifact definitions to a file. | ||
Args: | ||
artifacts: a list of ArtifactDefinition objects to be written. | ||
filename: the filename to write artifacts to. | ||
""" | ||
|
||
@abc.abstractmethod | ||
def FormatArtifacts(self, artifacts): | ||
"""Formats artifacts to desired output format. | ||
Args: | ||
artifacts: an ArtifactDefinition instance or list of ArtifactDefinitions. | ||
Returns: | ||
formatted string of artifact definition. | ||
""" | ||
|
||
|
||
class ArtifactWriter(BaseArtifactsWriter): | ||
"""Class that implements the artifacts writer interface.""" | ||
|
||
def WriteArtifactsFile(self, artifacts, filename): | ||
"""Writes artifact definitions to a file. | ||
Args: | ||
artifacts: a list of ArtifactDefinition objects to be written. | ||
filename: the filename to write artifacts to. | ||
""" | ||
with open(filename, 'wb') as file_object: | ||
file_object.write(self.FormatArtifacts(artifacts)) | ||
|
||
|
||
class JsonArtifactsWriter(ArtifactWriter): | ||
"""Class that implements the JSON artifacts writer interface.""" | ||
|
||
def FormatArtifacts(self, artifacts): | ||
"""Formats artifacts to desired output format. | ||
Args: | ||
artifacts: a list of ArtifactDefinitions. | ||
Returns: | ||
formatted string of artifact definition. | ||
""" | ||
artifact_definitions = [artifact.AsDict() for artifact in artifacts] | ||
json_data = json.dumps(artifact_definitions) | ||
if isinstance(json_data, py2to3.BYTES_TYPE): | ||
try: | ||
return json_data.decode('ascii') | ||
except UnicodeDecodeError: | ||
pass | ||
else: | ||
return json_data | ||
|
||
|
||
class YamlArtifactsWriter(ArtifactWriter): | ||
"""Class that implements the YAML artifacts writer interface.""" | ||
|
||
def FormatArtifacts(self, artifacts): | ||
"""Formats artifacts to desired output format. | ||
Args: | ||
artifacts: a list of ArtifactDefinitions. | ||
Returns: | ||
formatted string of artifact definition. | ||
""" | ||
# TODO: improve output formatting of yaml | ||
artifact_definitions = [artifact.AsDict() for artifact in artifacts] | ||
yaml_data = yaml.safe_dump_all(artifact_definitions) | ||
if isinstance(yaml_data, py2to3.BYTES_TYPE): | ||
try: | ||
return yaml_data.decode('ascii') | ||
except UnicodeDecodeError: | ||
pass | ||
else: | ||
return yaml_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Tests for the artifact definitions readers.""" | ||
|
||
import io | ||
import os | ||
import unittest | ||
import json | ||
|
||
from artifacts import reader | ||
from artifacts import writer | ||
|
||
|
||
class YamlArtifactsWriterTest(unittest.TestCase): | ||
"""Class to test the YAML artifacts writer""" | ||
|
||
def testYamlWriter(self): | ||
"""Tests the YamlArtifactsWriter FormatArtifacts method for loss during conversion.""" | ||
artifact_reader = reader.YamlArtifactsReader() | ||
artifact_writer = writer.YamlArtifactsWriter() | ||
test_file = os.path.join('test_data', 'definitions.yaml') | ||
|
||
artifact_definitions = list(artifact_reader.ReadFile(test_file)) | ||
artifacts_yaml = artifact_writer.FormatArtifacts(artifact_definitions) | ||
|
||
file_object = io.StringIO(initial_value=artifacts_yaml) | ||
converted_artifact_definitions = list(artifact_reader.ReadFileObject( | ||
file_object)) | ||
|
||
self.assertListEqual( | ||
[artifact.AsDict() for artifact in artifact_definitions], | ||
[artifact.AsDict() for artifact in converted_artifact_definitions]) | ||
|
||
|
||
class JsonArtifactsWriterTest(unittest.TestCase): | ||
"""Class to test the JSON artifacts writer""" | ||
|
||
def testJsonWriter(self): | ||
"""Tests the JsonArtifactsWriter FormatArtifacts method for loss during conversion.""" | ||
artifact_reader = reader.YamlArtifactsReader() | ||
artifact_writer = writer.JsonArtifactsWriter() | ||
test_file = os.path.join('test_data', 'definitions.yaml') | ||
|
||
artifact_definitions = list(artifact_reader.ReadFile(test_file)) | ||
artifacts_json = artifact_writer.FormatArtifacts(artifact_definitions) | ||
|
||
converted_artifact_definitions = [ | ||
artifact_reader.ReadArtifactDefinitionValues(artifact_definition) | ||
for artifact_definition in json.loads(artifacts_json) | ||
] | ||
|
||
self.assertListEqual( | ||
[artifact.AsDict() for artifact in artifact_definitions], | ||
[artifact.AsDict() for artifact in converted_artifact_definitions]) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |