spdx_tools.spdx.parser.parse_anything

 1# SPDX-License-Identifier: Apache-2.0
 2# Copyright (c) spdx contributors
 3# Licensed under the Apache License, Version 2.0 (the "License");
 4# you may not use this file except in compliance with the License.
 5# You may obtain a copy of the License at
 6#     http://www.apache.org/licenses/LICENSE-2.0
 7# Unless required by applicable law or agreed to in writing, software
 8# distributed under the License is distributed on an "AS IS" BASIS,
 9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10# See the License for the specific language governing permissions and
11# limitations under the License.
12import logging
13
14from spdx_tools.spdx.formats import FileFormat, file_name_to_format
15from spdx_tools.spdx.parser.json import json_parser
16from spdx_tools.spdx.parser.rdf import rdf_parser
17from spdx_tools.spdx.parser.tagvalue import tagvalue_parser
18from spdx_tools.spdx.parser.xml import xml_parser
19from spdx_tools.spdx.parser.yaml import yaml_parser
20
21
22def parse_file(file_name: str, encoding: str = "utf-8"):
23    if encoding != "utf-8":
24        logging.warning(
25            "It's recommended to use the UTF-8 encoding for any SPDX file. Consider changing the encoding of the file."
26        )
27
28    input_format = file_name_to_format(file_name)
29    if input_format == FileFormat.RDF_XML:
30        return rdf_parser.parse_from_file(file_name, encoding)
31    elif input_format == FileFormat.TAG_VALUE:
32        return tagvalue_parser.parse_from_file(file_name, encoding)
33    elif input_format == FileFormat.JSON:
34        return json_parser.parse_from_file(file_name, encoding)
35    elif input_format == FileFormat.XML:
36        return xml_parser.parse_from_file(file_name, encoding)
37    elif input_format == FileFormat.YAML:
38        return yaml_parser.parse_from_file(file_name, encoding)
def parse_file(file_name: str, encoding: str = 'utf-8'):
23def parse_file(file_name: str, encoding: str = "utf-8"):
24    if encoding != "utf-8":
25        logging.warning(
26            "It's recommended to use the UTF-8 encoding for any SPDX file. Consider changing the encoding of the file."
27        )
28
29    input_format = file_name_to_format(file_name)
30    if input_format == FileFormat.RDF_XML:
31        return rdf_parser.parse_from_file(file_name, encoding)
32    elif input_format == FileFormat.TAG_VALUE:
33        return tagvalue_parser.parse_from_file(file_name, encoding)
34    elif input_format == FileFormat.JSON:
35        return json_parser.parse_from_file(file_name, encoding)
36    elif input_format == FileFormat.XML:
37        return xml_parser.parse_from_file(file_name, encoding)
38    elif input_format == FileFormat.YAML:
39        return yaml_parser.parse_from_file(file_name, encoding)