"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "Cheetah/ImportManager.py" between
cheetah3-3.1.0.tar.gz and cheetah3-3.2.0.tar.gz

About: Cheetah3 is a template engine and code generation tool for e.g. for Web development or Java, SQL, LaTeX, form email ... (written in Python).

ImportManager.py  (cheetah3-3.1.0):ImportManager.py  (cheetah3-3.2.0)
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 sys
import imp import imp
import marshal import marshal
import py_compile
import sys
from Cheetah.compat import string_type from Cheetah.compat import string_type
_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 79 skipping to change at line 80
def join(a, b, sep=sep): def join(a, b, sep=sep):
if a == '': if a == '':
return b return b
lastchar = a[-1:] lastchar = a[-1:]
if lastchar == '/' or lastchar == sep: if lastchar == '/' or lastchar == sep:
return a + b return a + b
return a + sep + b return a + sep + b
if dirname is None: if dirname is None:
def dirname(a, sep=sep): def dirname(a, sep=sep):
for i in range(len(a)-1, -1, -1): for i in range(len(a) - 1, -1, -1):
c = a[i] c = a[i]
if c == '/' or c == sep: if c == '/' or c == sep:
return a[:i] return a[:i]
return '' return ''
global _os_stat global _os_stat
_os_stat = stat _os_stat = stat
global _os_path_join global _os_path_join
_os_path_join = join _os_path_join = join
global _os_path_dirname global _os_path_dirname
_os_path_dirname = dirname _os_path_dirname = dirname
global _os_getcwd global _os_getcwd
_os_getcwd = getcwd _os_getcwd = getcwd
_os_bootstrap() _os_bootstrap()
def packageName(s): def packageName(s):
for i in range(len(s)-1, -1, -1): for i in range(len(s) - 1, -1, -1):
if s[i] == '.': if s[i] == '.':
break break
else: else:
return '' return ''
return s[:i] return s[:i]
def nameSplit(s): def nameSplit(s):
rslt = [] rslt = []
i = j = 0 i = j = 0
for j in range(len(s)): for j in range(len(s)):
if s[j] == '.': if s[j] == '.':
rslt.append(s[i:j]) rslt.append(s[i:j])
i = j+1 i = j + 1
if i < len(s): if i < len(s):
rslt.append(s[i:]) rslt.append(s[i:])
return rslt return rslt
def getPathExt(fnm): def getPathExt(fnm):
for i in range(len(fnm)-1, -1, -1): for i in range(len(fnm) - 1, -1, -1):
if fnm[i] == '.': if fnm[i] == '.':
return fnm[i:] return fnm[i:]
return '' return ''
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
skipping to change at line 180 skipping to change at line 181
loadco=marshal.loads, newmod=imp.new_module): loadco=marshal.loads, newmod=imp.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 == imp.C_EXTENSION:
fp = open(attempt, 'rb') with open(attempt, 'rb') as fp:
mod = imp.load_module( mod = imp.load_module(
nm, fp, attempt, (ext, mode, typ)) nm, fp, attempt, (ext, mode, typ))
mod.__file__ = attempt mod.__file__ = attempt
return mod return mod
elif typ == imp.PY_SOURCE: 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:
co = compile(open(py[0], 'r').read()+'\n', py[0], 'exec') with open(py[0], 'r') as py_code_file:
py_code = py_code_file.read()
co = compile(py_code + '\n', py[0], 'exec')
try:
py_compile.compile(py[0])
except IOError:
pass
__file__ = py[0]
break break
except SyntaxError as e: except SyntaxError as e:
print("Invalid syntax in %s" % py[0]) print("Invalid syntax in %s" % py[0])
print(e.args) print(e.args)
raise raise
elif pyc: elif pyc:
stuff = open(pyc[0], 'rb').read() with open(pyc[0], 'rb') as pyc_file:
stuff = pyc_file.read()
try: try:
co = loadco(stuff[8:]) co = loadco(stuff[8:])
__file__ = pyc[0]
break break
except (ValueError, EOFError): except (ValueError, EOFError):
pyc = None pyc = None
else: else:
return None return None
mod = newmod(nm) mod = newmod(nm)
mod.__file__ = co.co_filename mod.__file__ = __file__
if ispkg: if ispkg:
mod.__path__ = [pkgpth] mod.__path__ = [pkgpth]
subimporter = PathImportDirector(mod.__path__) subimporter = PathImportDirector(mod.__path__)
mod.__importsub__ = subimporter.getmod mod.__importsub__ = subimporter.getmod
mod.__co__ = co mod.__co__ = co
return mod return mod
class ImportDirector(Owner): class ImportDirector(Owner):
"""ImportDirectors live on the metapath There's one for builtins, one for """ImportDirectors live on the metapath There's one for builtins, one for
frozen modules, and one for sys.path Windows gets one for modules gotten frozen modules, and one for sys.path Windows gets one for modules gotten
skipping to change at line 259 skipping to change at line 269
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): isFrozen=imp.is_frozen, loadMod=imp.load_module):
if isFrozen(nm): if isFrozen(nm):
mod = loadMod(nm, None, nm, ('', '', imp.PY_FROZEN)) mod = loadMod(nm, None, nm, ('', '', imp.PY_FROZEN))
if hasattr(mod, '__path__'): if hasattr(mod, '__path__'):
mod.__importsub__ = \ mod.__importsub__ = \
lambda name, pname=nm, owner=self: \ lambda name, pname=nm, owner=self: \
owner.getmod(pname+'.'+name) owner.getmod(pname + '.' + name)
return mod return mod
return None return None
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:
skipping to change at line 303 skipping to change at line 313
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, desc = stuff
fp = open(fnm, 'rb') with open(fnm, 'rb') as fp:
mod = imp.load_module(nm, fp, fnm, desc) mod = imp.load_module(nm, fp, fnm, desc)
mod.__file__ = fnm mod.__file__ = fnm
return mod 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
skipping to change at line 453 skipping to change at line 463
if i: if i:
break break
if i < len(nmparts): if i < len(nmparts):
if ctx and hasattr(sys.modules[ctx], nmparts[i]): if ctx and hasattr(sys.modules[ctx], nmparts[i]):
return sys.modules[nmparts[0]] return sys.modules[nmparts[0]]
del sys.modules[fqname] del sys.modules[fqname]
raise ImportError("No module named %s" % fqname) raise ImportError("No module named %s" % fqname)
if fromlist is None: if fromlist is None:
if context: if context:
return sys.modules[context+'.'+nmparts[0]] return sys.modules[context + '.' + nmparts[0]]
return sys.modules[nmparts[0]] return sys.modules[nmparts[0]]
bottommod = sys.modules[ctx] bottommod = sys.modules[ctx]
if hasattr(bottommod, '__path__'): if hasattr(bottommod, '__path__'):
fromlist = list(fromlist) fromlist = list(fromlist)
i = 0 i = 0
while i < len(fromlist): while i < len(fromlist):
nm = fromlist[i] nm = fromlist[i]
if nm == '*': if nm == '*':
fromlist[i:i+1] = list(getattr(bottommod, '__all__', [])) fromlist[i:i+1] = list(getattr(bottommod, '__all__', [])) # noqa: E226,E501 missing whitespace around operator
if i >= len(fromlist): if i >= len(fromlist):
break break
nm = fromlist[i] nm = fromlist[i]
i = i + 1 i = i + 1
if not hasattr(bottommod, nm): if not hasattr(bottommod, nm):
if self.threaded: if self.threaded:
self._acquire() self._acquire()
mod = self.doimport(nm, ctx, ctx+'.'+nm) mod = self.doimport(nm, ctx, ctx + '.' + nm)
if self.threaded: if self.threaded:
self._release() self._release()
if not mod: if not mod:
raise ImportError("%s not found in %s" % (nm, ctx)) raise ImportError("%s not found in %s" % (nm, ctx))
return bottommod return bottommod
def doimport(self, nm, parentnm, fqname): def doimport(self, nm, parentnm, fqname):
# Not that nm is NEVER a dotted name at this point # Not that nm is NEVER a dotted name at this point
if parentnm: if parentnm:
parent = sys.modules[parentnm] parent = sys.modules[parentnm]
 End of changes. 17 change blocks. 
22 lines changed or deleted 32 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)