Skip to content

Commit 2cd42a5

Browse files
committed
CU-8699vnuwf Ignore hidden files when loading model packs (#54)
* CU-8699vnuwf: Ignore hidden files/folders when loading model pack * CU-8699vnuwf: Add a test to make sure model pack can be loaded with hidden file/folders
1 parent 5be97fa commit 2cd42a5

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

medcat-v2/medcat/cat.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,9 @@ def load_model_pack(cls, model_pack_path: str,
722722
TOKENIZER_PREFIX,
723723
# components will be loaded semi-manually
724724
# within the creation of pipe
725-
COMPONENTS_FOLDER},
725+
COMPONENTS_FOLDER,
726+
# ignore hidden files/folders
727+
'.'},
726728
config_dict=config_dict,
727729
addon_config_dict=addon_config_dict)
728730
# NOTE: deserialising of components that need serialised

medcat-v2/tests/test_cat.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import unittest
2323
import tempfile
2424
import pickle
25+
import shutil
2526

2627
from . import EXAMPLE_MODEL_PACK_ZIP
2728
from . import V1_MODEL_PACK_PATH, UNPACKED_V1_MODEL_PACK_PATH
@@ -30,18 +31,19 @@
3031

3132
orig_init = cat.CAT.__init__
3233

34+
expected_model_pack_path = EXAMPLE_MODEL_PACK_ZIP.replace(".zip", "")
35+
3336

3437
class ModelLoadTests(unittest.TestCase):
3538

3639
def assert_has_model_name(self, func):
37-
expected_model_packl_name = EXAMPLE_MODEL_PACK_ZIP.replace(".zip", "")
3840

3941
def wrapper(*args, **kwargs):
4042
if 'model_load_path' in kwargs:
4143
self.assertEqual(kwargs['model_load_path'],
42-
expected_model_packl_name)
44+
expected_model_pack_path)
4345
else:
44-
self.assertEqual(args[-1], expected_model_packl_name)
46+
self.assertEqual(args[-1], expected_model_pack_path)
4547
return func(*args, **kwargs)
4648
return wrapper
4749

@@ -61,6 +63,31 @@ def test_can_load_CDB_from_model_pack(self):
6163
self.assertIsInstance(cdb, CDB)
6264

6365

66+
class ModelLoadIWithHiddenFilesTests(unittest.TestCase):
67+
68+
@classmethod
69+
def setUpClass(cls):
70+
cls._temp_dir = tempfile.TemporaryDirectory()
71+
cls.model_path = os.path.join(cls._temp_dir.name, "model")
72+
shutil.copytree(expected_model_pack_path, cls.model_path)
73+
# add file
74+
file_path = os.path.join(cls.model_path, '.some_file.txt')
75+
with open(file_path, 'w') as f:
76+
f.write("Nothing")
77+
# add folder
78+
folder_path = os.path.join(cls.model_path, '.some_folder')
79+
os.mkdir(folder_path)
80+
81+
@classmethod
82+
def tearDownClass(cls):
83+
cls._temp_dir.cleanup()
84+
85+
def test_can_load_with_add_hidden_files_and_folders(self):
86+
# try load
87+
inst = cat.CAT.load_model_pack(self.model_path)
88+
self.assertIsInstance(inst, cat.CAT)
89+
90+
6491
class TrainedModelTests(unittest.TestCase):
6592
TRAINED_MODEL_PATH = EXAMPLE_MODEL_PACK_ZIP
6693

0 commit comments

Comments
 (0)