Browse Source

Merge pull request #32 from ulugbekna/handle-invalid-file-extension

add a check that filename passed to ND2Reader has extension .nd2
Ruben Verweij 5 years ago
committed by GitHub
parent
commit
235070c658
3 changed files with 17 additions and 2 deletions
  1. +8
    -0
      nd2reader/exceptions.py
  2. +5
    -1
      nd2reader/reader.py
  3. +4
    -1
      tests/test_reader.py

+ 8
- 0
nd2reader/exceptions.py View File

@ -1,3 +1,11 @@
class InvalidFileType(Exception):
"""Non .nd2 extension file.
File does not have an extension .nd2.
"""
pass
class InvalidVersionError(Exception):
"""Unknown version.


+ 5
- 1
nd2reader/reader.py View File

@ -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("The file %s you want to read with nd2reader does not have extension .nd2." % filename)
self.filename = filename
# first use the parser to parse the file


+ 4
- 1
tests/test_reader.py View File

@ -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())


Loading…
Cancel
Save