Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise ValueError for unknown formats in the import/export OD APIs #476

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions canopen/objectdictionary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ def export_od(od, dest: Union[str, TextIO, None] = None, doc_type: Optional[str]
from canopen.objectdictionary import eds
return eds.export_dcf(od, dest)
else:
raise NotImplementedError(f"No support for the {doc_type!r} format")
allowed = ", ".join(doctypes)
raise ValueError(
f"Cannot export to the {doc_type!r} format; "
f"supported formats: {allowed}"
)
finally:
# If dest is opened in this fn, it should be closed
if opened_here:
Expand Down Expand Up @@ -88,7 +92,12 @@ def import_od(
from canopen.objectdictionary import epf
return epf.import_epf(source)
else:
raise NotImplementedError("No support for this format")
doc_type = suffix[1:]
allowed = ", ".join(["eds", "dcf", "epf"])
raise ValueError(
f"Cannot import from the {doc_type!r} format; "
f"supported formats: {allowed}"
)


class ObjectDictionary(MutableMapping):
Expand Down
6 changes: 5 additions & 1 deletion test/test_eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def test_load_nonexisting_file(self):
with self.assertRaises(IOError):
canopen.import_od('/path/to/wrong_file.eds')

def test_load_unsupported_format(self):
with self.assertRaisesRegex(ValueError, "'py'"):
canopen.import_od(__file__)

def test_load_file_object(self):
with open(EDS_PATH) as fp:
od = canopen.import_od(fp)
Expand Down Expand Up @@ -198,7 +202,7 @@ def test_export_eds(self):
self.verify_od(tempfile, doctype)

# Test for unknown doctype
with self.assertRaises(NotImplementedError):
with self.assertRaisesRegex(ValueError, "'unknown'"):
tempfile = str(Path(tempdir, "test.unknown"))
canopen.export_od(self.od, tempfile, doc_type="unknown")

Expand Down
Loading