summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2010-04-18 21:54:20 +0000
committerChris McDonough <chrism@agendaless.com>2010-04-18 21:54:20 +0000
commite6fa66bd2c18632ef0608c63fb024b01931a5272 (patch)
tree6c1df278eb35dc9dff725b62c2387183d63ba5f5
parentc91eff7524189db9152260ff5c8db1c40672ad55 (diff)
downloadpyramid-e6fa66bd2c18632ef0608c63fb024b01931a5272.tar.gz
pyramid-e6fa66bd2c18632ef0608c63fb024b01931a5272.tar.bz2
pyramid-e6fa66bd2c18632ef0608c63fb024b01931a5272.zip
- Use "Venusian" (`http://docs.repoze.org/venusian
<http://docs.repoze.org/venusian>`) to perform ``bfg_view`` decorator scanning rather than relying on a BFG-internal decorator scanner. (Truth be told, Venusian is really just a generalization of the BFG-internal decorator scanner). - A new install-time dependency on the ``venusian`` distribution was added. - Remove ``repoze.bfg.compat.pkgutil_26.py`` and import alias ``repoze.bfg.compat.walk_packages``. These were only required by internal scanning machinery; Venusian replaced the internal scanning machinery, so these are no longer required.
-rw-r--r--CHANGES.txt17
-rw-r--r--docs/api/configuration.rst2
-rw-r--r--docs/glossary.rst7
-rw-r--r--repoze/bfg/compat/__init__.py5
-rw-r--r--repoze/bfg/compat/pkgutil_26.py585
-rw-r--r--repoze/bfg/configuration.py41
-rw-r--r--repoze/bfg/tests/test_integration.py3
-rw-r--r--repoze/bfg/tests/test_view.py137
-rw-r--r--repoze/bfg/tests/test_zcml.py3
-rw-r--r--repoze/bfg/view.py37
-rw-r--r--repoze/bfg/zcml.py2
-rw-r--r--setup.py1
12 files changed, 159 insertions, 681 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 640c1e120..7561f224f 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -51,6 +51,12 @@ Features
This feature was kindly contributed by Andrey Popp.
+- Use "Venusian" (`http://docs.repoze.org/venusian
+ <http://docs.repoze.org/venusian>`) to perform ``bfg_view``
+ decorator scanning rather than relying on a BFG-internal decorator
+ scanner. (Truth be told, Venusian is really just a generalization
+ of the BFG-internal decorator scanner).
+
Deprecations
------------
@@ -61,6 +67,12 @@ Deprecations
the foreseeable future, but they are deprecated in the
documentation.
+Dependencies
+------------
+
+- A new install-time dependency on the ``venusian`` distribution was
+ added.
+
Internal
--------
@@ -73,6 +85,11 @@ Internal
they weren't APIs and they became vestigial with the addition of
exception views.
+- Remove ``repoze.bfg.compat.pkgutil_26.py`` and import alias
+ ``repoze.bfg.compat.walk_packages``. These were only required by
+ internal scanning machinery; Venusian replaced the internal scanning
+ machinery, so these are no longer required.
+
Documentation
-------------
diff --git a/docs/api/configuration.rst b/docs/api/configuration.rst
index eeea605b2..e872a91b9 100644
--- a/docs/api/configuration.rst
+++ b/docs/api/configuration.rst
@@ -40,7 +40,7 @@
.. automethod:: override_resource(to_override, override_with)
- .. automethod:: scan(package)
+ .. automethod:: scan(package=None, categories=None)
.. automethod:: set_forbidden_view(view=None, attr=None, renderer=None, wrapper=None)
diff --git a/docs/glossary.rst b/docs/glossary.rst
index 2915ff5f3..14bd4fc44 100644
--- a/docs/glossary.rst
+++ b/docs/glossary.rst
@@ -660,3 +660,10 @@ Glossary
"GAE") is a Python application hosting service offered by Google.
:mod:`repoze.bfg` runs on GAE.
+ Venusian
+ `Venusian <http://docs.repoze.org/venusian>`_ is a library which
+ allows framework authors to defer decorator actions. Instead of
+ taking actions when a function (or class) decorator is executed
+ at import time, the action usually taken by the decorator is
+ deferred until a separate "scan" phase. :mod:`repoze.bfg` relies
+ on Venusian to provide a basis for its :term:`scan` feature.
diff --git a/repoze/bfg/compat/__init__.py b/repoze/bfg/compat/__init__.py
index b3585eac2..205175132 100644
--- a/repoze/bfg/compat/__init__.py
+++ b/repoze/bfg/compat/__init__.py
@@ -136,11 +136,6 @@ except ImportError: # pragma: no cover
import simplejson as json
try:
- from pkgutil import walk_packages
-except ImportError: #pragma: no cover
- from pkgutil_26 import walk_packages
-
-try:
from hashlib import md5
except ImportError: # pragma: no cover
import md5
diff --git a/repoze/bfg/compat/pkgutil_26.py b/repoze/bfg/compat/pkgutil_26.py
deleted file mode 100644
index cf9d3b722..000000000
--- a/repoze/bfg/compat/pkgutil_26.py
+++ /dev/null
@@ -1,585 +0,0 @@
-"""Utilities to support packages."""
-
-# NOTE: This module must remain compatible with Python 2.3, as it is shared
-# by setuptools for distribution with Python 2.3 and up.
-
-if 1: # pragma: no cover
-
- import os
- import sys
- import imp
- import os.path
- from types import ModuleType
-
- __all__ = [
- 'get_importer', 'iter_importers', 'get_loader', 'find_loader',
- 'walk_packages', 'iter_modules',
- 'ImpImporter', 'ImpLoader', 'read_code', 'extend_path',
- ]
-
- def read_code(stream):
- # This helper is needed in order for the PEP 302 emulation to
- # correctly handle compiled files
- import marshal
-
- magic = stream.read(4)
- if magic != imp.get_magic():
- return None
-
- stream.read(4) # Skip timestamp
- return marshal.load(stream)
-
-
- def simplegeneric(func):
- """Make a trivial single-dispatch generic function"""
- registry = {}
- def wrapper(*args, **kw):
- ob = args[0]
- try:
- cls = ob.__class__
- except AttributeError:
- cls = type(ob)
- try:
- mro = cls.__mro__
- except AttributeError:
- try:
- class cls(cls, object):
- pass
- mro = cls.__mro__[1:]
- except TypeError:
- mro = object, # must be an ExtensionClass or some such :(
- for t in mro:
- if t in registry:
- return registry[t](*args, **kw)
- else:
- return func(*args, **kw)
- try:
- wrapper.__name__ = func.__name__
- except (TypeError, AttributeError):
- pass # Python 2.3 doesn't allow functions to be renamed
-
- def register(typ, func=None):
- if func is None:
- return lambda f: register(typ, f)
- registry[typ] = func
- return func
-
- wrapper.__dict__ = func.__dict__
- wrapper.__doc__ = func.__doc__
- wrapper.register = register
- return wrapper
-
-
- def walk_packages(path=None, prefix='', onerror=None):
- """Yields (module_loader, name, ispkg) for all modules recursively
- on path, or, if path is None, all accessible modules.
-
- 'path' should be either None or a list of paths to look for
- modules in.
-
- 'prefix' is a string to output on the front of every module name
- on output.
-
- Note that this function must import all *packages* (NOT all
- modules!) on the given path, in order to access the __path__
- attribute to find submodules.
-
- 'onerror' is a function which gets called with one argument (the
- name of the package which was being imported) if any exception
- occurs while trying to import a package. If no onerror function is
- supplied, ImportErrors are caught and ignored, while all other
- exceptions are propagated, terminating the search.
-
- Examples:
-
- # list all modules python can access
- walk_packages()
-
- # list all submodules of ctypes
- walk_packages(ctypes.__path__, ctypes.__name__+'.')
- """
-
- def seen(p, m={}):
- if p in m:
- return True
- m[p] = True
-
- for importer, name, ispkg in iter_modules(path, prefix):
- yield importer, name, ispkg
-
- if ispkg:
- try:
- __import__(name)
- except ImportError:
- if onerror is not None:
- onerror(name)
- except Exception:
- if onerror is not None:
- onerror(name)
- else:
- raise
- else:
- path = getattr(sys.modules[name], '__path__', None) or []
-
- # don't traverse path items we've seen before
- path = [p for p in path if not seen(p)]
-
- for item in walk_packages(path, name+'.', onerror):
- yield item
-
-
- def iter_modules(path=None, prefix=''):
- """Yields (module_loader, name, ispkg) for all submodules on path,
- or, if path is None, all top-level modules on sys.path.
-
- 'path' should be either None or a list of paths to look for
- modules in.
-
- 'prefix' is a string to output on the front of every module name
- on output.
- """
-
- if path is None:
- importers = iter_importers()
- else:
- importers = map(get_importer, path)
-
- yielded = {}
- for i in importers:
- for name, ispkg in iter_importer_modules(i, prefix):
- if name not in yielded:
- yielded[name] = 1
- yield i, name, ispkg
-
-
- #@simplegeneric
- def iter_importer_modules(importer, prefix=''):
- if not hasattr(importer, 'iter_modules'):
- return []
- return importer.iter_modules(prefix)
-
- iter_importer_modules = simplegeneric(iter_importer_modules)
-
-
- class ImpImporter:
- """PEP 302 Importer that wraps Python's "classic" import algorithm
-
- ImpImporter(dirname) produces a PEP 302 importer that searches that
- directory. ImpImporter(None) produces a PEP 302 importer that searches
- the current sys.path, plus any modules that are frozen or built-in.
-
- Note that ImpImporter does not currently support being used by placement
- on sys.meta_path.
- """
-
- def __init__(self, path=None):
- self.path = path
-
- def find_module(self, fullname, path=None):
- # Note: we ignore 'path' argument since it is only used via meta_path
- subname = fullname.split(".")[-1]
- if subname != fullname and self.path is None:
- return None
- if self.path is None:
- path = None
- else:
- path = [os.path.realpath(self.path)]
- try:
- file, filename, etc = imp.find_module(subname, path)
- except ImportError:
- return None
- return ImpLoader(fullname, file, filename, etc)
-
- def iter_modules(self, prefix=''):
- if self.path is None or not os.path.isdir(self.path):
- return
-
- yielded = {}
- import inspect
-
- filenames = os.listdir(self.path)
- filenames.sort() # handle packages before same-named modules
-
- for fn in filenames:
- modname = inspect.getmodulename(fn)
- if modname=='__init__' or modname in yielded:
- continue
-
- path = os.path.join(self.path, fn)
- ispkg = False
-
- if not modname and os.path.isdir(path) and '.' not in fn:
- modname = fn
- for fn in os.listdir(path):
- subname = inspect.getmodulename(fn)
- if subname=='__init__':
- ispkg = True
- break
- else:
- continue # not a package
-
- if modname and '.' not in modname:
- yielded[modname] = 1
- yield prefix + modname, ispkg
-
-
- class ImpLoader:
- """PEP 302 Loader that wraps Python's "classic" import algorithm
- """
- code = source = None
-
- def __init__(self, fullname, file, filename, etc):
- self.file = file
- self.filename = filename
- self.fullname = fullname
- self.etc = etc
-
- def load_module(self, fullname):
- self._reopen()
- try:
- mod = imp.load_module(fullname, self.file, self.filename, self.etc)
- finally:
- if self.file:
- self.file.close()
- # Note: we don't set __loader__ because we want the module to look
- # normal; i.e. this is just a wrapper for standard import machinery
- return mod
-
- def get_data(self, pathname):
- return open(pathname, "rb").read()
-
- def _reopen(self):
- if self.file and self.file.closed:
- mod_type = self.etc[2]
- if mod_type==imp.PY_SOURCE:
- self.file = open(self.filename, 'rU')
- elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION):
- self.file = open(self.filename, 'rb')
-
- def _fix_name(self, fullname):
- if fullname is None:
- fullname = self.fullname
- elif fullname != self.fullname:
- raise ImportError("Loader for module %s cannot handle "
- "module %s" % (self.fullname, fullname))
- return fullname
-
- def is_package(self, fullname):
- fullname = self._fix_name(fullname)
- return self.etc[2]==imp.PKG_DIRECTORY
-
- def get_code(self, fullname=None):
- fullname = self._fix_name(fullname)
- if self.code is None:
- mod_type = self.etc[2]
- if mod_type==imp.PY_SOURCE:
- source = self.get_source(fullname)
- self.code = compile(source, self.filename, 'exec')
- elif mod_type==imp.PY_COMPILED:
- self._reopen()
- try:
- self.code = read_code(self.file)
- finally:
- self.file.close()
- elif mod_type==imp.PKG_DIRECTORY:
- self.code = self._get_delegate().get_code()
- return self.code
-
- def get_source(self, fullname=None):
- fullname = self._fix_name(fullname)
- if self.source is None:
- mod_type = self.etc[2]
- if mod_type==imp.PY_SOURCE:
- self._reopen()
- try:
- self.source = self.file.read()
- finally:
- self.file.close()
- elif mod_type==imp.PY_COMPILED:
- if os.path.exists(self.filename[:-1]):
- f = open(self.filename[:-1], 'rU')
- self.source = f.read()
- f.close()
- elif mod_type==imp.PKG_DIRECTORY:
- self.source = self._get_delegate().get_source()
- return self.source
-
-
- def _get_delegate(self):
- return ImpImporter(self.filename).find_module('__init__')
-
- def get_filename(self, fullname=None):
- fullname = self._fix_name(fullname)
- mod_type = self.etc[2]
- if self.etc[2]==imp.PKG_DIRECTORY:
- return self._get_delegate().get_filename()
- elif self.etc[2] in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION):
- return self.filename
- return None
-
-
- try:
- import zipimport
- from zipimport import zipimporter
-
- def iter_zipimport_modules(importer, prefix=''):
- dirlist = zipimport._zip_directory_cache[importer.archive].keys()
- dirlist.sort()
- _prefix = importer.prefix
- plen = len(_prefix)
- yielded = {}
- import inspect
- for fn in dirlist:
- if not fn.startswith(_prefix):
- continue
-
- fn = fn[plen:].split(os.sep)
-
- if len(fn)==2 and fn[1].startswith('__init__.py'):
- if fn[0] not in yielded:
- yielded[fn[0]] = 1
- yield fn[0], True
-
- if len(fn)!=1:
- continue
-
- modname = inspect.getmodulename(fn[0])
- if modname=='__init__':
- continue
-
- if modname and '.' not in modname and modname not in yielded:
- yielded[modname] = 1
- yield prefix + modname, False
-
- iter_importer_modules.register(zipimporter, iter_zipimport_modules)
-
- except ImportError:
- pass
-
-
- def get_importer(path_item):
- """Retrieve a PEP 302 importer for the given path item
-
- The returned importer is cached in sys.path_importer_cache
- if it was newly created by a path hook.
-
- If there is no importer, a wrapper around the basic import
- machinery is returned. This wrapper is never inserted into
- the importer cache (None is inserted instead).
-
- The cache (or part of it) can be cleared manually if a
- rescan of sys.path_hooks is necessary.
- """
- try:
- importer = sys.path_importer_cache[path_item]
- except KeyError:
- for path_hook in sys.path_hooks:
- try:
- importer = path_hook(path_item)
- break
- except ImportError:
- pass
- else:
- importer = None
- sys.path_importer_cache.setdefault(path_item, importer)
-
- if importer is None:
- try:
- importer = ImpImporter(path_item)
- except ImportError:
- importer = None
- return importer
-
-
- def iter_importers(fullname=""):
- """Yield PEP 302 importers for the given module name
-
- If fullname contains a '.', the importers will be for the package
- containing fullname, otherwise they will be importers for sys.meta_path,
- sys.path, and Python's "classic" import machinery, in that order. If
- the named module is in a package, that package is imported as a side
- effect of invoking this function.
-
- Non PEP 302 mechanisms (e.g. the Windows registry) used by the
- standard import machinery to find files in alternative locations
- are partially supported, but are searched AFTER sys.path. Normally,
- these locations are searched BEFORE sys.path, preventing sys.path
- entries from shadowing them.
-
- For this to cause a visible difference in behaviour, there must
- be a module or package name that is accessible via both sys.path
- and one of the non PEP 302 file system mechanisms. In this case,
- the emulation will find the former version, while the builtin
- import mechanism will find the latter.
-
- Items of the following types can be affected by this discrepancy:
- imp.C_EXTENSION, imp.PY_SOURCE, imp.PY_COMPILED, imp.PKG_DIRECTORY
- """
- if fullname.startswith('.'):
- raise ImportError("Relative module names not supported")
- if '.' in fullname:
- # Get the containing package's __path__
- pkg = '.'.join(fullname.split('.')[:-1])
- if pkg not in sys.modules:
- __import__(pkg)
- path = getattr(sys.modules[pkg], '__path__', None) or []
- else:
- for importer in sys.meta_path:
- yield importer
- path = sys.path
- for item in path:
- yield get_importer(item)
- if '.' not in fullname:
- yield ImpImporter()
-
- def get_loader(module_or_name):
- """Get a PEP 302 "loader" object for module_or_name
-
- If the module or package is accessible via the normal import
- mechanism, a wrapper around the relevant part of that machinery
- is returned. Returns None if the module cannot be found or imported.
- If the named module is not already imported, its containing package
- (if any) is imported, in order to establish the package __path__.
-
- This function uses iter_importers(), and is thus subject to the same
- limitations regarding platform-specific special import locations such
- as the Windows registry.
- """
- if module_or_name in sys.modules:
- module_or_name = sys.modules[module_or_name]
- if isinstance(module_or_name, ModuleType):
- module = module_or_name
- loader = getattr(module, '__loader__', None)
- if loader is not None:
- return loader
- fullname = module.__name__
- else:
- fullname = module_or_name
- return find_loader(fullname)
-
- def find_loader(fullname):
- """Find a PEP 302 "loader" object for fullname
-
- If fullname contains dots, path must be the containing package's __path__.
- Returns None if the module cannot be found or imported. This function uses
- iter_importers(), and is thus subject to the same limitations regarding
- platform-specific special import locations such as the Windows registry.
- """
- for importer in iter_importers(fullname):
- loader = importer.find_module(fullname)
- if loader is not None:
- return loader
-
- return None
-
-
- def extend_path(path, name):
- """Extend a package's path.
-
- Intended use is to place the following code in a package's __init__.py:
-
- from pkgutil import extend_path
- __path__ = extend_path(__path__, __name__)
-
- This will add to the package's __path__ all subdirectories of
- directories on sys.path named after the package. This is useful
- if one wants to distribute different parts of a single logical
- package as multiple directories.
-
- It also looks for *.pkg files beginning where * matches the name
- argument. This feature is similar to *.pth files (see site.py),
- except that it doesn't special-case lines starting with 'import'.
- A *.pkg file is trusted at face value: apart from checking for
- duplicates, all entries found in a *.pkg file are added to the
- path, regardless of whether they are exist the filesystem. (This
- is a feature.)
-
- If the input path is not a list (as is the case for frozen
- packages) it is returned unchanged. The input path is not
- modified; an extended copy is returned. Items are only appended
- to the copy at the end.
-
- It is assumed that sys.path is a sequence. Items of sys.path that
- are not (unicode or 8-bit) strings referring to existing
- directories are ignored. Unicode items of sys.path that cause
- errors when used as filenames may cause this function to raise an
- exception (in line with os.path.isdir() behavior).
- """
-
- if not isinstance(path, list):
- # This could happen e.g. when this is called from inside a
- # frozen package. Return the path unchanged in that case.
- return path
-
- pname = os.path.join(*name.split('.')) # Reconstitute as relative path
- # Just in case os.extsep != '.'
- sname = os.extsep.join(name.split('.'))
- sname_pkg = sname + os.extsep + "pkg"
- init_py = "__init__" + os.extsep + "py"
-
- path = path[:] # Start with a copy of the existing path
-
- for dir in sys.path:
- if not isinstance(dir, basestring) or not os.path.isdir(dir):
- continue
- subdir = os.path.join(dir, pname)
- # XXX This may still add duplicate entries to path on
- # case-insensitive filesystems
- initfile = os.path.join(subdir, init_py)
- if subdir not in path and os.path.isfile(initfile):
- path.append(subdir)
- # XXX Is this the right thing for subpackages like zope.app?
- # It looks for a file named "zope.app.pkg"
- pkgfile = os.path.join(dir, sname_pkg)
- if os.path.isfile(pkgfile):
- try:
- f = open(pkgfile)
- except IOError, msg:
- sys.stderr.write("Can't open %s: %s\n" %
- (pkgfile, msg))
- else:
- for line in f:
- line = line.rstrip('\n')
- if not line or line.startswith('#'):
- continue
- path.append(line) # Don't check for existence!
- f.close()
-
- return path
-
- def get_data(package, resource):
- """Get a resource from a package.
-
- This is a wrapper round the PEP 302 loader get_data API. The package
- argument should be the name of a package, in standard module format
- (foo.bar). The resource argument should be in the form of a relative
- filename, using '/' as the path separator. The parent directory name '..'
- is not allowed, and nor is a rooted name (starting with a '/').
-
- The function returns a binary string, which is the contents of the
- specified resource.
-
- For packages located in the filesystem, which have already been imported,
- this is the rough equivalent of
-
- d = os.path.dirname(sys.modules[package].__file__)
- data = open(os.path.join(d, resource), 'rb').read()
-
- If the package cannot be located or loaded, or it uses a PEP 302 loader
- which does not support get_data(), then None is returned.
- """
-
- loader = get_loader(package)
- if loader is None or not hasattr(loader, 'get_data'):
- return None
- mod = sys.modules.get(package) or loader.load_module(package)
- if mod is None or not hasattr(mod, '__file__'):
- return None
-
- # Modify the resource name to be compatible with the loader.get_data
- # signature - an os.path format "filename" starting with the dirname of
- # the package's __file__
- parts = resource.split('/')
- parts.insert(0, os.path.dirname(mod.__file__))
- resource_name = os.path.join(*parts)
- return loader.get_data(resource_name)
diff --git a/repoze/bfg/configuration.py b/repoze/bfg/configuration.py
index 3d1c0214d..4673479da 100644
--- a/repoze/bfg/configuration.py
+++ b/repoze/bfg/configuration.py
@@ -6,6 +6,7 @@ import types
import inspect
from webob import Response
+import venusian
from zope.configuration import xmlconfig
@@ -40,7 +41,6 @@ from repoze.bfg import renderers
from repoze.bfg.authorization import ACLAuthorizationPolicy
from repoze.bfg.compat import all
from repoze.bfg.compat import md5
-from repoze.bfg.compat import walk_packages
from repoze.bfg.events import WSGIApplicationCreatedEvent
from repoze.bfg.exceptions import Forbidden
from repoze.bfg.exceptions import NotFound
@@ -138,6 +138,7 @@ class Configurator(object):
log warnings and authorization debugging information. """
manager = manager # for testing injection
+ venusian = venusian # for testing injection
def __init__(self, registry=None, package=None, settings=None,
root_factory=None, authentication_policy=None,
authorization_policy=None, renderers=DEFAULT_RENDERERS,
@@ -1103,7 +1104,7 @@ class Configurator(object):
self.registry.registerUtility(mapper, IRoutesMapper)
mapper.connect(path, name, factory, predicates=predicates)
- def scan(self, package=None, _info=u''):
+ def scan(self, package=None, categories=None, _info=u''):
""" Scan a Python package and any of its subpackages for
objects marked with :term:`configuration decoration` such as
:class:`repoze.bfg.view.bfg_view`. Any decorated object found
@@ -1112,27 +1113,29 @@ class Configurator(object):
The ``package`` argument should be a reference to a Python
:term:`package` or module object. If ``package`` is ``None``,
the package of the *caller* is used.
+
+ The ``categories`` argument, if provided, should be the
+ :term:`Venusian` 'scan categories' to use during scanning.
+ Providing this argument is not often necessary; specifying
+ scan categories is an extremely advanced usage.
+
+ By default, ``categories`` is ``None`` which will execute
+ *all* Venusian decorator callbacks including
+ :mod:`repoze.bfg`-related decorators such as ``bfg_view``. If
+ this is not desirable because the codebase has other
+ Venusian-using decorators that aren't meant to be invoked
+ during a particular scan, use ``('bfg',)`` as a ``categories``
+ value to limit the execution of decorator callbacks to only
+ those registered by :mod:`repoze.bfg` itself. Or pass a
+ sequence of Venusian scan categories as necessary
+ (e.g. ``('bfg', 'myframework')``) to limit the decorators
+ called to the set of categories required.
"""
if package is None: # pragma: no cover
package = caller_package()
- def register_decorations(name, ob):
- settings = getattr(ob, '__bfg_view_settings__', None)
- if settings is not None:
- for setting in settings:
- self.add_view(view=ob, _info=_info, **setting)
-
- for name, ob in inspect.getmembers(package):
- register_decorations(name, ob)
-
- if hasattr(package, '__path__'): # package, not module
- results = walk_packages(package.__path__, package.__name__+'.')
-
- for importer, modname, ispkg in results:
- __import__(modname)
- module = sys.modules[modname]
- for name, ob in inspect.getmembers(module, None):
- register_decorations(name, ob)
+ scanner = self.venusian.Scanner(config=self)
+ scanner.scan(package, categories=categories)
def add_renderer(self, name, factory, _info=u''):
""" Add a :mod:`repoze.bfg` :term:`renderer` factory to the current
diff --git a/repoze/bfg/tests/test_integration.py b/repoze/bfg/tests/test_integration.py
index c54509378..b6eb860ee 100644
--- a/repoze/bfg/tests/test_integration.py
+++ b/repoze/bfg/tests/test_integration.py
@@ -20,8 +20,9 @@ def wsgiapptest(environ, start_response):
class WGSIAppPlusBFGViewTests(unittest.TestCase):
def test_it(self):
+ from venusian import ATTACH_ATTR
import types
- self.failUnless(wsgiapptest.__bfg_view_settings__)
+ self.failUnless(getattr(wsgiapptest, ATTACH_ATTR))
self.failUnless(type(wsgiapptest) is types.FunctionType)
context = DummyContext()
request = DummyRequest()
diff --git a/repoze/bfg/tests/test_view.py b/repoze/bfg/tests/test_view.py
index bcfa45e91..76a218cf6 100644
--- a/repoze/bfg/tests/test_view.py
+++ b/repoze/bfg/tests/test_view.py
@@ -1,4 +1,5 @@
import unittest
+import sys
from repoze.bfg.testing import cleanUp
@@ -302,100 +303,109 @@ class TestBFGViewDecorator(unittest.TestCase):
def test_call_function(self):
decorator = self._makeOne()
- def foo():
- """ docstring """
+ venusian = DummyVenusian()
+ decorator.venusian = venusian
+ def foo(): pass
wrapped = decorator(foo)
self.failUnless(wrapped is foo)
- settings = wrapped.__bfg_view_settings__[0]
- self.assertEqual(settings['permission'], None)
- self.assertEqual(settings['context'], None)
- self.assertEqual(settings['request_type'], None)
+ settings = call_venusian(venusian)
+ self.assertEqual(len(settings), 1)
+ self.assertEqual(settings[0]['permission'], None)
+ self.assertEqual(settings[0]['context'], None)
+ self.assertEqual(settings[0]['request_type'], None)
- def test_call_oldstyle_class(self):
+ def test_call_class(self):
decorator = self._makeOne()
- class foo:
- """ docstring """
+ venusian = DummyVenusian()
+ decorator.venusian = venusian
+ decorator.venusian.info.scope = 'class'
+ class foo(object): pass
wrapped = decorator(foo)
self.failUnless(wrapped is foo)
- settings = wrapped.__bfg_view_settings__[0]
- self.assertEqual(settings['permission'], None)
- self.assertEqual(settings['context'], None)
- self.assertEqual(settings['request_type'], None)
-
- def test_call_newstyle_class(self):
- decorator = self._makeOne()
- class foo(object):
- """ docstring """
- wrapped = decorator(foo)
- self.failUnless(wrapped is foo)
- settings = wrapped.__bfg_view_settings__[0]
- self.assertEqual(settings['permission'], None)
- self.assertEqual(settings['context'], None)
- self.assertEqual(settings['request_type'], None)
+ settings = call_venusian(venusian)
+ self.assertEqual(len(settings), 1)
+ self.assertEqual(settings[0]['permission'], None)
+ self.assertEqual(settings[0]['context'], None)
+ self.assertEqual(settings[0]['request_type'], None)
def test_stacking(self):
decorator1 = self._makeOne(name='1')
+ venusian1 = DummyVenusian()
+ decorator1.venusian = venusian1
+ venusian2 = DummyVenusian()
decorator2 = self._makeOne(name='2')
- def foo():
- """ docstring """
+ decorator2.venusian = venusian2
+ def foo(): pass
wrapped1 = decorator1(foo)
wrapped2 = decorator2(wrapped1)
self.failUnless(wrapped1 is foo)
self.failUnless(wrapped2 is foo)
- self.assertEqual(len(foo.__bfg_view_settings__), 2)
- settings1 = foo.__bfg_view_settings__[0]
- self.assertEqual(settings1['name'], '1')
- settings2 = foo.__bfg_view_settings__[1]
- self.assertEqual(settings2['name'], '2')
+ settings1 = call_venusian(venusian1)
+ self.assertEqual(len(settings1), 1)
+ self.assertEqual(settings1[0]['name'], '1')
+ settings2 = call_venusian(venusian2)
+ self.assertEqual(len(settings2), 1)
+ self.assertEqual(settings2[0]['name'], '2')
def test_call_as_method(self):
decorator = self._makeOne()
+ venusian = DummyVenusian()
+ decorator.venusian = venusian
+ decorator.venusian.info.scope = 'class'
def foo(self): pass
def bar(self): pass
class foo(object):
- """ docstring """
foomethod = decorator(foo)
barmethod = decorator(bar)
- settings = foo.__bfg_view_settings__
+ settings = call_venusian(venusian)
self.assertEqual(len(settings), 2)
self.assertEqual(settings[0]['attr'], 'foo')
self.assertEqual(settings[1]['attr'], 'bar')
def test_with_custom_predicates(self):
decorator = self._makeOne(custom_predicates=(1,))
- def foo(context, request): return 'OK'
+ venusian = DummyVenusian()
+ decorator.venusian = venusian
+ def foo(context, request): pass
decorated = decorator(foo)
- settings = decorated.__bfg_view_settings__
+ self.failUnless(decorated is foo)
+ settings = call_venusian(venusian)
self.assertEqual(settings[0]['custom_predicates'], (1,))
def test_call_with_renderer_nodot(self):
decorator = self._makeOne(renderer='json')
- def foo():
- """ docstring """
+ venusian = DummyVenusian()
+ decorator.venusian = venusian
+ def foo(): pass
wrapped = decorator(foo)
self.failUnless(wrapped is foo)
- settings = wrapped.__bfg_view_settings__[0]
- self.assertEqual(settings['renderer'], 'json')
+ settings = call_venusian(venusian)
+ self.assertEqual(len(settings), 1)
+ self.assertEqual(settings[0]['renderer'], 'json')
def test_call_with_renderer_relpath(self):
decorator = self._makeOne(renderer='fixtures/minimal.pt')
- def foo():
- """ docstring """
+ venusian = DummyVenusian()
+ decorator.venusian = venusian
+ def foo(): pass
wrapped = decorator(foo)
self.failUnless(wrapped is foo)
- settings = wrapped.__bfg_view_settings__[0]
- self.assertEqual(settings['renderer'],
+ settings = call_venusian(venusian)
+ self.assertEqual(len(settings), 1)
+ self.assertEqual(settings[0]['renderer'],
'repoze.bfg.tests:fixtures/minimal.pt')
def test_call_with_renderer_pkgpath(self):
decorator = self._makeOne(
renderer='repoze.bfg.tests:fixtures/minimal.pt')
- def foo():
- """ docstring """
+ venusian = DummyVenusian()
+ decorator.venusian = venusian
+ def foo(): pass
wrapped = decorator(foo)
self.failUnless(wrapped is foo)
- settings = wrapped.__bfg_view_settings__[0]
- self.assertEqual(settings['renderer'],
+ settings = call_venusian(venusian)
+ self.assertEqual(len(settings), 1)
+ self.assertEqual(settings[0]['renderer'],
'repoze.bfg.tests:fixtures/minimal.pt')
class TestDefaultForbiddenView(BaseTest, unittest.TestCase):
@@ -521,3 +531,36 @@ class DummyResponse:
from zope.interface import Interface
class IContext(Interface):
pass
+
+class DummyVenusianInfo(object):
+ scope = 'notaclass'
+ module = sys.modules['repoze.bfg.tests']
+
+class DummyVenusian(object):
+ def __init__(self, info=None):
+ if info is None:
+ info = DummyVenusianInfo()
+ self.info = info
+ self.attachments = []
+
+ def attach(self, wrapped, callback, category=None):
+ self.attachments.append((wrapped, callback, category))
+ return self.info
+
+class DummyConfig(object):
+ def __init__(self):
+ self.settings = []
+
+ def add_view(self, **kw):
+ self.settings.append(kw)
+
+class DummyVenusianContext(object):
+ def __init__(self):
+ self.config = DummyConfig()
+
+def call_venusian(venusian):
+ context = DummyVenusianContext()
+ for wrapped, callback, category in venusian.attachments:
+ callback(context, None, None)
+ return context.config.settings
+
diff --git a/repoze/bfg/tests/test_zcml.py b/repoze/bfg/tests/test_zcml.py
index 452769de8..7f42e9f09 100644
--- a/repoze/bfg/tests/test_zcml.py
+++ b/repoze/bfg/tests/test_zcml.py
@@ -837,8 +837,7 @@ class TestZCMLScanDirective(unittest.TestCase):
action = actions[0]
self.assertEqual(action['callable'].im_func, Configurator.scan.im_func)
self.assertEqual(action['discriminator'], None)
- self.assertEqual(action['args'], (dummy_module, None))
-
+ self.assertEqual(action['args'], (dummy_module, None, None))
class TestAdapterDirective(unittest.TestCase):
def setUp(self):
diff --git a/repoze/bfg/view.py b/repoze/bfg/view.py
index 1f83faa5b..2ccc75f5c 100644
--- a/repoze/bfg/view.py
+++ b/repoze/bfg/view.py
@@ -1,7 +1,6 @@
import cgi
import mimetypes
import os
-import sys
# See http://bugs.python.org/issue5853 which is a recursion bug
# that seems to effect Python 2.6, Python 2.6.1, and 2.6.2 (a fix
@@ -16,11 +15,12 @@ if hasattr(mimetypes, 'init'):
from webob import Response
from webob.exc import HTTPFound
+import venusian
+
from paste.urlparser import StaticURLParser
from zope.deprecation import deprecated
from zope.interface import providedBy
-from zope.interface.advice import getFrameInfo
from repoze.bfg.interfaces import IResponseFactory
from repoze.bfg.interfaces import IRoutesMapper
@@ -457,6 +457,7 @@ class bfg_view(object):
config.scan()
"""
+ venusian = venusian # for testing injection
def __init__(self, name='', request_type=None, for_=None, permission=None,
route_name=None, request_method=None, request_param=None,
containment=None, attr=None, renderer=None, wrapper=None,
@@ -480,34 +481,30 @@ class bfg_view(object):
self.custom_predicates = custom_predicates
def __call__(self, wrapped):
- setting = self.__dict__.copy()
- frame = sys._getframe(1)
- scope, module, f_locals, f_globals = getFrameInfo(frame)
- if scope == 'class':
- # we're in the midst of a class statement; the setdefault
- # below actually adds a __bfg_view_settings__ attr to the
- # class __dict__ if one does not already exist
- settings = f_locals.setdefault('__bfg_view_settings__', [])
- if setting['attr'] is None:
- setting['attr'] = wrapped.__name__
- else:
- settings = getattr(wrapped, '__bfg_view_settings__', [])
- wrapped.__bfg_view_settings__ = settings
+ settings = self.__dict__.copy()
+
+ def callback(context, name, ob):
+ context.config.add_view(view=ob, **settings)
+
+ info = self.venusian.attach(wrapped, callback, category='bfg')
+
+ if info.scope == 'class':
+ if settings['attr'] is None:
+ settings['attr'] = wrapped.__name__
# try to convert the renderer provided into a fully qualified
# resource specification
- abspath = setting.get('renderer')
+ abspath = settings.get('renderer')
if abspath is not None and '.' in abspath:
isabs = os.path.isabs(abspath)
if not (':' in abspath and not isabs):
# not already a resource spec
if not isabs:
- pp = package_path(module)
+ pp = package_path(info.module)
abspath = os.path.join(pp, abspath)
- resource = resource_spec_from_abspath(abspath, module)
- setting['renderer'] = resource
+ resource = resource_spec_from_abspath(abspath, info.module)
+ settings['renderer'] = resource
- settings.append(setting)
return wrapped
def default_view(context, request, status):
diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py
index 3935303c5..7ba82bfe7 100644
--- a/repoze/bfg/zcml.py
+++ b/repoze/bfg/zcml.py
@@ -582,7 +582,7 @@ def scan(_context, package):
_context.action(
discriminator=None,
callable=config.scan,
- args=(package, _context.info)
+ args=(package, None, _context.info)
)
class IAdapterDirective(Interface):
diff --git a/setup.py b/setup.py
index 3d03b96b3..3dbed2c2a 100644
--- a/setup.py
+++ b/setup.py
@@ -38,6 +38,7 @@ install_requires=[
'zope.configuration',
'zope.deprecation',
'zope.interface >= 3.5.1', # 3.5.0 comment: "allow to bootstrap on jython"
+ 'venusian >= 0.2',
]
if sys.version_info[:2] < (2, 6):