def write_document(document, out): """Writes out a tag value representation of the document. Out must implement a method write that takes a single string. """ messages = [] if not document.validate(messages): raise InvalidDocumentError(messages) # Write out document information out.write('# Document Information\n\n') version_value = 'SPDX-{0}.{1}'.format(document.version.major, document.version.minor) write_value('SPDXVersion', version_value, out) write_value('DataLicense', document.data_license.identifier, out) if document.has_comment: write_text_value('DocumentComment', document.comment, out) write_seperators(out) # Write out creation info write_creation_info(document.creation_info, out) write_seperators(out) # Write out reviews for review in document.reviews: write_review(review, out) write_seperators(out) # Write out package info write_package(document.package, out) write_seperators(out) out.write('# Extracted Licenses\n\n') for lic in document.extracted_licenses: write_extracted_licenses(lic, out) write_seperators(out)
def write_document(document, out, validate=True): """ Write an SPDX tag value document. - document - spdx.document instance. - out - file like object that will be written to. Optionally `validate` the document before writing and raise InvalidDocumentError if document.validate returns False. """ messages = [] messages = document.validate(messages) if validate and messages: raise InvalidDocumentError(messages) # Write out document information out.write('# Document Information\n\n') write_value('SPDXVersion', str(document.version), out) write_value('DataLicense', document.data_license.identifier, out) if document.name: write_value('DocumentName', document.name, out) write_value('SPDXID', 'SPDXRef-DOCUMENT', out) if document.namespace: write_value('DocumentNamespace', document.namespace, out) if document.has_comment: write_text_value('DocumentComment', document.comment, out) for doc_ref in document.ext_document_references: doc_ref_str = ' '.join([doc_ref.external_document_id, doc_ref.spdx_document_uri, doc_ref.check_sum.identifier + ':' + doc_ref.check_sum.value]) write_value('ExternalDocumentRef', doc_ref_str, out) write_separators(out) # Write out creation info write_creation_info(document.creation_info, out) write_separators(out) # Writesorted reviews for review in sorted(document.reviews): write_review(review, out) write_separators(out) # Write sorted annotations for annotation in sorted(document.annotations): write_annotation(annotation, out) write_separators(out) # Write out package info write_package(document.package, out) write_separators(out) # Write out snippet info for snippet in document.snippet: write_snippet(snippet, out) write_separators(out) out.write('# Extracted Licenses\n\n') for lic in sorted(document.extracted_licenses): write_extracted_licenses(lic, out) write_separators(out)
def write_document(document, out): """Writes a document. document - spdx.document instance. out - file like object that will be written to. raises InvalidDocumentError if document.validate returns False. """ if not document.validate([]): raise InvalidDocumentError() writer = Writer(document, out) writer.write()
def write_document(document, out, validate=True): """ Write an SPDX RDF document. - document - spdx.document instance. - out - file like object that will be written to. Optionally `validate` the document before writing and raise InvalidDocumentError if document.validate returns False. """ if validate: messages = [] messages = document.validate(messages) if messages: raise InvalidDocumentError(messages) writer = Writer(document, out) writer.write()
def write_document(document, out, validate=True): """ Write an SPDX tag value document. - document - spdx.document instance. - out - file like object that will be written to. Optionally `validate` the document before writing and raise InvalidDocumentError if document.validate returns False. """ messages = ErrorMessages() messages = document.validate(messages) if validate and messages: raise InvalidDocumentError(messages) # Write out document information out.write("# Document Information\n\n") write_value("SPDXVersion", str(document.version), out) write_value("DataLicense", document.data_license.identifier, out) if document.namespace: write_value("DocumentNamespace", document.namespace, out) if document.name: write_value("DocumentName", document.name, out) if document.license_list_version: write_value("LicenseListVersion", str(document.license_list_version), out) write_value("SPDXID", "SPDXRef-DOCUMENT", out) if document.has_comment: write_text_value("DocumentComment", document.comment, out) for doc_ref in document.ext_document_references: doc_ref_str = " ".join([ doc_ref.external_document_id, doc_ref.spdx_document_uri, doc_ref.check_sum.identifier + ":" + doc_ref.check_sum.value, ]) write_value("ExternalDocumentRef", doc_ref_str, out) write_separators(out) # Write out creation info write_creation_info(document.creation_info, out) write_separators(out) # Writesorted reviews for review in sorted(document.reviews): write_review(review, out) write_separators(out) # Write sorted annotations for annotation in sorted(document.annotations): write_annotation(annotation, out) write_separators(out) # Write relationships for relationship in document.relationships: write_relationship(relationship, out) write_separators(out) # Write out package info for package in document.packages: write_package(package, out) write_separators(out) # Write out snippet info for snippet in document.snippet: write_snippet(snippet, out) write_separators(out) out.write("# Extracted Licenses\n\n") for lic in sorted(document.extracted_licenses): write_extracted_licenses(lic, out) write_separators(out)