diff --git a/casimport/__init__.py b/casimport/__init__.py index a07cc98..13520c0 100644 --- a/casimport/__init__.py +++ b/casimport/__init__.py @@ -23,6 +23,7 @@ # SUCH DAMAGE. import contextlib +import filecmp import functools import glob import hashlib @@ -341,8 +342,18 @@ class CASFinder(MetaPathFinder, Loader): exec(data, module.__dict__) def defaultinit(casf): - cachedir = pathlib.Path.home() / '.casimport_cache' - cachedir.mkdir(exist_ok=True) + basedir = pathlib.Path.home() / '.casimport' + + conffile = basedir / 'casimport.conf' + if not conffile.exists(): + basedir.mkdir(exist_ok=True) + import casimport + with importlib.resources.path(casimport, + 'default.conf') as defconf: + shutil.copy(defconf, conffile) + + cachedir = basedir / 'cache' + cachedir.mkdir(parents=True, exist_ok=True) casf.register(FileDirCAS(cachedir)) casf.register(IPFSCAS()) @@ -458,18 +469,24 @@ class Test(unittest.TestCase): def test_defaultinit(self): temphome = self.tempdir / 'home' temphome.mkdir() - cachedir = temphome / '.casimport_cache' + defcachedir = temphome / '.casimport' / 'cache' + # testing w/ default config with tempset(os.environ, 'HOME', str(temphome)): with CASFinder() as f: # Setup the defaults defaultinit(f) + # That the default.conf file got copied over. + filecmp.cmp(defcachedir.parent / + 'casimport.conf', + pathlib.Path(__file__).parent / 'default.conf') + # that the cache got created - self.assertTrue(cachedir.is_dir()) + self.assertTrue(defcachedir.is_dir()) # and that when hello.py is copied to the cache - shutil.copy(self.fixtures / 'hello.py', cachedir) + shutil.copy(self.fixtures / 'hello.py', defcachedir) # it can be imported from cas.v1_f_330884aa2febb5e19fb7194ec6a69ed11dd3d77122f1a5175ee93e73cf0161c3 import hello @@ -525,10 +542,16 @@ class Test(unittest.TestCase): 'load_mod_aliases', f.load_mod_aliases): defaultinit(f) + # XXX - fix this so this is properly + # used. It's using other methods to + # work currently + # and that hello.py is in the cache shutil.copy(self.fixtures / 'hello.py', cachedir) + self.assertNotIn('randpkg', sys.modules) + # that the import is successful import randpkg @@ -570,6 +593,8 @@ class Test(unittest.TestCase): # and pulled in the method self.assertTrue(hasattr(randpkg, 'hello')) + + del sys.modules['randpkg'] finally: sys.path.remove(fixdir) diff --git a/casimport/default.conf b/casimport/default.conf new file mode 100644 index 0000000..4b25866 --- /dev/null +++ b/casimport/default.conf @@ -0,0 +1,32 @@ +[casimport] +# List of priority to search for files to import +# +# Each item is a name of a section which defines the properties of that +# item. This allows for multiple caches to be defines, such as a +# read-only one vs a local writable one. + +module_prio = homecache, ipfsgw, https + +[homecache] +# Default cache is in the user's home directory. + +type = cache +dir = ~/.casimport/cache + +#size = 10MB + +[ipfsgw] +# The ipfs module converts ipfs://cidvX urls into a gateway call. +# This could be configured to be the local ipfs gateway if you run +# one. If the local gateway isn't running all the time, one could +# configure a backup gateway that uses one of the common public +# gateways: https://ipfs.github.io/public-gateway-checker/ + +type = ipfs +gateway = https://gateway.ipfs.io/ipfs/ + +[https] +# This is just to handle https requests. Without this, they won't be +# resolved. + +type = https