本文整理汇总了Python中zope.interface.advice.addClassAdvisor函数的典型用法代码示例。如果您正苦于以下问题:Python addClassAdvisor函数的具体用法?Python addClassAdvisor怎么用?Python addClassAdvisor使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了addClassAdvisor函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, field_name, widget, *args, **kwargs):
self.field_name = field_name
if widget is None:
self.widget = None
else:
self.widget = CustomWidgetFactory(widget, *args, **kwargs)
addClassAdvisor(self.advise)
示例2: strictly_implements
def strictly_implements(*interfaces):
frame = sys._getframe(1)
f_locals = frame.f_locals
# Try to make sure we were called from a class def. Assumes Python > 2.2.
if f_locals is frame.f_globals or '__module__' not in f_locals:
raise TypeError("implements can be used only from a class definition.")
if '__implements_advice_data__' in f_locals:
raise TypeError("implements can be used only once in a class definition.")
def _implements_advice(cls):
interfaces, classImplements = cls.__dict__['__implements_advice_data__']
del cls.__implements_advice_data__
classImplements(cls, *interfaces)
if interesting_modules.match(cls.__module__):
if not excluded_classnames.match(cls.__name__):
for interface in interfaces:
try:
verifyClass(interface, cls)
except Exception as e:
print("%s.%s does not correctly implement %s.%s:\n%s"
% (cls.__module__, cls.__name__,
interface.__module__, interface.__name__, e), file=_err)
else:
_other_modules_with_violations.add(cls.__module__)
return cls
f_locals['__implements_advice_data__'] = interfaces, zi.classImplements
addClassAdvisor(_implements_advice, depth=2)
示例3: ping
def ping(log, value):
def pong(klass):
log.append((value,klass))
return [klass]
addClassAdvisor(pong)
示例4: kindInfo
def kindInfo(**attrs):
"""Declare metadata for a class' schema item
The attributes defined by the keyword arguments will be set on the
enclosing class' schema Item. For example, the following class'
repository Kind will have a ``displayName`` of ``"Example Item"``, and
a ``displayAttribute`` of ``"someAttr"``::
class SomeItem(schema.Item):
schema.kindInfo(
displayName = "Example Item",
displayAttribute = "someAttr",
)
``kindInfo()`` can only be used in the body of a class that derives from
``schema.Item`` or ``schema.Enumeration``, and it will only accept keywords
that are valid attributes of the ``Kind`` or ``Enumeration`` kinds,
respectively.
(Note: if your class includes a ``__metaclass__`` definition, any calls to
``kindInfo`` must come *after* the ``__metaclass__`` assignment.)
"""
_update_info('kindInfo','__kind_info__',attrs)
def callback(cls):
for k,v in attrs.items():
if not hasattr(cls._kind_class, k):
raise TypeError(
"%r is not an attribute of %s" %
(k, cls._kind_class.__name__)
)
return cls
from zope.interface.advice import addClassAdvisor
addClassAdvisor(callback)
示例5: __init__
def __init__(self, arg1, arg2=None, status=None):
if arg2 is None:
self.fromname = None
self.toname = arg1
else:
self.fromname = arg1
self.toname = lambda self: arg2
addClassAdvisor(self.advise)
self.status = status
示例6: __call__
def __call__(self, fn):
# We are being used as a descriptor.
assert self.fromname is None, (
"redirection() can not be used as a descriptor in its "
"two argument form")
self.fromname = self.toname
self.toname = fn
addClassAdvisor(self.advise)
return fn
示例7: implements
def implements(*ifaces):
"""
This is a slight hack, it overrides the default zope.interface implements
function because it to uses the addClassAdvisor functionality to delay its
work till after the class is created. So we must as well, and by placing
our hook in here we avoid the client having to manually add it.
"""
declarations._implements("implements", ifaces, _classImplements)
# These are called after the class that
advice.addClassAdvisor(_register_class)
advice.addClassAdvisor(_verify_class)
示例8: _implements
def _implements(name, interfaces, classImplements):
frame = sys._getframe(2)
locals = frame.f_locals
# Try to make sure we were called from a class def
if (locals is frame.f_globals) or ('__module__' not in locals):
raise TypeError(name+" can be used only from a class definition.")
if '__implements_advice_data__' in locals:
raise TypeError(name+" can be used only once in a class definition.")
locals['__implements_advice_data__'] = interfaces, classImplements
addClassAdvisor(_implements_advice, depth=3)
示例9: delegates
def delegates(interface_spec, context='context'):
"""Make an adapter into a decorator.
Use like:
class RosettaProject:
implements(IRosettaProject)
delegates(IProject)
def __init__(self, context):
self.context = context
def methodFromRosettaProject(self):
return self.context.methodFromIProject()
If you want to use a different name than "context" then you can explicitly
say so:
class RosettaProject:
implements(IRosettaProject)
delegates(IProject, context='project')
def __init__(self, project):
self.project = project
def methodFromRosettaProject(self):
return self.project.methodFromIProject()
The adapter class will implement the interface it is decorating.
The minimal decorator looks like this:
class RosettaProject:
delegates(IProject)
def __init__(self, context):
self.context = context
"""
# pylint: disable-msg=W0212
frame = sys._getframe(1)
locals = frame.f_locals
# Try to make sure we were called from a class def
if (locals is frame.f_globals) or ('__module__' not in locals):
raise TypeError(
"delegates() can be used only from a class definition.")
locals['__delegates_advice_data__'] = interface_spec, context
addClassAdvisor(_delegates_advice, depth=2)
示例10: classProvides
def classProvides(*interfaces):
"""Declare interfaces provided directly by a class
This function is called in a class definition.
The arguments are one or more interfaces or interface specifications
(``IDeclaration`` objects).
The given interfaces (including the interfaces in the specifications)
are used to create the class's direct-object interface specification.
An error will be raised if the module class has an direct interface
specification. In other words, it is an error to call this function more
than once in a class definition.
Note that the given interfaces have nothing to do with the interfaces
implemented by instances of the class.
This function is provided for convenience. It provides a more convenient
way to call directlyProvides for a class. For example::
classProvides(I1)
is equivalent to calling::
directlyProvides(theclass, I1)
after the class has been created.
"""
# This entire approach is invalid under Py3K. Don't even try to fix
# the coverage for this block there. :(
if PYTHON3: #pragma NO COVER
raise TypeError(_ADVICE_ERROR % 'provider')
frame = sys._getframe(1)
locals = frame.f_locals
# Try to make sure we were called from a class def
if (locals is frame.f_globals) or ('__module__' not in locals):
raise TypeError("classProvides can be used only from a "
"class definition.")
if '__provides__' in locals:
raise TypeError(
"classProvides can only be used once in a class definition.")
locals["__provides__"] = _normalizeargs(interfaces)
addClassAdvisor(_classProvides_advice, depth=2)
示例11: _implements
def _implements(name, interfaces, classImplements):
frame = sys._getframe(2)
locals = frame.f_locals
# Try to make sure we were called from a class def. In 2.2.0 we can't
# check for __module__ since it doesn't seem to be added to the locals
# until later on.
if (locals is frame.f_globals) or (
('__module__' not in locals) and sys.version_info[:3] > (2, 2, 0)):
raise TypeError(name+" can be used only from a class definition.")
if '__implements_advice_data__' in locals:
raise TypeError(name+" can be used only once in a class definition.")
locals['__implements_advice_data__'] = interfaces, classImplements
addClassAdvisor(_implements_advice, depth=3)
示例12: instanceClassOf
def instanceClassOf(typeClass):
"""Signal that the class suite is an instance factory for the
specified protocol class.
The instance class must have a constructor that accepts no arguments.
"""
def callback(instanceClass):
class InstanceFactory:
def __init__(self, protocolClass):
pass
def buildInstance(self):
return instanceClass()
components.registerAdapter(InstanceFactory, typeClass,
igwt.IInstanceFactory)
annotation.registerTypeAdapter(typeClass, instanceClass)
return instanceClass
advice.addClassAdvisor(callback)
示例13: _implements
def _implements(name, interfaces, classImplements):
# This entire approach is invalid under Py3K. Don't even try to fix
# the coverage for this block there. :(
frame = sys._getframe(2)
locals = frame.f_locals
# Try to make sure we were called from a class def. In 2.2.0 we can't
# check for __module__ since it doesn't seem to be added to the locals
# until later on.
if locals is frame.f_globals or '__module__' not in locals:
raise TypeError(name+" can be used only from a class definition.")
if '__implements_advice_data__' in locals:
raise TypeError(name+" can be used only once in a class definition.")
locals['__implements_advice_data__'] = interfaces, classImplements
addClassAdvisor(_implements_advice, depth=3)
示例14: factory
def factory(regex):
_f = {}
def decorator(f):
_f[f.__name__] = f
return f
def advisor(cls):
def wrapped(f):
def __init__(self, *args, **kwargs):
f(self, *args, **kwargs)
for func_name in _f:
orig = _f[func_name]
func = getattr(self, func_name)
if func.im_func==orig:
self.register(method, regex, func)
return __init__
cls.__init__ = wrapped(cls.__init__)
return cls
addClassAdvisor(advisor)
return decorator
示例15: _implements
def _implements(name, args, kw, classImplements):
#NOTE: str(classImplements) в данном случае используется просто в качестве уникального идентификатора
def _implements_advice(cls):
classImplements(cls, *args, **kw)
return cls
frame = sys._getframe(2)
locals = frame.f_locals
# Try to make sure we were called from a class def. In 2.2.0 we can't
# check for __module__ since it doesn't seem to be added to the locals
# until later on.
if (locals is frame.f_globals) or (
('__module__' not in locals) and sys.version_info[:3] > (2, 2, 0)):
raise TypeError(name+" can be used only from a class definition.")
if str(classImplements) in locals:
raise TypeError(name+" can be used only once in a class definition.")
locals[str(classImplements)] = args, kw, classImplements
addClassAdvisor(_implements_advice, depth=3)