本文介绍了zope模式的循环导入引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题与这个SO问题非常相似,但是我尝试应用这些以前的答案的尝试并没有通过,有人建议我将其作为一个新问题开始:

i'm having a problem very similar to this SO question, but my attempts to apply these previous answers isn't going thru and it was suggested i start it as a new question:

在下面的代码中,我定义了几个getChoices()函数,我认为这些函数会延迟循环引用,但不!请问这是怎么了?

in the code below i define a couple of getChoices() functions that i thought would defer the circular refs, but no!? what's wrong here, please?

# ns.content/ns/content/foo.py
from zope import schema
from plone.directives import form
from z3c.relationfield.schema import Relation, RelationChoice
from plone.formwidget.contenttree import ObjPathSourceBinder

class IFoo(form.Schema):

    def getBarChoices():
        # avoiding circular refs...
        from bar import IBar
        return ObjPathSourceBinder(object_provides=IBar.__identifier__)

    barChoices = getBarChoices()
    form.widget(bar=AutocompleteFieldWidget)
    bar = Relation(source= barChoices,required=False)

# ns.content/ns/content/bar.py
from zope import schema
from plone.directives import form
from z3c.relationfield.schema import Relation, RelationChoice
from plone.formwidget.contenttree import ObjPathSourceBinder

class IBar(form.Schema):

    def getFooChoices():
        # avoiding circular refs...
        from foo import IFoo
        return ObjPathSourceBinder(object_provides=IFoo.__identifier__)

    fooChoices = getFooChoices()
    form.widget(foo=AutocompleteFieldWidget)
    foo = Relation(source= fooChoices,required=False)

resultingError = """
  File ".../buildout-cache/eggs/martian-0.11.3-py2.7.egg/martian/scan.py", line 217, in resolve
    __import__(used)
  File ".../zeocluster/src/ns.content/ns/content/bar.py", line 32, in <module>
    class IBar(form.Schema):
  File ".../zeocluster/src/ns.content/ns/content/bar.py", line 48, in IBar
    fooChoices = getFooChoices()
  File ".../zeocluster/src/ns.content/ns/content/bar.py", line 38, in getFooChoices
    from ns.content.foo import IFoo
  File ".../zeocluster/src/ns.content/ns/content/foo.py", line 33, in <module>
    class IFoo(form.Schema):
  File ".../zeocluster/src/ns.content/ns/content/foo.py", line 73, in IFoo
    barChoices = getBarChoices()
  File ".../zeocluster/src/ns.content/ns/content/foo.py", line 39, in getBarChoices
    from ns.content.bar import IBar
zope.configuration.xmlconfig.ZopeXMLConfigurationError: File ".../zeocluster/parts/client1/etc/site.zcml", line 16.2-16.23
    ZopeXMLConfigurationError: File ".../buildout-cache/eggs/Products.CMFPlone-4.2.0.1-py2.7.egg/Products/CMFPlone/configure.zcml", line 102.4-106.10
    ZopeXMLConfigurationError: File ".../zeocluster/src/ns.content/ns/content/configure.zcml", line 18.2-18.27
    ImportError: cannot import name IBar
"""

推荐答案

在定义类IFoo时,您正在定义时间调用getBarChoices().因此,在解析foo.py时将执行from bar import IBar,从而导致循环导入.

You're calling getBarChoices() at definition time, when defining the class IFoo. So from bar import IBar will be executed while parsing foo.py leading to the circular import.

据我所见,您基本上有两种选择:

As far as I see it, you have basically two choices:

1)使用字符串作为object_provides的标识符.

无论如何,您都已经通过使用IFoo.__identifier__进行了此操作,但是如果将其设置为静态而非动态,则将消除循环依赖:

You're doing that already anyway by using IFoo.__identifier__, but if you make it static instead of dynamic that will eliminate your circular dependencies:

source = ObjPathSourceBinder(object_provides='ns.content.bar.IBar')
bar = Relation(source=source,required=False)

无需在foo.py中导入IBar.这具有明显的缺点,即IBar的位置现在已在代码中进行了硬编码,因此,每当更改IBar的名称或位置时,都需要在foo.py中更新其点分名称.

No need to import IBar in foo.py. This has the obvious disadvantage that the location of IBar is now hardcoded in your code, so whenever you change the name or location of IBar you'll need to update its dotted name in foo.py.

2)标记界面

另一种选择是让IFooIBar实现您保存在第三个文件(例如ns/content/interfaces.py)中的标记接口.这样一来,您可以按照

The other alternative would be to let IFoo and IBar implement marker interfaces that you keep in a third file, ns/content/interfaces.py for example. That way you could do something along the lines of

interfaces.py

from zope.interface import Interface

class IBarMarker(Interface):
    """Marker interface for IBar objects.
    """

class IFooMarker(Interface):
    """Marker interface for IFoo objects.
    """

foo.py

from zope.interface import directlyProvides
from plone.directives import form
from plone.formwidget.contenttree import ObjPathSourceBinder
from plone.formwidget.autocomplete import AutocompleteFieldWidget
from z3c.relationfield.schema import RelationChoice

from ns.content.interfaces import IBarMarker
from ns.content.interfaces import IFooMarker


class IFoo(form.Schema):
    directlyProvides(IFooMarker)

    form.widget(bar=AutocompleteFieldWidget)
    bar = RelationChoice(source=ObjPathSourceBinder(
                            object_provides=IBarMarker.__identifier__),
                         required=False)

bar.py

class IBar(form.Schema):
    directlyProvides(IBarMarker)

    form.widget(foo=AutocompleteFieldWidget)
    foo = RelationChoice(source=ObjPathSourceBinder(
                            object_provides=IFooMarker.__identifier__),
                         required=False)

这篇关于zope模式的循环导入引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:11