From 523e9498fb8227e7a173c0568d472a6249361e6b Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Sat, 1 Feb 2020 11:20:42 -0800 Subject: [PATCH] add a few comments, and a disconnect method --- casimport/__init__.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/casimport/__init__.py b/casimport/__init__.py index 52b2693..bfcb79d 100644 --- a/casimport/__init__.py +++ b/casimport/__init__.py @@ -66,6 +66,12 @@ class CASFinder(MetaPathFinder, Loader): sys.meta_path.append(self) + def disconnect(self): + try: + sys.meta_path.remove(self) + except ValueError: + pass + def register(self, loader): self._loaders.append(loader) @@ -98,12 +104,26 @@ import unittest class Test(unittest.TestCase): def test_casimport(self): + # That a CASFinder f = CASFinder() + + # when registering the fixtures directory f.register(FileDirCAS(os.path.join(os.path.dirname(__file__), '..', 'fixtures'))) import cas + # can import the function from cas.v1_f_330884aa2febb5e19fb7194ec6a69ed11dd3d77122f1a5175ee93e73cf0161c3 import hello name = 'Olof' + # and run the code self.assertEqual(hello(name), 'hello ' + name) + + # and when finished, can disconnect + f.disconnect() + + # and is no longer in the meta_path + self.assertNotIn(f, sys.meta_path) + + # and when disconnected as second time, nothing happens + f.disconnect()