From 67ad8fd46c5af6a43486a8c612bb7ab1d1cd8688 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Fri, 14 Feb 2020 12:40:01 -0800 Subject: [PATCH] add registering the writer to the config... make sure that extra modules don't get loaded.. --- casimport/__init__.py | 37 ++++++++++++++++++++++++++++++------- fixtures/writecache.conf | 15 +++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 fixtures/writecache.conf diff --git a/casimport/__init__.py b/casimport/__init__.py index 1c2f32c..95dc93f 100644 --- a/casimport/__init__.py +++ b/casimport/__init__.py @@ -230,6 +230,7 @@ class CASFinder(MetaPathFinder, Loader): def __init__(self): self._loaders = [] self._aliases = {} + self._writer = None if [ x for x in sys.meta_path if isinstance(x, self.__class__) ]: @@ -244,6 +245,9 @@ class CASFinder(MetaPathFinder, Loader): def __exit__(self, exc_type, exc_value, traceback): self.disconnect() + def register_write(self, wr): + self._writer = wr + def load_aliases(self, data): self._aliases.update(self._parsealiases(data)) @@ -406,16 +410,17 @@ def defaultinit(casf): modorder = [ x.strip() for x in cp['casimport']['module_prio'].split(',') ] + mods = {} for i in modorder: modfun = _supportedmodules[cp[i]['type']] - casf.register(modfun(cp[i])) - - cachedir = basedir / 'cache' - cachedir.mkdir(parents=True, exist_ok=True) + mod = modfun(cp[i]) + mods[i] = mod + casf.register(mod) - casf.register(FileDirCAS(cachedir)) - casf.register(IPFSCAS()) - casf.register(HTTPSCAS()) + try: + casf.register_write(mods[cp['casimport']['write_cache']]) + except KeyError: + pass def main(): # Thoughts on supported features: @@ -553,6 +558,21 @@ class Test(unittest.TestCase): # and that the second loader is the IPFSCAS self.assertIsInstance(f._loaders[1], IPFSCAS) + def test_writecache(self): + conf = self.fixtures / 'writecache.conf' + + # that w/ a custom config + with tempset(os.environ, 'CASIMPORT_CONF', str(conf)), \ + CASFinder() as f: + # that is gets loaded + defaultinit(f) + + # that the first loader is the cache + self.assertIsInstance(f._loaders[0], FileDirCAS) + + # and that the stored writer matches + self.assertIs(f._writer, f._loaders[0]) + def test_ipfsgwfromconfig(self): gwurl = 'https://www.example.com/somepath/' ipfsconf = dict(gateway=gwurl) @@ -592,6 +612,9 @@ class Test(unittest.TestCase): # and that the third loader is the HTTPSCAS self.assertIsInstance(f._loaders[2], HTTPSCAS) + # and that these are the only ones loaded + self.assertEqual(len(f._loaders), 3) + with CASFinder() as f: defaultinit(f) diff --git a/fixtures/writecache.conf b/fixtures/writecache.conf new file mode 100644 index 0000000..af7ebc9 --- /dev/null +++ b/fixtures/writecache.conf @@ -0,0 +1,15 @@ +[casimport] +module_prio = homecache, https + +write_cache = homecache + +[homecache] +# Default cache is in the user's home directory. + +type = cache +path = ~/.casimport/cache + +#size = 10MB + +[https] +type = https