Named tuples make everything much more readable.

This commit is contained in:
Elf M. Sternberg 2016-07-08 21:31:27 -07:00
parent 9fac406305
commit 1cc09df2ff
1 changed files with 13 additions and 9 deletions

View File

@ -5,12 +5,16 @@ import sys
import imp import imp
import types import types
import pkgutil import pkgutil
from collections import namedtuple
SEP = os.sep SEP = os.sep
EXS = os.extsep EXS = os.extsep
FLS = [('%s' + SEP + '__init__' + EXS + '%s', True), FLS = [('%s' + SEP + '__init__' + EXS + '%s', True),
('%s' + EXS + '%s', False)] ('%s' + EXS + '%s', False)]
Loader = namedtuple('Loader', 'suffix compiler')
class PolyLoader(): class PolyLoader():
_loader_handlers = [] _loader_handlers = []
_installed = False _installed = False
@ -29,11 +33,11 @@ class PolyLoader():
if overlap: if overlap:
raise RuntimeError("Override of native Python extensions is not permitted.") raise RuntimeError("Override of native Python extensions is not permitted.")
overlap = suffixes.intersection( overlap = suffixes.intersection(
set([suffix for (_, suffix) in cls._loader_handlers])) set([loader.suffix for loader in cls._loader_handlers]))
if overlap: if overlap:
# Fail silently # Fail silently
return return
cls._loader_handlers += [(compiler, suf) for suf in suffixes] cls._loader_handlers += [Loader(suf, compiler) for suf in suffixes]
def load_module(self, fullname): def load_module(self, fullname):
if fullname in sys.modules: if fullname in sys.modules:
@ -42,17 +46,17 @@ class PolyLoader():
if fullname != self.fullname: if fullname != self.fullname:
raise ImportError("Load confusion: %s vs %s." % (fullname, self.fullname)) raise ImportError("Load confusion: %s vs %s." % (fullname, self.fullname))
matches = [(compiler, suffix) for (compiler, suffix) in self._loader_handlers matches = [loader for loader in self._loader_handlers
if self.path.endswith(suffix)] if self.path.endswith(loader.suffix)]
if len(matches) == 0: if len(matches) == 0:
raise ImportError("%s is not a recognized module?" % fullname) raise ImportError("%s is not a recognized module?" % fullname)
if len(matches) > 1: if len(matches) > 1:
raise ImportError("Multiple possible resolutions for %s: %s" % ( raise ImportError("Multiple possible resolutions for %s: %s" % (
fullname, ', '.join([suffix for (compiler, suffix) in matches]))) fullname, ', '.join([loader.suffix for loader in matches])))
compiler = matches[0][0] compiler = matches[0].compiler
with io.FileIO(self.path, 'r') as file: with io.FileIO(self.path, 'r') as file:
source_text = file.read() source_text = file.read()
@ -90,8 +94,8 @@ class PolyFinder(object):
path = os.path.realpath(self.path) path = os.path.realpath(self.path)
for (fp, ispkg) in FLS: for (fp, ispkg) in FLS:
for (compiler, suffix) in PolyLoader._loader_handlers: for loader in PolyLoader._loader_handlers:
composed_path = fp % (('%s' + SEP + '%s') % (path, subname), suffix) composed_path = fp % (('%s' + SEP + '%s') % (path, subname), loader.suffix)
if os.path.isdir(composed_path): if os.path.isdir(composed_path):
raise IOError("Invalid: Directory name ends in recognized suffix") raise IOError("Invalid: Directory name ends in recognized suffix")
if os.path.isfile(composed_path): if os.path.isfile(composed_path):
@ -111,7 +115,7 @@ class PolyFinder(object):
def getmodulename(path): def getmodulename(path):
filename = os.path.basename(path) filename = os.path.basename(path)
suffixes = ([(-len(suf[0]), suf[0]) for suf in imp.get_suffixes()] + suffixes = ([(-len(suf[0]), suf[0]) for suf in imp.get_suffixes()] +
[(-len(suf[1]), suf[1]) for suf in PolyLoader._loader_handlers]) [(-len(suf[0]), suf[0]) for suf in PolyLoader._loader_handlers])
suffixes.sort() suffixes.sort()
for neglen, suffix in suffixes: for neglen, suffix in suffixes:
if filename[neglen:] == suffix: if filename[neglen:] == suffix: