Skip to content

Commit 44196ef

Browse files
Christian Deckerarmintaenzertng
authored andcommitted
add encoding parameter for parsing files
Signed-off-by: Christian Decker <christian.decker@homag.com>
1 parent 777bd27 commit 44196ef

12 files changed

+41
-17
lines changed

src/spdx_tools/spdx/parser/json/json_parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44
import json
5+
from typing import Optional
56

67
from beartype.typing import Dict
78

89
from spdx_tools.spdx.model import Document
910
from spdx_tools.spdx.parser.jsonlikedict.json_like_dict_parser import JsonLikeDictParser
1011

1112

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

1617
return JsonLikeDictParser().parse(input_doc_as_dict)

src/spdx_tools/spdx/parser/parse_anything.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
12+
from typing import Optional
13+
1214
from spdx_tools.spdx.formats import FileFormat, file_name_to_format
1315
from spdx_tools.spdx.parser.json import json_parser
1416
from spdx_tools.spdx.parser.rdf import rdf_parser
@@ -17,15 +19,15 @@
1719
from spdx_tools.spdx.parser.yaml import yaml_parser
1820

1921

20-
def parse_file(file_name: str):
22+
def parse_file(file_name: str, encoding: Optional[str] = None):
2123
input_format = file_name_to_format(file_name)
2224
if input_format == FileFormat.RDF_XML:
23-
return rdf_parser.parse_from_file(file_name)
25+
return rdf_parser.parse_from_file(file_name, encoding)
2426
elif input_format == FileFormat.TAG_VALUE:
25-
return tagvalue_parser.parse_from_file(file_name)
27+
return tagvalue_parser.parse_from_file(file_name, encoding)
2628
elif input_format == FileFormat.JSON:
27-
return json_parser.parse_from_file(file_name)
29+
return json_parser.parse_from_file(file_name, encoding)
2830
elif input_format == FileFormat.XML:
29-
return xml_parser.parse_from_file(file_name)
31+
return xml_parser.parse_from_file(file_name, encoding)
3032
elif input_format == FileFormat.YAML:
31-
return yaml_parser.parse_from_file(file_name)
33+
return yaml_parser.parse_from_file(file_name, encoding)

src/spdx_tools/spdx/parser/rdf/rdf_parser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# SPDX-FileCopyrightText: 2023 spdx contributors
22
#
33
# SPDX-License-Identifier: Apache-2.0
4+
from typing import Optional
5+
46
from beartype.typing import Any, Dict
57
from rdflib import RDF, Graph
68

@@ -22,9 +24,9 @@
2224
from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE
2325

2426

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

3032
document: Document = translate_graph_to_document(graph)
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# SPDX-FileCopyrightText: 2023 spdx contributors
22
#
33
# SPDX-License-Identifier: Apache-2.0
4+
from typing import Optional
5+
46
from spdx_tools.spdx.model import Document
57
from spdx_tools.spdx.parser.tagvalue.parser import Parser
68

79

8-
def parse_from_file(file_name: str) -> Document:
10+
def parse_from_file(file_name: str, encoding: Optional[str] = None) -> Document:
911
parser = Parser()
10-
with open(file_name) as file:
12+
with open(file_name, encoding=encoding) as file:
1113
data = file.read()
1214
document: Document = parser.parse(data)
1315
return document

src/spdx_tools/spdx/parser/xml/xml_parser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# SPDX-FileCopyrightText: 2023 spdx contributors
22
#
33
# SPDX-License-Identifier: Apache-2.0
4+
from typing import Optional
5+
46
import xmltodict
57
from beartype.typing import Any, Dict
68

@@ -36,8 +38,8 @@
3638
]
3739

3840

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

4345
input_doc_as_dict: Dict = _fix_list_like_fields(parsed_xml).get("Document")
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# SPDX-FileCopyrightText: 2023 spdx contributors
22
#
33
# SPDX-License-Identifier: Apache-2.0
4+
from typing import Optional
5+
46
import yaml
57
from beartype.typing import Dict
68

79
from spdx_tools.spdx.model import Document
810
from spdx_tools.spdx.parser.jsonlikedict.json_like_dict_parser import JsonLikeDictParser
911

1012

11-
def parse_from_file(file_name: str) -> Document:
12-
with open(file_name) as file:
13+
def parse_from_file(file_name: str, encoding: Optional[str] = None) -> Document:
14+
with open(file_name, encoding=encoding) as file:
1315
input_doc_as_dict: Dict = yaml.safe_load(file)
1416

1517
return JsonLikeDictParser().parse(input_doc_as_dict)
41.2 KB
Binary file not shown.
Binary file not shown.
36 KB
Binary file not shown.
47.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)