spdx_tools.spdx.formats

 1# SPDX-FileCopyrightText: 2022 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4from enum import Enum, auto
 5
 6from spdx_tools.spdx.parser.error import SPDXParsingError
 7
 8
 9class FileFormat(Enum):
10    JSON = auto()
11    YAML = auto()
12    XML = auto()
13    TAG_VALUE = auto()
14    RDF_XML = auto()
15
16
17def file_name_to_format(file_name: str) -> FileFormat:
18    if file_name.endswith(".rdf") or file_name.endswith(".rdf.xml"):
19        return FileFormat.RDF_XML
20    elif file_name.endswith(".tag") or file_name.endswith(".spdx"):
21        return FileFormat.TAG_VALUE
22    elif file_name.endswith(".json"):
23        return FileFormat.JSON
24    elif file_name.endswith(".xml"):
25        return FileFormat.XML
26    elif file_name.endswith(".yaml") or file_name.endswith(".yml"):
27        return FileFormat.YAML
28    else:
29        raise SPDXParsingError(["Unsupported SPDX file type: " + str(file_name)])
class FileFormat(enum.Enum):
10class FileFormat(Enum):
11    JSON = auto()
12    YAML = auto()
13    XML = auto()
14    TAG_VALUE = auto()
15    RDF_XML = auto()

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access::
>>> Color.RED
<Color.RED: 1>
  • value lookup:
>>> Color(1)
<Color.RED: 1>
  • name lookup:
>>> Color['RED']
<Color.RED: 1>

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes -- see the documentation for details.

JSON = <FileFormat.JSON: 1>
YAML = <FileFormat.YAML: 2>
XML = <FileFormat.XML: 3>
TAG_VALUE = <FileFormat.TAG_VALUE: 4>
RDF_XML = <FileFormat.RDF_XML: 5>
Inherited Members
enum.Enum
name
value
def file_name_to_format(file_name: str) -> FileFormat:
18def file_name_to_format(file_name: str) -> FileFormat:
19    if file_name.endswith(".rdf") or file_name.endswith(".rdf.xml"):
20        return FileFormat.RDF_XML
21    elif file_name.endswith(".tag") or file_name.endswith(".spdx"):
22        return FileFormat.TAG_VALUE
23    elif file_name.endswith(".json"):
24        return FileFormat.JSON
25    elif file_name.endswith(".xml"):
26        return FileFormat.XML
27    elif file_name.endswith(".yaml") or file_name.endswith(".yml"):
28        return FileFormat.YAML
29    else:
30        raise SPDXParsingError(["Unsupported SPDX file type: " + str(file_name)])