Skip to content

add encoding parameter for parsing files #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/spdx_tools/spdx/parser/json/json_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
#
# SPDX-License-Identifier: Apache-2.0
import json
from typing import Optional

from beartype.typing import Dict

from spdx_tools.spdx.model import Document
from spdx_tools.spdx.parser.jsonlikedict.json_like_dict_parser import JsonLikeDictParser


def parse_from_file(file_name: str) -> Document:
with open(file_name) as file:
def parse_from_file(file_name: str, encoding: Optional[str] = None) -> Document:
with open(file_name, encoding=encoding) as file:
input_doc_as_dict: Dict = json.load(file)

return JsonLikeDictParser().parse(input_doc_as_dict)
14 changes: 8 additions & 6 deletions src/spdx_tools/spdx/parser/parse_anything.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional

from spdx_tools.spdx.formats import FileFormat, file_name_to_format
from spdx_tools.spdx.parser.json import json_parser
from spdx_tools.spdx.parser.rdf import rdf_parser
Expand All @@ -17,15 +19,15 @@
from spdx_tools.spdx.parser.yaml import yaml_parser


def parse_file(file_name: str):
def parse_file(file_name: str, encoding: Optional[str] = None):
input_format = file_name_to_format(file_name)
if input_format == FileFormat.RDF_XML:
return rdf_parser.parse_from_file(file_name)
return rdf_parser.parse_from_file(file_name, encoding)
elif input_format == FileFormat.TAG_VALUE:
return tagvalue_parser.parse_from_file(file_name)
return tagvalue_parser.parse_from_file(file_name, encoding)
elif input_format == FileFormat.JSON:
return json_parser.parse_from_file(file_name)
return json_parser.parse_from_file(file_name, encoding)
elif input_format == FileFormat.XML:
return xml_parser.parse_from_file(file_name)
return xml_parser.parse_from_file(file_name, encoding)
elif input_format == FileFormat.YAML:
return yaml_parser.parse_from_file(file_name)
return yaml_parser.parse_from_file(file_name, encoding)
6 changes: 4 additions & 2 deletions src/spdx_tools/spdx/parser/rdf/rdf_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: 2023 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
from typing import Optional

from beartype.typing import Any, Dict
from rdflib import RDF, Graph

Expand All @@ -22,9 +24,9 @@
from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE


def parse_from_file(file_name: str) -> Document:
def parse_from_file(file_name: str, encoding: Optional[str] = None) -> Document:
graph = Graph()
with open(file_name) as file:
with open(file_name, encoding=encoding) as file:
graph.parse(file, format="xml")

document: Document = translate_graph_to_document(graph)
Expand Down
6 changes: 4 additions & 2 deletions src/spdx_tools/spdx/parser/tagvalue/tagvalue_parser.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# SPDX-FileCopyrightText: 2023 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
from typing import Optional

from spdx_tools.spdx.model import Document
from spdx_tools.spdx.parser.tagvalue.parser import Parser


def parse_from_file(file_name: str) -> Document:
def parse_from_file(file_name: str, encoding: Optional[str] = None) -> Document:
parser = Parser()
with open(file_name) as file:
with open(file_name, encoding=encoding) as file:
data = file.read()
document: Document = parser.parse(data)
return document
6 changes: 4 additions & 2 deletions src/spdx_tools/spdx/parser/xml/xml_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: 2023 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
from typing import Optional

import xmltodict
from beartype.typing import Any, Dict

Expand Down Expand Up @@ -36,8 +38,8 @@
]


def parse_from_file(file_name: str) -> Document:
with open(file_name) as file:
def parse_from_file(file_name: str, encoding: Optional[str] = None) -> Document:
with open(file_name, encoding=encoding) as file:
parsed_xml: Dict = xmltodict.parse(file.read(), encoding="utf-8")

input_doc_as_dict: Dict = _fix_list_like_fields(parsed_xml).get("Document")
Expand Down
6 changes: 4 additions & 2 deletions src/spdx_tools/spdx/parser/yaml/yaml_parser.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# SPDX-FileCopyrightText: 2023 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
from typing import Optional

import yaml
from beartype.typing import Dict

from spdx_tools.spdx.model import Document
from spdx_tools.spdx.parser.jsonlikedict.json_like_dict_parser import JsonLikeDictParser


def parse_from_file(file_name: str) -> Document:
with open(file_name) as file:
def parse_from_file(file_name: str, encoding: Optional[str] = None) -> Document:
with open(file_name, encoding=encoding) as file:
input_doc_as_dict: Dict = yaml.safe_load(file)

return JsonLikeDictParser().parse(input_doc_as_dict)
Binary file added tests/spdx/data/SPDXJSONExample-UTF-16.spdx.json
Binary file not shown.
Binary file not shown.
Binary file added tests/spdx/data/SPDXTagExample-UTF-16.spdx
Binary file not shown.
Binary file added tests/spdx/data/SPDXXMLExample-UTF-16.spdx.xml
Binary file not shown.
Binary file added tests/spdx/data/SPDXYAMLExample-UTF-16.spdx.yaml
Binary file not shown.
15 changes: 14 additions & 1 deletion tests/spdx/parser/all_formats/test_parse_from_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_parse_from_file_with_2_3_example(self, parser, format_name, extension):
assert len(doc.relationships) == 13
assert len(doc.extracted_licensing_info) == 5

def test_parse_json_with_2_2_example(self, parser, format_name, extension):
def test_parse_from_file_with_2_2_example(self, parser, format_name, extension):
doc = parser.parse_from_file(
os.path.join(os.path.dirname(__file__), f"../../data/SPDX{format_name}Example-v2.2.spdx{extension}")
)
Expand All @@ -55,3 +55,16 @@ def test_parse_json_with_2_2_example(self, parser, format_name, extension):
assert len(doc.snippets) == 1
assert len(doc.relationships) == 11
assert len(doc.extracted_licensing_info) == 5

def test_parse_from_file_with_encoding_example(self, parser, format_name, extension):
doc = parser.parse_from_file(
os.path.join(os.path.dirname(__file__), f"../../data/SPDX{format_name}Example-UTF-16.spdx{extension}"),
"utf-16",
)
assert isinstance(doc, Document)
assert len(doc.annotations) == 5
assert len(doc.files) == 4
assert len(doc.packages) == 4
assert len(doc.snippets) == 1
assert len(doc.relationships) == 11
assert len(doc.extracted_licensing_info) == 5