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.model import Document 16from spdx_tools.spdx.parser.json import json_parser 17from spdx_tools.spdx.parser.rdf import rdf_parser 18from spdx_tools.spdx.parser.tagvalue import tagvalue_parser 19from spdx_tools.spdx.parser.xml import xml_parser 20from spdx_tools.spdx.parser.yaml import yaml_parser 21 22 23def parse_file(file_name: str, encoding: str = "utf-8") -> Document: 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)
def
parse_file( file_name: str, encoding: str = 'utf-8') -> spdx_tools.spdx.model.document.Document:
24def parse_file(file_name: str, encoding: str = "utf-8") -> Document: 25 if encoding != "utf-8": 26 logging.warning( 27 "It's recommended to use the UTF-8 encoding for any SPDX file. Consider changing the encoding of the file." 28 ) 29 30 input_format = file_name_to_format(file_name) 31 if input_format == FileFormat.RDF_XML: 32 return rdf_parser.parse_from_file(file_name, encoding) 33 elif input_format == FileFormat.TAG_VALUE: 34 return tagvalue_parser.parse_from_file(file_name, encoding) 35 elif input_format == FileFormat.JSON: 36 return json_parser.parse_from_file(file_name, encoding) 37 elif input_format == FileFormat.XML: 38 return xml_parser.parse_from_file(file_name, encoding) 39 elif input_format == FileFormat.YAML: 40 return yaml_parser.parse_from_file(file_name, encoding)