spdx_tools.spdx.writer.xml.xml_writer

 1# SPDX-FileCopyrightText: 2023 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4import xmltodict
 5from beartype.typing import IO
 6
 7from spdx_tools.spdx.jsonschema.document_converter import DocumentConverter
 8from spdx_tools.spdx.model import Document
 9from spdx_tools.spdx.writer.write_utils import convert, validate_and_deduplicate
10
11
12def write_document_to_stream(
13    document: Document,
14    stream: IO[str],
15    validate: bool = True,
16    converter: DocumentConverter = None,
17    drop_duplicates: bool = True,
18):
19    """
20    Serializes the provided document to XML and writes it to a file with the provided name. Unless validate is set
21    to False, validates the document before serialization. Unless a DocumentConverter instance is provided,
22    a new one is created.
23    """
24    document = validate_and_deduplicate(document, validate, drop_duplicates)
25    document_dict = {"Document": convert(document, converter)}
26    xmltodict.unparse(document_dict, stream, encoding="utf-8", pretty=True)
27
28
29def write_document_to_file(
30    document: Document,
31    file_name: str,
32    validate: bool = True,
33    converter: DocumentConverter = None,
34    drop_duplicates: bool = True,
35):
36    with open(file_name, "w", encoding="utf-8") as out:
37        write_document_to_stream(document, out, validate, converter, drop_duplicates)
def write_document_to_stream( document: spdx_tools.spdx.model.document.Document, stream: IO[str], validate: bool = True, converter: spdx_tools.spdx.jsonschema.document_converter.DocumentConverter = None, drop_duplicates: bool = True):
13def write_document_to_stream(
14    document: Document,
15    stream: IO[str],
16    validate: bool = True,
17    converter: DocumentConverter = None,
18    drop_duplicates: bool = True,
19):
20    """
21    Serializes the provided document to XML and writes it to a file with the provided name. Unless validate is set
22    to False, validates the document before serialization. Unless a DocumentConverter instance is provided,
23    a new one is created.
24    """
25    document = validate_and_deduplicate(document, validate, drop_duplicates)
26    document_dict = {"Document": convert(document, converter)}
27    xmltodict.unparse(document_dict, stream, encoding="utf-8", pretty=True)

Serializes the provided document to XML and writes it to a file with the provided name. Unless validate is set to False, validates the document before serialization. Unless a DocumentConverter instance is provided, a new one is created.

def write_document_to_file( document: spdx_tools.spdx.model.document.Document, file_name: str, validate: bool = True, converter: spdx_tools.spdx.jsonschema.document_converter.DocumentConverter = None, drop_duplicates: bool = True):
30def write_document_to_file(
31    document: Document,
32    file_name: str,
33    validate: bool = True,
34    converter: DocumentConverter = None,
35    drop_duplicates: bool = True,
36):
37    with open(file_name, "w", encoding="utf-8") as out:
38        write_document_to_stream(document, out, validate, converter, drop_duplicates)