Browse Source

add support for copying over the default config file... This is

in preparation to add support for configuring this module..
main
John-Mark Gurney 5 years ago
parent
commit
64f1829862
2 changed files with 62 additions and 5 deletions
  1. +30
    -5
      casimport/__init__.py
  2. +32
    -0
      casimport/default.conf

+ 30
- 5
casimport/__init__.py View File

@@ -23,6 +23,7 @@
# SUCH DAMAGE. # SUCH DAMAGE.


import contextlib import contextlib
import filecmp
import functools import functools
import glob import glob
import hashlib import hashlib
@@ -341,8 +342,18 @@ class CASFinder(MetaPathFinder, Loader):
exec(data, module.__dict__) exec(data, module.__dict__)


def defaultinit(casf): 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(FileDirCAS(cachedir))
casf.register(IPFSCAS()) casf.register(IPFSCAS())
@@ -458,18 +469,24 @@ class Test(unittest.TestCase):
def test_defaultinit(self): def test_defaultinit(self):
temphome = self.tempdir / 'home' temphome = self.tempdir / 'home'
temphome.mkdir() temphome.mkdir()
cachedir = temphome / '.casimport_cache'
defcachedir = temphome / '.casimport' / 'cache'


# testing w/ default config
with tempset(os.environ, 'HOME', str(temphome)): with tempset(os.environ, 'HOME', str(temphome)):
with CASFinder() as f: with CASFinder() as f:
# Setup the defaults # Setup the defaults
defaultinit(f) 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 # 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 # 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 # it can be imported
from cas.v1_f_330884aa2febb5e19fb7194ec6a69ed11dd3d77122f1a5175ee93e73cf0161c3 import hello from cas.v1_f_330884aa2febb5e19fb7194ec6a69ed11dd3d77122f1a5175ee93e73cf0161c3 import hello
@@ -525,10 +542,16 @@ class Test(unittest.TestCase):
'load_mod_aliases', f.load_mod_aliases): 'load_mod_aliases', f.load_mod_aliases):
defaultinit(f) 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 # and that hello.py is in the cache
shutil.copy(self.fixtures / 'hello.py', shutil.copy(self.fixtures / 'hello.py',
cachedir) cachedir)


self.assertNotIn('randpkg', sys.modules)

# that the import is successful # that the import is successful
import randpkg import randpkg


@@ -570,6 +593,8 @@ class Test(unittest.TestCase):


# and pulled in the method # and pulled in the method
self.assertTrue(hasattr(randpkg, 'hello')) self.assertTrue(hasattr(randpkg, 'hello'))

del sys.modules['randpkg']
finally: finally:
sys.path.remove(fixdir) sys.path.remove(fixdir)




+ 32
- 0
casimport/default.conf View File

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

Loading…
Cancel
Save