Skip to content

Commit 65cd211

Browse files
authored
Replace deprecated method in csv2rdf (#2901)
* Replace deprecated method ConfigParser.readfp has been deprecated since Python 3.2 and is removed in 3.12. * Test that the config file is opened
1 parent 0a0d4b6 commit 65cd211

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

rdflib/tools/csv2rdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ def main():
482482

483483
if "-f" in opts:
484484
config = configparser.ConfigParser()
485-
config.readfp(open(opts["-f"]))
485+
config.read_file(open(opts["-f"]))
486486
for k, v in config.items("csv2rdf"):
487487
if k == "out":
488488
csv2rdf.OUT = codecs.open(v, "w", "utf-8")

test/test_tools/test_csv2rdf.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import subprocess
44
import sys
55
from tempfile import mkstemp
6+
from unittest.mock import mock_open, patch, sentinel
67

8+
from rdflib.tools import csv2rdf
79
from test.data import TEST_DATA_DIR
810

911
REALESTATE_FILE_PATH = os.path.join(TEST_DATA_DIR, "csv", "realestate.csv")
@@ -44,3 +46,20 @@ def test_csv2rdf_cli_fileout(self):
4446
assert len(f.readlines()) == 228
4547
os.close(fh)
4648
os.remove(fname)
49+
50+
@patch.object(csv2rdf.CSV2RDF, "convert", return_value=None)
51+
def test_csv2rdf_config_file_opened(self, config_mock):
52+
"""Test that the config file is read when specified."""
53+
# Pretend there is a file with the section we're looking for.
54+
# We don't care about the actual path, since it won't really be opened
55+
# but when we try to open it, we will get back the section header
56+
# so that the reader doesn't complain.
57+
config_file = sentinel.file_path
58+
open_mock = mock_open(read_data="[csv2rdf]")
59+
# Also pretend that we're passing the arguments from the command line
60+
cli_args = ["csv2rdf.py", "-f", config_file, str(REALESTATE_FILE_PATH)]
61+
with patch.object(csv2rdf.sys, "argv", cli_args):
62+
with patch("builtins.open", open_mock):
63+
csv2rdf.main()
64+
# Check that we've "opened" the right file
65+
open_mock.assert_called_once_with(config_file)

0 commit comments

Comments
 (0)