Skip to content

Commit

Permalink
Replace deprecated method in csv2rdf (#2901)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ageorgou committed Sep 1, 2024
1 parent 0a0d4b6 commit 65cd211
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rdflib/tools/csv2rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def main():

if "-f" in opts:
config = configparser.ConfigParser()
config.readfp(open(opts["-f"]))
config.read_file(open(opts["-f"]))
for k, v in config.items("csv2rdf"):
if k == "out":
csv2rdf.OUT = codecs.open(v, "w", "utf-8")
Expand Down
19 changes: 19 additions & 0 deletions test/test_tools/test_csv2rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import subprocess
import sys
from tempfile import mkstemp
from unittest.mock import mock_open, patch, sentinel

from rdflib.tools import csv2rdf
from test.data import TEST_DATA_DIR

REALESTATE_FILE_PATH = os.path.join(TEST_DATA_DIR, "csv", "realestate.csv")
Expand Down Expand Up @@ -44,3 +46,20 @@ def test_csv2rdf_cli_fileout(self):
assert len(f.readlines()) == 228
os.close(fh)
os.remove(fname)

@patch.object(csv2rdf.CSV2RDF, "convert", return_value=None)
def test_csv2rdf_config_file_opened(self, config_mock):
"""Test that the config file is read when specified."""
# Pretend there is a file with the section we're looking for.
# We don't care about the actual path, since it won't really be opened
# but when we try to open it, we will get back the section header
# so that the reader doesn't complain.
config_file = sentinel.file_path
open_mock = mock_open(read_data="[csv2rdf]")
# Also pretend that we're passing the arguments from the command line
cli_args = ["csv2rdf.py", "-f", config_file, str(REALESTATE_FILE_PATH)]
with patch.object(csv2rdf.sys, "argv", cli_args):
with patch("builtins.open", open_mock):
csv2rdf.main()
# Check that we've "opened" the right file
open_mock.assert_called_once_with(config_file)

0 comments on commit 65cd211

Please sign in to comment.