Commit 6dbe74b5 authored by Tres Seaver's avatar Tres Seaver

Cleanup, PEP8, sane variable names.

parent 693a53df
...@@ -10,44 +10,53 @@ ...@@ -10,44 +10,53 @@
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE
# #
############################################################################## ##############################################################################
__doc__='''Standard routines for handling extensions. """Standard routines for handling extensions.
Extensions currently include external methods and pluggable brains. Extensions currently include external methods and pluggable brains.
"""
import imp
import os
$Id$'''
__version__='$Revision: 1.23 $'[11:-2]
import os, imp
import Products import Products
from zExceptions import NotFound from zExceptions import NotFound
path_split=os.path.split
path_join=os.path.join
exists=os.path.exists
class FuncCode: class FuncCode:
def __init__(self, f, im=0): def __init__(self, f, im=0):
self.co_varnames=f.func_code.co_varnames[im:] self.co_varnames = f.func_code.co_varnames[im:]
self.co_argcount=f.func_code.co_argcount-im self.co_argcount = f.func_code.co_argcount - im
def __cmp__(self,other): def __cmp__(self, other):
if other is None: return 1 if other is None:
try: return cmp((self.co_argcount, self.co_varnames), return 1
try:
return cmp((self.co_argcount, self.co_varnames),
(other.co_argcount, other.co_varnames)) (other.co_argcount, other.co_varnames))
except: return 1 except:
return 1
def _getPath(home, prefix, name, suffixes): def _getPath(home, prefix, name, suffixes):
d=path_join(home, prefix)
if d==prefix: raise ValueError, ( dir = os.path.join(home, prefix)
'The prefix, %s, should be a relative path' % prefix) if dir == prefix:
d=path_join(d,name) raise ValueError('The prefix, %s, should be a relative path' % prefix)
if d==name: raise ValueError, ( # Paranoia
'The file name, %s, should be a simple file name' % name) fn = os.path.join(dir, name)
for s in suffixes: if fn == name:
if s: s="%s.%s" % (d, s) # Paranoia
else: s=d raise ValueError('The file name, %s, should be a simple file name'
if exists(s): return s % name)
for suffix in suffixes:
if suffix:
fqn = "%s.%s" % (fn, suffix)
else:
fqn = fn
if os.path.exists(fqn):
return fqn
def getPath(prefix, name, checkProduct=1, suffixes=('',), cfg=None): def getPath(prefix, name, checkProduct=1, suffixes=('',), cfg=None):
"""Find a file in one of several relative locations """Find a file in one of several relative locations
...@@ -76,69 +85,69 @@ def getPath(prefix, name, checkProduct=1, suffixes=('',), cfg=None): ...@@ -76,69 +85,69 @@ def getPath(prefix, name, checkProduct=1, suffixes=('',), cfg=None):
the directory containing the directory containing the software the directory containing the directory containing the software
home, and possibly product areas. home, and possibly product areas.
""" """
d,n = path_split(name) dir, ignored = os.path.split(name)
if d: raise ValueError, ( if dir:
'The file name, %s, should be a simple file name' % name) raise ValueError('The file name, %s, should be a simple file name'
% name)
result = None result = None
if checkProduct: if checkProduct:
l = name.find('.') dot = name.find('.')
if l > 0: if dot > 0:
p = name[:l] product = name[:dot]
n = name[l + 1:] extname = name[dot + 1:]
for product_dir in Products.__path__: for product_dir in Products.__path__:
r = _getPath(product_dir, os.path.join(p, prefix), n, suffixes) found = _getPath(product_dir, os.path.join(product, prefix),
if r is not None: result = r extname, suffixes)
if found is not None:
return found
if result is None:
if cfg is None: if cfg is None:
import App.config import App.config
cfg = App.config.getConfiguration() cfg = App.config.getConfiguration()
locations = []
locations.append(cfg.instancehome) locations = [cfg.instancehome]
sw = getattr(cfg, 'softwarehome', None)
if sw is not None: softwarehome = getattr(cfg, 'softwarehome', None)
sw = os.path.dirname(sw) if softwarehome is not None:
locations.append(sw) zopehome = os.path.dirname(softwarehome)
locations.append(zopehome)
for home in locations: for home in locations:
r=_getPath(home, prefix, name, suffixes) found = _getPath(home, prefix, name, suffixes)
if r is not None: if found is not None:
result = r return found
del locations
if result is None:
try: try:
l = name.rfind('.') dot = name.rfind('.')
if l > 0: if dot > 0:
realName = name[l + 1:] realName = name[dot+1:]
toplevel = name[:l] toplevel = name[:dot]
pos = toplevel.rfind('.') rdot = toplevel.rfind('.')
if pos > -1: if rdot > -1:
m = __import__(toplevel, globals(), {}, toplevel[pos+1:]) module = __import__(toplevel, globals(), {}, toplevel[rdot+1:])
else: else:
m = __import__(toplevel) module = __import__(toplevel)
d = os.path.join(m.__path__[0], prefix, realName) prefix = os.path.join(module.__path__[0], prefix, realName)
for s in suffixes: for suffix in suffixes:
if s: s="%s.%s" % (d, s) if suffix:
else: s=d fn = "%s.%s" % (prefix, suffix)
if os.path.exists(s): else:
result = s fn = prefix
break if os.path.exists(fn):
return fn
except: except:
pass pass
return result
def getObject(module, name, reload=0, def getObject(module, name, reload=0,
# The use of a mutable default is intentional here, # The use of a mutable default is intentional here,
# because modules is a module cache. # because modules is a module cache.
modules={} modules={}
): ):
# The use of modules here is not thread safe, however, there is # The use of modules here is not thread safe, however, there is
# no real harm in a race condition here. If two threads # no real harm in a race condition here. If two threads
# update the cache, then one will have simply worked a little # update the cache, then one will have simply worked a little
...@@ -151,56 +160,60 @@ def getObject(module, name, reload=0, ...@@ -151,56 +160,60 @@ def getObject(module, name, reload=0,
base, ext = os.path.splitext(module) base, ext = os.path.splitext(module)
if ext in ('py', 'pyc'): if ext in ('py', 'pyc'):
# XXX should never happen; splitext() keeps '.' with the extension # XXX should never happen; splitext() keeps '.' with the extension
p = base prefix = base
else: else:
p = module prefix = module
p=getPath('Extensions', p, suffixes=('','py','pyc'))
if p is None:
raise NotFound, (
"The specified module, <em>%s</em>, couldn't be found." % module)
__traceback_info__=p, module path = getPath('Extensions', prefix, suffixes=('','py','pyc'))
if path is None:
raise NotFound("The specified module, '%s', couldn't be found."
% module)
__traceback_info__= path, module
base, ext = os.path.splitext(p) base, ext = os.path.splitext(path)
if ext=='.pyc': if ext=='.pyc':
file = open(p, 'rb') file = open(path, 'rb')
binmod=imp.load_compiled('Extension', p, file) binmod = imp.load_compiled('Extension', path, file)
file.close() file.close()
m=binmod.__dict__ module_dict = binmod.__dict__
else: else:
try: execsrc=open(p) try:
except: raise NotFound, ( execsrc = open(path)
"The specified module, <em>%s</em>, couldn't be opened." except:
% module) raise NotFound("The specified module, '%s', "
m={} "couldn't be opened." % module)
exec execsrc in m module_dict = {}
exec execsrc in module_dict
if old is not None: if old is not None:
old.update(m) # XXX Accretive??
old.update(module_dict)
else: else:
modules[module] = m modules[module] = module_dict
try: try:
return m[name] return module_dict[name]
except KeyError: except KeyError:
raise NotFound, ( raise NotFound("The specified object, '%s', was not found "
"The specified object, <em>%s</em>, was not found in module, " "in module, '%s'." % (name, module))
"<em>%s</em>." % (name, module))
class NoBrains: pass class NoBrains:
pass
def getBrain(module, class_name, reload=0):
'Check/load a class'
if not module and not class_name: return NoBrains def getBrain(module, class_name, reload=0, modules=None):
""" Check/load a class from an extension.
"""
if not module and not class_name:
return NoBrains
try: c=getObject(module, class_name, reload) if modules is None:
except KeyError, v: c=getObject(module, class_name, reload)
if v == class_name: raise ValueError, ( else:
'The class, %s, is not defined in file, %s' % (class_name, module)) c=getObject(module, class_name, reload, modules=modules)
if not hasattr(c,'__bases__'): raise ValueError, ( if getattr(c, '__bases__', None) is None:
'%s, is not a class' % class_name) raise ValueError('%s, is not a class' % class_name)
return c return c
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment