ImportManager.py (cheetah3-3.2.0) | : | ImportManager.py (cheetah3-3.2.1) | ||
---|---|---|---|---|
skipping to change at line 20 | skipping to change at line 20 | |||
- made it a little less terse | - made it a little less terse | |||
- added docstrings and explanatations | - added docstrings and explanatations | |||
- standardized the variable naming scheme | - standardized the variable naming scheme | |||
- reorganized the code layout to enhance readability | - reorganized the code layout to enhance readability | |||
""" | """ | |||
import imp | ||||
import marshal | import marshal | |||
import py_compile | import py_compile | |||
import sys | import sys | |||
from Cheetah.compat import string_type | from Cheetah.compat import PY2, string_type, new_module, get_suffixes, \ | |||
load_module_from_file | ||||
if PY2: | ||||
import imp | ||||
else: | ||||
import importlib.machinery | ||||
_installed = False | _installed = False | |||
# _globalOwnerTypes is defined at the bottom of this file | # _globalOwnerTypes is defined at the bottom of this file | |||
_os_stat = _os_path_join = _os_getcwd = _os_path_dirname = None | _os_stat = _os_path_join = _os_getcwd = _os_path_dirname = None | |||
################################################## | ################################################## | |||
# FUNCTIONS | # FUNCTIONS | |||
skipping to change at line 135 | skipping to change at line 139 | |||
def pathIsDir(pathname): | def pathIsDir(pathname): | |||
"Local replacement for os.path.isdir()." | "Local replacement for os.path.isdir()." | |||
try: | try: | |||
s = _os_stat(pathname) | s = _os_stat(pathname) | |||
except OSError: | except OSError: | |||
return None | return None | |||
return (s[0] & 0o170000) == 0o040000 | return (s[0] & 0o170000) == 0o040000 | |||
def getDescr(fnm): | def getDescr(fnm): | |||
ext = getPathExt(fnm) | ext = getPathExt(fnm) | |||
for (suffix, mode, typ) in imp.get_suffixes(): | for (suffix, mode, typ) in get_suffixes(): | |||
if suffix == ext: | if suffix == ext: | |||
return (suffix, mode, typ) | return (suffix, mode, typ) | |||
################################################## | ################################################## | |||
# CLASSES | # CLASSES | |||
class Owner: | class Owner: | |||
"""An Owner does imports from a particular piece of turf That is, there's | """An Owner does imports from a particular piece of turf That is, there's | |||
an Owner for each thing on sys.path There are owners for directories and | an Owner for each thing on sys.path There are owners for directories and | |||
skipping to change at line 170 | skipping to change at line 174 | |||
class DirOwner(Owner): | class DirOwner(Owner): | |||
def __init__(self, path): | def __init__(self, path): | |||
if path == '': | if path == '': | |||
path = _os_getcwd() | path = _os_getcwd() | |||
if not pathIsDir(path): | if not pathIsDir(path): | |||
raise ValueError("%s is not a directory" % path) | raise ValueError("%s is not a directory" % path) | |||
Owner.__init__(self, path) | Owner.__init__(self, path) | |||
def getmod(self, nm, getsuffixes=imp.get_suffixes, | def getmod(self, nm, getsuffixes=get_suffixes, | |||
loadco=marshal.loads, newmod=imp.new_module): | loadco=marshal.loads, newmod=new_module): | |||
pth = _os_path_join(self.path, nm) | pth = _os_path_join(self.path, nm) | |||
possibles = [(pth, 0, None)] | possibles = [(pth, 0, None)] | |||
if pathIsDir(pth): | if pathIsDir(pth): | |||
possibles.insert(0, (_os_path_join(pth, '__init__'), 1, pth)) | possibles.insert(0, (_os_path_join(pth, '__init__'), 1, pth)) | |||
py = pyc = None | py = pyc = None | |||
for pth, ispkg, pkgpth in possibles: | for pth, ispkg, pkgpth in possibles: | |||
for ext, mode, typ in getsuffixes(): | for ext, mode, typ in getsuffixes(): | |||
attempt = pth + ext | attempt = pth + ext | |||
try: | try: | |||
st = _os_stat(attempt) | st = _os_stat(attempt) | |||
except Exception: | except Exception: | |||
pass | pass | |||
else: | else: | |||
if typ == imp.C_EXTENSION: | if typ == 3: # imp.C_EXTENSION | |||
with open(attempt, 'rb') as fp: | return load_module_from_file(nm, nm, attempt) | |||
mod = imp.load_module( | elif typ == 1: # imp.PY_SOURCE | |||
nm, fp, attempt, (ext, mode, typ)) | ||||
mod.__file__ = attempt | ||||
return mod | ||||
elif typ == imp.PY_SOURCE: | ||||
py = (attempt, st) | py = (attempt, st) | |||
else: | else: | |||
pyc = (attempt, st) | pyc = (attempt, st) | |||
if py or pyc: | if py or pyc: | |||
break | break | |||
if py is None and pyc is None: | if py is None and pyc is None: | |||
return None | return None | |||
while True: | while True: | |||
if pyc is None or py and pyc[1][8] < py[1][8]: | if pyc is None or py and pyc[1][8] < py[1][8]: | |||
try: | try: | |||
skipping to change at line 250 | skipping to change at line 250 | |||
from the Registry Mac would have them for PY_RESOURCE modules etc. A | from the Registry Mac would have them for PY_RESOURCE modules etc. A | |||
generalization of Owner - their concept of 'turf' is broader""" | generalization of Owner - their concept of 'turf' is broader""" | |||
pass | pass | |||
class BuiltinImportDirector(ImportDirector): | class BuiltinImportDirector(ImportDirector): | |||
"""Directs imports of builtin modules""" | """Directs imports of builtin modules""" | |||
def __init__(self): | def __init__(self): | |||
self.path = 'Builtins' | self.path = 'Builtins' | |||
def getmod(self, nm, isbuiltin=imp.is_builtin): | def getmod(self, nm): | |||
if isbuiltin(nm): | if nm in sys.builtin_module_names: | |||
mod = imp.load_module(nm, None, nm, ('', '', imp.C_BUILTIN)) | return __import__(nm) | |||
return mod | ||||
return None | return None | |||
class FrozenImportDirector(ImportDirector): | class FrozenImportDirector(ImportDirector): | |||
"""Directs imports of frozen modules""" | """Directs imports of frozen modules""" | |||
def __init__(self): | def __init__(self): | |||
self.path = 'FrozenModules' | self.path = 'FrozenModules' | |||
def getmod(self, nm, | def getmod(self, nm): | |||
isFrozen=imp.is_frozen, loadMod=imp.load_module): | mod = None | |||
if isFrozen(nm): | if PY2: | |||
mod = loadMod(nm, None, nm, ('', '', imp.PY_FROZEN)) | if imp.is_frozen(nm): | |||
if hasattr(mod, '__path__'): | mod = imp.load_module(nm, None, nm, ('', '', imp.PY_FROZEN)) | |||
mod.__importsub__ = \ | else: | |||
lambda name, pname=nm, owner=self: \ | if importlib.machinery.FrozenImporter.find_spec(nm): | |||
owner.getmod(pname + '.' + name) | mod = importlib.machinery.FrozenImporter.load_module(nm) | |||
return mod | if mod and hasattr(mod, '__path__'): | |||
return None | mod.__importsub__ = \ | |||
lambda name, pname=nm, owner=self: \ | ||||
owner.getmod(pname + '.' + name) | ||||
return mod | ||||
class RegistryImportDirector(ImportDirector): | class RegistryImportDirector(ImportDirector): | |||
"""Directs imports of modules stored in the Windows Registry""" | """Directs imports of modules stored in the Windows Registry""" | |||
def __init__(self): | def __init__(self): | |||
self.path = "WindowsRegistry" | self.path = "WindowsRegistry" | |||
self.map = {} | self.map = {} | |||
try: | try: | |||
import win32api | import win32api | |||
# import win32con | # import win32con | |||
skipping to change at line 312 | skipping to change at line 314 | |||
val = win32api.RegQueryValueEx(hskey, '') | val = win32api.RegQueryValueEx(hskey, '') | |||
desc = getDescr(val[0]) | desc = getDescr(val[0]) | |||
self.map[subkeyname] = (val[0], desc) | self.map[subkeyname] = (val[0], desc) | |||
hskey.Close() | hskey.Close() | |||
hkey.Close() | hkey.Close() | |||
break | break | |||
def getmod(self, nm): | def getmod(self, nm): | |||
stuff = self.map.get(nm) | stuff = self.map.get(nm) | |||
if stuff: | if stuff: | |||
fnm, desc = stuff | fnm = stuff[0] | |||
with open(fnm, 'rb') as fp: | return load_module_from_file(nm, nm, fnm) | |||
mod = imp.load_module(nm, fp, fnm, desc) | ||||
mod.__file__ = fnm | ||||
return mod | ||||
return None | return None | |||
class PathImportDirector(ImportDirector): | class PathImportDirector(ImportDirector): | |||
"""Directs imports of modules stored on the filesystem.""" | """Directs imports of modules stored on the filesystem.""" | |||
def __init__(self, pathlist=None, importers=None, ownertypes=None): | def __init__(self, pathlist=None, importers=None, ownertypes=None): | |||
if pathlist is None: | if pathlist is None: | |||
self.path = sys.path | self.path = sys.path | |||
else: | else: | |||
self.path = pathlist | self.path = pathlist | |||
End of changes. 8 change blocks. | ||||
31 lines changed or deleted | 30 lines changed or added |