diff --git a/.gitignore b/.gitignore index b1d550b..aa265b5 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,5 @@ coverage.xml *.log *.pot +tests/test_data/ + diff --git a/nd2reader/common.py b/nd2reader/common.py index 3316bf9..c6ba459 100644 --- a/nd2reader/common.py +++ b/nd2reader/common.py @@ -1,3 +1,4 @@ +import os import struct import array from datetime import datetime @@ -329,6 +330,16 @@ def get_from_dict_if_exists(key, dictionary, convert_key_to_binary=True): if convert_key_to_binary: key = six.b(key) - if not key in dictionary: + if key not in dictionary: return None return dictionary[key] + + +def check_or_make_dir(directory): + """ + Check if a directory exists, if not, create it + Args: + directory: the path to the directory + """ + if not os.path.exists(directory): + os.makedirs(directory) diff --git a/tests/test_common.py b/tests/test_common.py index afee44b..5dea7a0 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -5,13 +5,14 @@ import struct from nd2reader.artificial import ArtificialND2 from nd2reader.common import get_version, parse_version, parse_date, _add_to_metadata, _parse_unsigned_char, \ - _parse_unsigned_int, _parse_unsigned_long, _parse_double + _parse_unsigned_int, _parse_unsigned_long, _parse_double, check_or_make_dir from nd2reader.exceptions import InvalidVersionError class TestCommon(unittest.TestCase): def setUp(self): dir_path = path.dirname(path.realpath(__file__)) + check_or_make_dir(path.join(dir_path, 'test_data/')) self.test_file = path.join(dir_path, 'test_data/test.nd2') def create_test_nd2(self): diff --git a/tests/test_legacy.py b/tests/test_legacy.py index c28d5d6..7ce29dc 100644 --- a/tests/test_legacy.py +++ b/tests/test_legacy.py @@ -2,6 +2,7 @@ import unittest from os import path from nd2reader.artificial import ArtificialND2 +from nd2reader.common import check_or_make_dir from nd2reader.legacy import Nd2 @@ -12,6 +13,7 @@ class TestLegacy(unittest.TestCase): def setUp(self): dir_path = path.dirname(path.realpath(__file__)) + check_or_make_dir(path.join(dir_path, 'test_data/')) self.test_file = path.join(dir_path, 'test_data/test.nd2') def test_can_open_test_file(self): diff --git a/tests/test_parser.py b/tests/test_parser.py index 04ec4f8..1558acc 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1,6 +1,7 @@ import unittest from os import path from nd2reader.artificial import ArtificialND2 +from nd2reader.common import check_or_make_dir from nd2reader.exceptions import InvalidVersionError from nd2reader.parser import Parser @@ -12,6 +13,7 @@ class TestParser(unittest.TestCase): def setUp(self): dir_path = path.dirname(path.realpath(__file__)) + check_or_make_dir(path.join(dir_path, 'test_data/')) self.test_file = path.join(dir_path, 'test_data/test.nd2') self.create_test_nd2() diff --git a/tests/test_reader.py b/tests/test_reader.py index c7842d7..5889a84 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -3,6 +3,7 @@ from os import path import numpy as np from nd2reader.artificial import ArtificialND2 +from nd2reader.common import check_or_make_dir from nd2reader.parser import Parser from nd2reader.reader import ND2Reader @@ -14,6 +15,7 @@ class TestReader(unittest.TestCase): def setUp(self): dir_path = path.dirname(path.realpath(__file__)) + check_or_make_dir(path.join(dir_path, 'test_data/')) self.test_file = path.join(dir_path, 'test_data/test.nd2') def test_can_open_test_file(self):