8000 Merge pull request #759 from HomagGroup/feature/encoding_compliance · spdx/tools-python@e08e4d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit e08e4d2

Browse files
authored
Merge pull request #759 from HomagGroup/feature/encoding_compliance
implement recommended UTF-8 encoding for reading and writing SPDX files
2 parents ed9a135 + e8ae39d commit e08e4d2

File tree

11 files changed

+20
-22
lines changed

11 files changed

+20
-22
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44
import json
5-
from typing import Optional
65

76
from beartype.typing import Dict
87

98
from spdx_tools.spdx.model import Document
109
from spdx_tools.spdx.parser.jsonlikedict.json_like_dict_parser import JsonLikeDictParser
1110

1211

13-
def parse_from_file(file_name: str, encoding: Optional[str] = None) -> Document:
12+
def parse_from_file(file_name: str, encoding: str = "utf-8") -> Document:
1413
with open(file_name, encoding=encoding) as file:
1514
input_doc_as_dict: Dict = json.load(file)
1615

src/spdx_tools/spdx/parser/parse_anything.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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
12+
import logging
1313

1414
from spdx_tools.spdx.formats import FileFormat, file_name_to_format
1515
from spdx_tools.spdx.parser.json import json_parser
@@ -19,7 +19,12 @@
1919
from spdx_tools.spdx.parser.yaml import yaml_parser
2020

2121

22-
def parse_file(file_name: str, encoding: Optional[str] = None):
22+
def 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+
2328
input_format = file_name_to_format(file_name)
2429
if input_format == FileFormat.RDF_XML:
2530
return rdf_parser.parse_from_file(file_name, encoding)

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

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

@@ -24,7 +22,7 @@
2422
from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE
2523

2624

27-
def parse_from_file(file_name: str, encoding: Optional[str] = None) -> Document:
25+
def parse_from_file(file_name: str, encoding: str = "utf-8") -> Document:
2826
graph = Graph()
2927
with open(file_name, encoding=encoding) as file:
3028
graph.parse(file, format="xml")

src/spdx_tools/spdx/parser/tagvalue/tagvalue_parser.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# SPDX-FileCopyrightText: 2023 spdx contributors
22
#
33
# SPDX-License-Identifier: Apache-2.0
4-
from typing import Optional
5-
64
from spdx_tools.spdx.model import Document
75
from spdx_tools.spdx.parser.tagvalue.parser import Parser
86

97

10-
def parse_from_file(file_name: str, encoding: Optional[str] = None) -> Document:
8+
def parse_from_file(file_name: str, encoding: str = "utf-8") -> Document:
119
parser = Parser()
1210
with open(file_name, encoding=encoding) as file:
1311
data = file.read()

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

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

@@ -38,7 +36,7 @@
3836
]
3937

4038

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

src/spdx_tools/spdx/parser/yaml/yaml_parser.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
# SPDX-FileCopyrightText: 2023 spdx contributors
22
#
33
# SPDX-License-Identifier: Apache-2.0
4-
from typing import Optional
5-
64
import yaml
75
from beartype.typing import Dict
86

97
from spdx_tools.spdx.model import Document
108
from spdx_tools.spdx.parser.jsonlikedict.json_like_dict_parser import JsonLikeDictParser
119

1210

13-
def parse_from_file(file_name: str, encoding: Optional[str] = None) -> Document:
11+
def parse_from_file(file_name: str, encoding: str = "utf-8") -> Document:
1412
with open(file_name, encoding=encoding) as file:
1513
input_doc_as_dict: Dict = yaml.safe_load(file)
1614

src/spdx_tools/spdx/writer/json/json_writer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ def write_document_to_file(
3434
converter: DocumentConverter = None,
3535
drop_duplicates: bool = True,
3636
):
37-
with open(file_name, "w") as out:
37+
with open(file_name, "w", encoding="utf-8") as out:
3838
write_document_to_stream(document, out, validate, converter, drop_duplicates)

src/spdx_tools/spdx/writer/tagvalue/tagvalue_writer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def write_document_to_stream(document: Document, stream: TextIO, validate: bool
3535

3636

3737
def write_document_to_file(document: Document, file_name: str, validate: bool = True, drop_duplicates: bool = True):
38-
with open(file_name, "w") as out:
38+
with open(file_name, "w", encoding="utf-8") as out:
3939
write_document_to_stream(document, out, validate, drop_duplicates)
4040

4141

src/spdx_tools/spdx/writer/xml/xml_writer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ def write_document_to_file(
3333
converter: DocumentConverter = None,
3434
drop_duplicates: bool = True,
3535
):
36-
with open(file_name, "w") as out:
36+
with open(file_name, "w", encoding="utf-8") as out:
3737
write_document_to_stream(document, out, validate, converter, drop_duplicates)

src/spdx_tools/spdx/writer/yaml/yaml_writer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ def write_document_to_file(
3333
converter: DocumentConverter = None,
3434
drop_duplicates: bool = True,
3535
):
36-
with open(file_name, "w") as out:
36+
with open(file_name, "w", encoding="utf-8") as out:
3737
write_document_to_stream(document, out, validate, converter, drop_duplicates)

0 commit comments

Comments
 (0)
0