Exemplo n.º 1
0
def attach(wrapped, callback, category=None, depth=1):
    """ Attach a callback to the wrapped object.  It will be found
    later during a scan.  This function returns an instance of the
    :class:`venusian.AttachInfo` class."""

    frame = sys._getframe(depth+1)
    scope, module, f_locals, f_globals, codeinfo = getFrameInfo(frame)
    module_name = getattr(module, '__name__', None)
    if scope == 'class':
        # we're in the midst of a class statement
        categories = f_locals.setdefault(ATTACH_ATTR, Categories(None))
        callbacks = categories.setdefault(category, [])
        callbacks.append((callback, module_name))
    else:
        categories = getattr(wrapped, ATTACH_ATTR, None)
        if categories is None or not categories.attached_to(wrapped):
            # if there aren't any attached categories, or we've retrieved
            # some by inheritance, we need to create new ones
            categories = Categories(wrapped)
            setattr(wrapped, ATTACH_ATTR, categories)
        callbacks = categories.setdefault(category, [])
        callbacks.append((callback, module_name))
    return AttachInfo(
        scope=scope, module=module, locals=f_locals, globals=f_globals,
        category=category, codeinfo=codeinfo)
Exemplo n.º 2
0
def attach(wrapped, callback, category=None, depth=1):
    """ Attach a callback to the wrapped object.  It will be found
    later during a scan.  This function returns an instance of the
    :class:`venusian.AttachInfo` class."""

    frame = sys._getframe(depth + 1)
    scope, module, f_locals, f_globals, codeinfo = getFrameInfo(frame)
    if scope == 'class':
        # we're in the midst of a class statement
        categories = f_locals.setdefault(ATTACH_ATTR, Categories(None))
        callbacks = categories.setdefault(category, [])
        callbacks.append(callback)
    else:
        categories = getattr(wrapped, ATTACH_ATTR, None)
        if categories is None or not categories.attached_to(wrapped):
            # if there aren't any attached categories, or we've retrieved
            # some by inheritance, we need to create new ones
            categories = Categories(wrapped)
            setattr(wrapped, ATTACH_ATTR, categories)
        callbacks = categories.setdefault(category, [])
        callbacks.append(callback)
    return AttachInfo(scope=scope,
                      module=module,
                      locals=f_locals,
                      globals=f_globals,
                      category=category,
                      codeinfo=codeinfo)
Exemplo n.º 3
0
 def testCallInfo(self):
     (kind, module, f_locals, f_globals,
      codeinfo) = advice.getFrameInfo(sys._getframe())
     self.assertEqual(kind, "function call")
     self.assertTrue(f_locals is locals()) # ???
     for d in module.__dict__, f_globals:
         self.assertTrue(d is globals())
     self.assertEqual(len(codeinfo), 4)
Exemplo n.º 4
0
 def testCallInfo(self):
     (kind, module, f_locals, f_globals,
      codeinfo) = advice.getFrameInfo(sys._getframe())
     self.assertEqual(kind, "function call")
     self.assertTrue(f_locals is locals())  # ???
     for d in module.__dict__, f_globals:
         self.assertTrue(d is globals())
     self.assertEqual(len(codeinfo), 4)
Exemplo n.º 5
0
    def __call__(self, wrapped):
        # Shamefully stolen from venusian.lift

        if not isclass(wrapped):
            raise RuntimeError(
                '"view_overrides" only works as a class decorator; you tried to use it against %r'
                % wrapped)

        frame = sys._getframe(1)
        scope, module, f_locals, f_globals, codeinfo = getFrameInfo(frame)
        module_name = getattr(module, '__name__', None)
        newcategories = Categories(wrapped)
        newcategories.lifted = True

        # Did we find any view_config decorators from the parents of wrapped class
        found = False

        for cls in getmro(wrapped):

            attached_categories = cls.__dict__.get(ATTACH_ATTR, None)
            if attached_categories is None:
                attached_categories = cls.__dict__.get(LIFTONLY_ATTR, None)

            if attached_categories is not None:

                for cname, category in attached_categories.items():
                    if cls is not wrapped:
                        if self.categories and not cname in self.categories:
                            continue
                    callbacks = newcategories.get(cname, [])
                    newcallbacks = []

                    for cb, _, liftid, cscope in category:
                        append = True
                        toappend = (cb, module_name, liftid, cscope)

                        if cscope == 'class':
                            for ncb, _, nliftid, nscope in callbacks:
                                if (nscope == 'class' and liftid == nliftid):
                                    append = False
                        if append:
                            toappend, _found = _create_child_view_config_from_parent_cb(
                                *toappend, overrides=self.kwargs)
                            found = found or _found
                            newcallbacks.append(toappend)
                    newcategory = list(callbacks) + newcallbacks
                    newcategories[cname] = newcategory
                if attached_categories.lifted:
                    break
        if newcategories:  # if it has any keys
            setattr(wrapped, ATTACH_ATTR, newcategories)

        if not found:
            raise RuntimeError(
                '"view_overrides" could not find any @view_config decorators on the parent classes of %r'
                % wrapped)

        return wrapped
Exemplo n.º 6
0
    def __call__(self, wrapped):
        # Shamefully stolen from venusian.lift

        if not isclass(wrapped):
            raise RuntimeError('"view_overrides" only works as a class decorator; you tried to use it against %r' % wrapped)

        frame = sys._getframe(1)
        scope, module, f_locals, f_globals, codeinfo = getFrameInfo(frame)
        module_name = getattr(module, '__name__', None)
        newcategories = Categories(wrapped)
        newcategories.lifted = True

        # Did we find any view_config decorators from the parents of wrapped class
        found = False

        for cls in getmro(wrapped):

            attached_categories = cls.__dict__.get(ATTACH_ATTR, None)
            if attached_categories is None:
                attached_categories = cls.__dict__.get(LIFTONLY_ATTR, None)

            if attached_categories is not None:

                for cname, category in attached_categories.items():
                    if cls is not wrapped:
                        if self.categories and not cname in self.categories:
                            continue
                    callbacks = newcategories.get(cname, [])
                    newcallbacks = []

                    for cb, _, liftid, cscope in category:
                        append = True
                        toappend = (cb, module_name, liftid, cscope)

                        if cscope == 'class':
                            for ncb, _, nliftid, nscope in callbacks:
                                if (nscope == 'class' and liftid == nliftid):
                                    append = False
                        if append:
                            toappend, _found = _create_child_view_config_from_parent_cb(*toappend, overrides=self.kwargs)
                            found = found or _found
                            newcallbacks.append(toappend)
                    newcategory = list(callbacks) + newcallbacks
                    newcategories[cname] = newcategory
                if attached_categories.lifted:
                    break
        if newcategories: # if it has any keys
            setattr(wrapped, ATTACH_ATTR, newcategories)

        if not found:
            raise RuntimeError('"view_overrides" could not find any @view_config decorators on the parent classes of %r' % wrapped)

        return wrapped
Exemplo n.º 7
0
class FrameInfoTest(unittest.TestCase):

    classLevelFrameInfo = advice.getFrameInfo(sys._getframe())

    def testModuleInfo(self):
        kind, module, f_locals, f_globals, codeinfo = moduleLevelFrameInfo
        self.assertEqual(kind, "module")
        for d in module.__dict__, f_locals, f_globals:
            self.assertTrue(d is globals())
        self.assertEqual(len(codeinfo), 4)

    if not PY3:

        def testClassicClassInfo(self):
            (
                kind,
                module,
                f_locals,
                f_globals,
                codeinfo,
            ) = ClassicClass.classLevelFrameInfo
            self.assertEqual(kind, "class")

            self.assertTrue(f_locals is ClassicClass.__dict__)  # ???
            for d in module.__dict__, f_globals:
                self.assertTrue(d is globals())
            self.assertEqual(len(codeinfo), 4)

    def testNewStyleClassInfo(self):
        (
            kind,
            module,
            f_locals,
            f_globals,
            codeinfo,
        ) = NewStyleClass.classLevelFrameInfo
        self.assertEqual(kind, "class")

        for d in module.__dict__, f_globals:
            self.assertTrue(d is globals())
        self.assertEqual(len(codeinfo), 4)

    def testCallInfo(self):
        (kind, module, f_locals, f_globals, codeinfo) = advice.getFrameInfo(
            sys._getframe()
        )
        self.assertEqual(kind, "function call")
        self.assertTrue(f_locals is locals())  # ???
        for d in module.__dict__, f_globals:
            self.assertTrue(d is globals())
        self.assertEqual(len(codeinfo), 4)
Exemplo n.º 8
0
def attach(wrapped, callback, category=None, depth=1, name=None):
    """ Attach a callback to the wrapped object.  It will be found
    later during a scan.  This function returns an instance of the
    :class:`venusian.AttachInfo` class.

    ``category`` should be ``None`` or a string representing a decorator
    category name.

    ``name`` should be ``None`` or a string representing a subcategory within
    the category.  This will be used by the ``lift`` class decorator to
    determine if decorations of a method should be inherited or overridden.
    """

    frame = sys._getframe(depth+1)
    scope, module, f_locals, f_globals, codeinfo = getFrameInfo(frame)
    module_name = getattr(module, '__name__', None)
    wrapped_name = getattr(wrapped, '__name__', None)
    class_name = codeinfo[2]

    liftid = '%s %s' % (wrapped_name, name)
    
    if scope == 'class':
        # we're in the midst of a class statement
        categories = f_locals.get(ATTACH_ATTR, None)
        if categories is None or not categories.attached_to(
            module_name, class_name, None
            ):
            categories = Categories((module_name, class_name))
            f_locals[ATTACH_ATTR] = categories
        callbacks = categories.setdefault(category, [])
    else:
        categories = getattr(wrapped, ATTACH_ATTR, None)
        if categories is None or not categories.attached_to(
            module_name, class_name, wrapped
            ):
            # if there aren't any attached categories, or we've retrieved
            # some by inheritance, we need to create new ones
            categories = Categories(wrapped)
            setattr(wrapped, ATTACH_ATTR, categories)
        callbacks = categories.setdefault(category, [])

    callbacks.append((callback, module_name, liftid, scope))

    return AttachInfo(
        scope=scope,
        module=module,
        locals=f_locals,
        globals=f_globals,
        category=category,
        codeinfo=codeinfo,
        )
Exemplo n.º 9
0
def attach(wrapped, callback, category=None, depth=1, name=None):
    """ Attach a callback to the wrapped object.  It will be found
    later during a scan.  This function returns an instance of the
    :class:`venusian.AttachInfo` class.

    ``category`` should be ``None`` or a string representing a decorator
    category name.

    ``name`` should be ``None`` or a string representing a subcategory within
    the category.  This will be used by the ``lift`` class decorator to
    determine if decorations of a method should be inherited or overridden.
    """

    frame = sys._getframe(depth + 1)
    scope, module, f_locals, f_globals, codeinfo = getFrameInfo(frame)
    module_name = getattr(module, '__name__', None)
    wrapped_name = getattr(wrapped, '__name__', None)
    class_name = codeinfo[2]

    liftid = '%s %s' % (wrapped_name, name)

    if scope == 'class':
        # we're in the midst of a class statement
        categories = f_locals.get(ATTACH_ATTR, None)
        if categories is None or not categories.attached_to(
                module_name, class_name, None):
            categories = Categories((module_name, class_name))
            f_locals[ATTACH_ATTR] = categories
        callbacks = categories.setdefault(category, [])
    else:
        categories = getattr(wrapped, ATTACH_ATTR, None)
        if categories is None or not categories.attached_to(
                module_name, class_name, wrapped):
            # if there aren't any attached categories, or we've retrieved
            # some by inheritance, we need to create new ones
            categories = Categories(wrapped)
            setattr(wrapped, ATTACH_ATTR, categories)
        callbacks = categories.setdefault(category, [])

    callbacks.append((callback, module_name, liftid, scope))

    return AttachInfo(
        scope=scope,
        module=module,
        locals=f_locals,
        globals=f_globals,
        category=category,
        codeinfo=codeinfo,
    )
Exemplo n.º 10
0
    def __init__(self, depth=1, moduleLevel=False, allowed_scope=None):
        scope, module, f_locals, f_globals, codeinfo = \
            getFrameInfo(sys._getframe(depth + 1))

        if allowed_scope and scope not in allowed_scope: # pragma: no cover
            raise TypeError("This directive is not allowed "
                            "to run in this scope: %s" % scope)

        if scope == 'module':
            self.name = f_locals['__name__']
        else:
            self.name = codeinfo[2]

        self.locals = f_locals
        self.scope = scope
        self.module = module
        self.codeinfo = CodeInfo(
            codeinfo[0], codeinfo[1], codeinfo[2], codeinfo[3], module.__name__)

        if depth > 1:
            _, mod, _, _, ci = getFrameInfo(sys._getframe(2))
            self.hash = (module.__name__, codeinfo[1], mod.__name__, ci[1])
        else:
            self.hash = (module.__name__, codeinfo[1])
Exemplo n.º 11
0
    def __init__(self, depth=1, moduleLevel=False, allowed_scope=None):
        scope, module, f_locals, f_globals, codeinfo = \
            getFrameInfo(sys._getframe(depth + 1))

        if allowed_scope and scope not in allowed_scope:  # pragma: no cover
            raise TypeError("This directive is not allowed "
                            "to run in this scope: %s" % scope)

        if scope == 'module':
            self.name = f_locals['__name__']
        else:
            self.name = codeinfo[2]

        self.locals = f_locals
        self.scope = scope
        self.module = module
        self.codeinfo = CodeInfo(codeinfo[0], codeinfo[1], codeinfo[2],
                                 codeinfo[3], module.__name__)

        if depth > 1:
            _, mod, _, _, ci = getFrameInfo(sys._getframe(2))
            self.hash = (module.__name__, codeinfo[1], mod.__name__, ci[1])
        else:
            self.hash = (module.__name__, codeinfo[1])
Exemplo n.º 12
0
 def __call__(self, wrapped):
     if not isclass(wrapped):
         raise RuntimeError(
             '"lift" only works as a class decorator; you tried to use '
             'it against %r' % wrapped
             )
     frame = sys._getframe(1)
     scope, module, f_locals, f_globals, codeinfo = getFrameInfo(frame)
     module_name = getattr(module, '__name__', None)
     newcategories = Categories(wrapped)
     newcategories.lifted = True
     for cls in getmro(wrapped):
         attached_categories = cls.__dict__.get(ATTACH_ATTR, None)
         if attached_categories is None:
             attached_categories = cls.__dict__.get(LIFTONLY_ATTR, None)
         if attached_categories is not None:
             for cname, category in attached_categories.items():
                 if cls is not wrapped:
                     if self.categories and not cname in self.categories:
                         continue
                 callbacks = newcategories.get(cname, [])
                 newcallbacks = []
                 for cb, _, liftid, cscope in category:
                     append = True
                     toappend = (cb, module_name, liftid, cscope)
                     if cscope == 'class':
                         for ncb, _, nliftid, nscope in callbacks:
                             if (nscope == 'class' and liftid == nliftid):
                                 append = False
                     if append:
                         newcallbacks.append(toappend)
                 newcategory = list(callbacks) + newcallbacks
                 newcategories[cname] = newcategory
             if attached_categories.lifted:
                 break
     if newcategories: # if it has any keys
         setattr(wrapped, ATTACH_ATTR, newcategories)
     return wrapped
Exemplo n.º 13
0
 def __call__(self, wrapped):
     if not isclass(wrapped):
         raise RuntimeError(
             '"lift" only works as a class decorator; you tried to use '
             'it against %r' % wrapped)
     frame = sys._getframe(1)
     scope, module, f_locals, f_globals, codeinfo = getFrameInfo(frame)
     module_name = getattr(module, '__name__', None)
     newcategories = Categories(wrapped)
     newcategories.lifted = True
     for cls in getmro(wrapped):
         attached_categories = cls.__dict__.get(ATTACH_ATTR, None)
         if attached_categories is None:
             attached_categories = cls.__dict__.get(LIFTONLY_ATTR, None)
         if attached_categories is not None:
             for cname, category in attached_categories.items():
                 if cls is not wrapped:
                     if self.categories and not cname in self.categories:
                         continue
                 callbacks = newcategories.get(cname, [])
                 newcallbacks = []
                 for cb, _, liftid, cscope in category:
                     append = True
                     toappend = (cb, module_name, liftid, cscope)
                     if cscope == 'class':
                         for ncb, _, nliftid, nscope in callbacks:
                             if (nscope == 'class' and liftid == nliftid):
                                 append = False
                     if append:
                         newcallbacks.append(toappend)
                 newcategory = list(callbacks) + newcallbacks
                 newcategories[cname] = newcategory
             if attached_categories.lifted:
                 break
     if newcategories:  # if it has any keys
         setattr(wrapped, ATTACH_ATTR, newcategories)
     return wrapped
Exemplo n.º 14
0
def attach(wrapped, callback, category=None, depth=1):
    """ Attach a callback to the wrapped object.  It will be found
    later during a scan.  This function returns an instance of the
    :class:`venusian.AttachInfo` class."""

    frame = sys._getframe(depth+1)
    scope, module, f_locals, f_globals, codeinfo = getFrameInfo(frame)
    if scope == 'class':
        # we're in the midst of a class statement
        categories = f_locals.setdefault(ATTACH_ATTR, {})
        callbacks = categories.setdefault(category, [])
        callbacks.append(callback)
    else:
        if inspect.isclass(wrapped):
            # ignore any superclass attachments, these should not be inherited
            categories = wrapped.__dict__.get(ATTACH_ATTR, {})
        else:
            categories = getattr(wrapped, ATTACH_ATTR, {})
        callbacks = categories.setdefault(category, [])
        callbacks.append(callback)
        setattr(wrapped, ATTACH_ATTR, categories)
    return AttachInfo(
        scope=scope, module=module, locals=f_locals, globals=f_globals,
        category=category, codeinfo=codeinfo)
Exemplo n.º 15
0
class NewStyleClass(object):
    classLevelFrameInfo = advice.getFrameInfo(sys._getframe())
Exemplo n.º 16
0
 class ClassicClass:
     classLevelFrameInfo = advice.getFrameInfo(sys._getframe())
Exemplo n.º 17
0
import sys
from venusian import advice

PY3 = sys.version_info[0] >= 3

if not PY3:

    class ClassicClass:
        classLevelFrameInfo = advice.getFrameInfo(sys._getframe())


class NewStyleClass(object):
    classLevelFrameInfo = advice.getFrameInfo(sys._getframe())


moduleLevelFrameInfo = advice.getFrameInfo(sys._getframe())


class FrameInfoTest(unittest.TestCase):

    classLevelFrameInfo = advice.getFrameInfo(sys._getframe())

    def testModuleInfo(self):
        kind, module, f_locals, f_globals, codeinfo = moduleLevelFrameInfo
        self.assertEqual(kind, "module")
        for d in module.__dict__, f_locals, f_globals:
            self.assertTrue(d is globals())
        self.assertEqual(len(codeinfo), 4)

    if not PY3:
Exemplo n.º 18
0
"""

import unittest
import sys
from venusian import advice

PY3 = sys.version_info[0] >= 3

if not PY3:
    class ClassicClass:
        classLevelFrameInfo = advice.getFrameInfo(sys._getframe())

class NewStyleClass(object):
    classLevelFrameInfo = advice.getFrameInfo(sys._getframe())

moduleLevelFrameInfo = advice.getFrameInfo(sys._getframe())

class FrameInfoTest(unittest.TestCase):

    classLevelFrameInfo = advice.getFrameInfo(sys._getframe())

    def testModuleInfo(self):
        kind, module, f_locals, f_globals, codeinfo = moduleLevelFrameInfo
        self.assertEqual(kind, "module")
        for d in module.__dict__, f_locals, f_globals:
            self.assertTrue(d is globals())
        self.assertEqual(len(codeinfo), 4)

    if not PY3:
        def testClassicClassInfo(self):
            (kind, module, f_locals, f_globals,