瀏覽代碼

simplify function, and make it so that aliases and hashes of the same

value return the same module, so the same code doesn't get imported
multiple times..
main
John-Mark Gurney 4 年之前
父節點
當前提交
699af38a90
共有 1 個檔案被更改,包括 54 行新增22 行删除
  1. +54
    -22
      casimport/__init__.py

+ 54
- 22
casimport/__init__.py 查看文件

@@ -215,12 +215,15 @@ class CASFinder(MetaPathFinder, Loader):
def __exit__(self, exc_type, exc_value, traceback):
self.disconnect()

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

def load_mod_aliases(self, name):
'''Load the aliases from the module with the passed in name.'''

aliases = importlib.resources.read_text(sys.modules[name],
'cas_aliases.txt')
self._aliases.update(self._parsealiases(aliases))
self.load_aliases(aliases)

@staticmethod
def _makebasichashurl(url):
@@ -302,6 +305,9 @@ class CASFinder(MetaPathFinder, Loader):
else:
raise ValueError('unable to find base hash url for alias %s' % repr(arg))

# fix up the full name:
fullname = 'cas.v1_f_%s' % hashurl.path[1:]

ms = ModuleSpec(fullname, self, is_package=False,
loader_state=(hashurl,))

@@ -532,39 +538,63 @@ class Test(unittest.TestCase):
],
})

def test_aliasmulti(self):
# setup the cache
cachedir = self.tempdir / 'cache'
cachedir.mkdir()

with CASFinder() as f, \
tempattrset(sys.modules[__name__],
'load_mod_aliases', f.load_mod_aliases):
f.register(FileDirCAS(cachedir))

# and that hello.py is in the cache
shutil.copy(self.fixtures / 'hello.py',
cachedir)

# and that the aliases are loaded
with open(self.fixtures / 'randpkg' / 'cas_aliases.txt') as fp:
f.load_aliases(fp.read())

# that when we load the alias first
from cas.v1_a_hello import hello as hello_alias

# and then load the same module via hash
from cas.v1_f_330884aa2febb5e19fb7194ec6a69ed11dd3d77122f1a5175ee93e73cf0161c3 import hello as hello_hash

# they are the same
self.assertIs(hello_alias, hello_hash)

def test_aliasimports(self):
# setup the cache
temphome = self.tempdir / 'home'
temphome.mkdir()
cachedir = temphome / '.casimport' / 'cache'
cachedir.mkdir(parents=True)
cachedir = self.tempdir / 'cache'
cachedir.mkdir()

# add the test module's path
fixdir = str(self.fixtures)
sys.path.append(fixdir)

with tempset(os.environ, 'HOME', str(temphome)):
try:
with CASFinder() as f, \
tempattrset(sys.modules[__name__],
'load_mod_aliases', f.load_mod_aliases):
f.register(FileDirCAS(cachedir))
try:
with CASFinder() as f, \
tempattrset(sys.modules[__name__],
'load_mod_aliases', f.load_mod_aliases):
f.register(FileDirCAS(cachedir))

# and that hello.py is in the cache
shutil.copy(self.fixtures / 'hello.py',
cachedir)
# and that hello.py is in the cache
shutil.copy(self.fixtures / 'hello.py',
cachedir)

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

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

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

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

def test_aliasipfsimports(self):
# add the test module's path
@@ -679,6 +709,7 @@ class Test(unittest.TestCase):
with self.assertRaises(RuntimeError):
httpsldr.fetch_data(hashurl)

@unittest.skip('todo')
def test_overlappingaliases(self):
# make sure that an aliases file is consistent and does not
# override other urls. That is that any hashes are
@@ -690,6 +721,7 @@ class Test(unittest.TestCase):
# this case, BOTH hashse have to be checked.
pass

@unittest.skip('todo')
def test_loaderpriority(self):
# XXX - write test to allow you to specify the priority of
# a loader, to ensure that cache stays at top.


Loading…
取消
儲存