# Path: spdx/writers/tagvalue.py
# class InvalidDocumentError(Exception):
#     """
#     Raised when attempting to write an invalid document.
#     """
# 
#     pass
# 
# Path: spdx/writers/jsonyamlxml.py
# class Writer(
#     CreationInfoWriter,
#     ReviewInfoWriter,
#     FileWriter,
#     PackageWriter,
#     AnnotationInfoWriter,
#     RelationshipInfoWriter,
#     SnippetWriter,
#     ExtractedLicenseWriter,
# ):
#     """
#     Wrapper for the other writers.
#     Represent a whole SPDX Document as json-serializable objects to then
#     be written as json or yaml files.
#     """
# 
#     def __init__(self, document):
#         super(Writer, self).__init__(document)
# 
#     def create_ext_document_references(self):
#         """
#         Create the External Document References json-serializable representation
#         """
#         ext_document_references_field = self.document.ext_document_references
#         ext_document_reference_objects = []
#         for ext_document_reference in ext_document_references_field:
#             ext_document_reference_object = dict()
#             ext_document_reference_object[
#                 "externalDocumentId"
#             ] = ext_document_reference.external_document_id
#             ext_document_reference_object[
#                 "spdxDocument"
#             ] = ext_document_reference.spdx_document_uri
# 
#             ext_document_reference_object["checksum"] = self.checksum(
#                 ext_document_reference.check_sum
#             )
# 
#             ext_document_reference_objects.append(ext_document_reference_object)
# 
#         return ext_document_reference_objects
# 
#     def create_document(self):
#         self.document_object = dict()
# 
#         self.document_object["spdxVersion"] = self.document.version.__str__()
#         self.document_object["documentNamespace"] = self.document.namespace.__str__()
#         self.document_object["creationInfo"] = self.create_creation_info()
#         self.document_object["dataLicense"] = self.license(self.document.data_license)
#         self.document_object["SPDXID"] = self.spdx_id(self.document.spdx_id)
#         self.document_object["name"] = self.document.name
# 
#         package_objects = []
#         for package in self.document.packages:
#             package_info_object = self.create_package_info(package)
#             package_info_object["files"] = self.create_file_info(package)
#             package_objects.append({"Package": package_info_object})
# 
#         self.document_object["documentDescribes"] = package_objects
# 
#         if self.document.has_comment:
#             self.document_object["comment"] = self.document.comment
# 
#         if self.document.ext_document_references:
#             self.document_object[
#                 "externalDocumentRefs"
#             ] = self.create_ext_document_references()
# 
#         if self.document.extracted_licenses:
#             self.document_object[
#                 "hasExtractedLicensingInfos"
#             ] = self.create_extracted_license()
# 
#         if self.document.reviews:
#             self.document_object["reviewers"] = self.create_review_info()
# 
#         if self.document.snippet:
#             self.document_object["snippets"] = self.create_snippet_info()
# 
#         if self.document.annotations:
#             self.document_object["annotations"] = self.create_annotation_info()
# 
#         if self.document.relationships:
#             self.document_object["relationships"] = self.create_relationship_info()
# 
#         return {"Document": self.document_object}
# 
# Path: spdx/parsers/loggers.py
# class ErrorMessages:
# 
#     def __init__(self):
#         self.messages = []
#         self.context = []
#     
#     def push_context(self, context):
#         """push some context information to better indentify where is the problem"""
#         self.context.append(context)
# 
#     def pop_context(self):
#         """pop the last context information"""
#         self.context.pop()
#     
#     def append(self, message, *args, **kwargs):
#         """add a message with standard python format
#         the current context is prefixed to the message
#         """
#         message = message.format(*args, **kwargs)
#         message = "".join([c + ": " for c in self.context if c]) + message
#         self.messages.append(message)
# 
#     def __iter__(self):
#         return self.messages.__iter__()
# 
#     def __bool__(self):
#         return len(self.messages)>0
# 
#     def __nonzero__(self):
#         return len(self.messages)>0
#     
#     def __eq__(self, b):
#         if isinstance(b, ErrorMessages):
#             return self.messages == b.messages
#         return self.messages == b


import yaml
from spdx.writers.tagvalue import InvalidDocumentError
from spdx.writers.jsonyamlxml import Writer
from spdx.parsers.loggers import ErrorMessages
# Copyright (c) Xavier Figueroa
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.




def write_document(document, out, validate=True):

    if validate:
        messages = ErrorMessages()
        messages = document.validate(messages)
        if messages:
            raise InvalidDocumentError(messages)

    writer = Writer(document)
    document_object = writer.create_document()


# Path: spdx/writers/tagvalue.py
# class InvalidDocumentError(Exception):
#     """
#     Raised when attempting to write an invalid document.
#     """
# 
#     pass
# 
# Path: spdx/writers/jsonyamlxml.py
# class Writer(
#     CreationInfoWriter,
#     ReviewInfoWriter,
#     FileWriter,
#     PackageWriter,
#     AnnotationInfoWriter,
#     RelationshipInfoWriter,
#     SnippetWriter,
#     ExtractedLicenseWriter,
# ):
#     """
#     Wrapper for the other writers.
#     Represent a whole SPDX Document as json-serializable objects to then
#     be written as json or yaml files.
#     """
# 
#     def __init__(self, document):
#         super(Writer, self).__init__(document)
# 
#     def create_ext_document_references(self):
#         """
#         Create the External Document References json-serializable representation
#         """
#         ext_document_references_field = self.document.ext_document_references
#         ext_document_reference_objects = []
#         for ext_document_reference in ext_document_references_field:
#             ext_document_reference_object = dict()
#             ext_document_reference_object[
#                 "externalDocumentId"
#             ] = ext_document_reference.external_document_id
#             ext_document_reference_object[
#                 "spdxDocument"
#             ] = ext_document_reference.spdx_document_uri
# 
#             ext_document_reference_object["checksum"] = self.checksum(
#                 ext_document_reference.check_sum
#             )
# 
#             ext_document_reference_objects.append(ext_document_reference_object)
# 
#         return ext_document_reference_objects
# 
#     def create_document(self):
#         self.document_object = dict()
# 
#         self.document_object["spdxVersion"] = self.document.version.__str__()
#         self.document_object["documentNamespace"] = self.document.namespace.__str__()
#         self.document_object["creationInfo"] = self.create_creation_info()
#         self.document_object["dataLicense"] = self.license(self.document.data_license)
#         self.document_object["SPDXID"] = self.spdx_id(self.document.spdx_id)
#         self.document_object["name"] = self.document.name
# 
#         package_objects = []
#         for package in self.document.packages:
#             package_info_object = self.create_package_info(package)
#             package_info_object["files"] = self.create_file_info(package)
#             package_objects.append({"Package": package_info_object})
# 
#         self.document_object["documentDescribes"] = package_objects
# 
#         if self.document.has_comment:
#             self.document_object["comment"] = self.document.comment
# 
#         if self.document.ext_document_references:
#             self.document_object[
#                 "externalDocumentRefs"
#             ] = self.create_ext_document_references()
# 
#         if self.document.extracted_licenses:
#             self.document_object[
#                 "hasExtractedLicensingInfos"
#             ] = self.create_extracted_license()
# 
#         if self.document.reviews:
#             self.document_object["reviewers"] = self.create_review_info()
# 
#         if self.document.snippet:
#             self.document_object["snippets"] = self.create_snippet_info()
# 
#         if self.document.annotations:
#             self.document_object["annotations"] = self.create_annotation_info()
# 
#         if self.document.relationships:
#             self.document_object["relationships"] = self.create_relationship_info()
# 
#         return {"Document": self.document_object}
# 
# Path: spdx/parsers/loggers.py
# class ErrorMessages:
# 
#     def __init__(self):
#         self.messages = []
#         self.context = []
#     
#     def push_context(self, context):
#         """push some context information to better indentify where is the problem"""
#         self.context.append(context)
# 
#     def pop_context(self):
#         """pop the last context information"""
#         self.context.pop()
#     
#     def append(self, message, *args, **kwargs):
#         """add a message with standard python format
#         the current context is prefixed to the message
#         """
#         message = message.format(*args, **kwargs)
#         message = "".join([c + ": " for c in self.context if c]) + message
#         self.messages.append(message)
# 
#     def __iter__(self):
#         return self.messages.__iter__()
# 
#     def __bool__(self):
#         return len(self.messages)>0
# 
#     def __nonzero__(self):
#         return len(self.messages)>0
#     
#     def __eq__(self, b):
#         if isinstance(b, ErrorMessages):
#             return self.messages == b.messages
#         return self.messages == b


# Path: spdx/writers/yaml.py
import yaml
from spdx.writers.tagvalue import InvalidDocumentError
from spdx.writers.jsonyamlxml import Writer
from spdx.parsers.loggers import ErrorMessages

# Copyright (c) Xavier Figueroa
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.




def write_document(document, out, validate=True):

    if validate:
        messages = ErrorMessages()
        messages = document.validate(messages)
        if messages:
            raise InvalidDocumentError(messages)

    writer = Writer(document)
    document_object = writer.create_document()


    yaml.safe_dump(document_object, out, indent=2, explicit_start=True)