From 538910ba01f256a64efa3dc67e5f79bc97bd5483 Mon Sep 17 00:00:00 2001 From: Ulugbek Abdullaev Date: Sat, 7 Dec 2019 23:55:31 +0100 Subject: [PATCH 1/2] add a check that filename passed to nd2reader has an extension .nd2 because the current implementation raises InvalidVersionError if the passed file is non-nd2 type, which is misleading add an exception InvalidFileType add a test for the new behavior of ND2Reader constructor --- nd2reader/exceptions.py | 8 ++++++++ nd2reader/reader.py | 6 +++++- tests/test_reader.py | 5 ++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/nd2reader/exceptions.py b/nd2reader/exceptions.py index cb1b1fb..fd31708 100644 --- a/nd2reader/exceptions.py +++ b/nd2reader/exceptions.py @@ -1,3 +1,11 @@ +class InvalidFileType(Exception): + """Non .nd2 extension file. + + File does not have an extension .nd2. + + """ + pass + class InvalidVersionError(Exception): """Unknown version. diff --git a/nd2reader/reader.py b/nd2reader/reader.py index 34c6386..0eeea16 100644 --- a/nd2reader/reader.py +++ b/nd2reader/reader.py @@ -1,7 +1,7 @@ from pims import Frame from pims.base_frames import FramesSequenceND -from nd2reader.exceptions import EmptyFileError +from nd2reader.exceptions import EmptyFileError, InvalidFileType from nd2reader.parser import Parser import numpy as np @@ -15,6 +15,10 @@ class ND2Reader(FramesSequenceND): def __init__(self, filename): super(ND2Reader, self).__init__() + + if not filename.endswith(".nd2"): + raise InvalidFileType(f"The file {filename} you want to read with nd2reader does not have extension .nd2.") + self.filename = filename # first use the parser to parse the file diff --git a/tests/test_reader.py b/tests/test_reader.py index 91f96e5..cf0f22c 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -4,12 +4,15 @@ import struct from pims import Frame from nd2reader.artificial import ArtificialND2 -from nd2reader.exceptions import EmptyFileError +from nd2reader.exceptions import EmptyFileError, InvalidFileType from nd2reader.reader import ND2Reader from nd2reader.parser import Parser class TestReader(unittest.TestCase): + def test_invalid_file_extension(self): + self.assertRaises(InvalidFileType, lambda: ND2Reader('test_data/invalid_extension_file.inv')) + def test_extension(self): self.assertTrue('nd2' in ND2Reader.class_exts()) From 595978cda0f7379990c3f8b8867db3cc3ce651d7 Mon Sep 17 00:00:00 2001 From: Gabriele Girelli Date: Mon, 17 Feb 2020 17:28:39 +0100 Subject: [PATCH 2/2] f-strings not supported by Python3.6- --- nd2reader/reader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nd2reader/reader.py b/nd2reader/reader.py index 0eeea16..7d9e4b9 100644 --- a/nd2reader/reader.py +++ b/nd2reader/reader.py @@ -17,7 +17,7 @@ class ND2Reader(FramesSequenceND): super(ND2Reader, self).__init__() if not filename.endswith(".nd2"): - raise InvalidFileType(f"The file {filename} you want to read with nd2reader does not have extension .nd2.") + raise InvalidFileType("The file %s you want to read with nd2reader does not have extension .nd2." % filename) self.filename = filename