Skip to content

Commit 503918c

Browse files
Raise ValueError for unknown formats in the import/export OD APIs (#476)
Fixes #475 * Test the offending suffix is part of the error message * Make the exception message more helpful
1 parent 5734f37 commit 503918c

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

canopen/objectdictionary/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ def export_od(od, dest: Union[str, TextIO, None] = None, doc_type: Optional[str]
5050
from canopen.objectdictionary import eds
5151
return eds.export_dcf(od, dest)
5252
else:
53-
raise NotImplementedError(f"No support for the {doc_type!r} format")
53+
allowed = ", ".join(doctypes)
54+
raise ValueError(
55+
f"Cannot export to the {doc_type!r} format; "
56+
f"supported formats: {allowed}"
57+
)
5458
finally:
5559
# If dest is opened in this fn, it should be closed
5660
if opened_here:
@@ -88,7 +92,12 @@ def import_od(
8892
from canopen.objectdictionary import epf
8993
return epf.import_epf(source)
9094
else:
91-
raise NotImplementedError("No support for this format")
95+
doc_type = suffix[1:]
96+
allowed = ", ".join(["eds", "dcf", "epf"])
97+
raise ValueError(
98+
f"Cannot import from the {doc_type!r} format; "
99+
f"supported formats: {allowed}"
100+
)
92101

93102

94103
class ObjectDictionary(MutableMapping):

test/test_eds.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def test_load_nonexisting_file(self):
5454
with self.assertRaises(IOError):
5555
canopen.import_od('/path/to/wrong_file.eds')
5656

57+
def test_load_unsupported_format(self):
58+
with self.assertRaisesRegex(ValueError, "'py'"):
59+
canopen.import_od(__file__)
60+
5761
def test_load_file_object(self):
5862
with open(EDS_PATH) as fp:
5963
od = canopen.import_od(fp)
@@ -198,7 +202,7 @@ def test_export_eds(self):
198202
self.verify_od(tempfile, doctype)
199203

200204
# Test for unknown doctype
201-
with self.assertRaises(NotImplementedError):
205+
with self.assertRaisesRegex(ValueError, "'unknown'"):
202206
tempfile = str(Path(tempdir, "test.unknown"))
203207
canopen.export_od(self.od, tempfile, doc_type="unknown")
204208

0 commit comments

Comments
 (0)