Browse Source

add registering the writer to the config...

make sure that extra modules don't get loaded..
main
John-Mark Gurney 5 years ago
parent
commit
67ad8fd46c
2 changed files with 45 additions and 7 deletions
  1. +30
    -7
      casimport/__init__.py
  2. +15
    -0
      fixtures/writecache.conf

+ 30
- 7
casimport/__init__.py View File

@@ -230,6 +230,7 @@ class CASFinder(MetaPathFinder, Loader):
def __init__(self): def __init__(self):
self._loaders = [] self._loaders = []
self._aliases = {} self._aliases = {}
self._writer = None


if [ x for x in sys.meta_path if if [ x for x in sys.meta_path if
isinstance(x, self.__class__) ]: isinstance(x, self.__class__) ]:
@@ -244,6 +245,9 @@ class CASFinder(MetaPathFinder, Loader):
def __exit__(self, exc_type, exc_value, traceback): def __exit__(self, exc_type, exc_value, traceback):
self.disconnect() self.disconnect()


def register_write(self, wr):
self._writer = wr

def load_aliases(self, data): def load_aliases(self, data):
self._aliases.update(self._parsealiases(data)) self._aliases.update(self._parsealiases(data))


@@ -406,16 +410,17 @@ def defaultinit(casf):
modorder = [ x.strip() for x in modorder = [ x.strip() for x in
cp['casimport']['module_prio'].split(',') ] cp['casimport']['module_prio'].split(',') ]


mods = {}
for i in modorder: for i in modorder:
modfun = _supportedmodules[cp[i]['type']] 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(): def main():
# Thoughts on supported features: # Thoughts on supported features:
@@ -553,6 +558,21 @@ class Test(unittest.TestCase):
# and that the second loader is the IPFSCAS # and that the second loader is the IPFSCAS
self.assertIsInstance(f._loaders[1], 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): def test_ipfsgwfromconfig(self):
gwurl = 'https://www.example.com/somepath/' gwurl = 'https://www.example.com/somepath/'
ipfsconf = dict(gateway=gwurl) ipfsconf = dict(gateway=gwurl)
@@ -592,6 +612,9 @@ class Test(unittest.TestCase):
# and that the third loader is the HTTPSCAS # and that the third loader is the HTTPSCAS
self.assertIsInstance(f._loaders[2], 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: with CASFinder() as f:
defaultinit(f) defaultinit(f)




+ 15
- 0
fixtures/writecache.conf View File

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

Loading…
Cancel
Save