[Grok-dev] grokcore.component in non-web application

Martijn Faassen faassen at startifact.com
Wed Dec 3 11:16:29 EST 2008


Hey,

Lacko Roman wrote:
> In past project I used grok framework to build my web application, and I 
> must say I was impressed.

We're very happy to hear that!

> Zope’s Component Architecture (ZCA) was exactly the missing future in my 
> python world J
> Now I’m developing some desktop applications (one using wxPython, 
> another with Mozilla+PyXPCOM extensions) and I definitely want to use 
> ZCA,  grokcore.component seems to be the perfect choice.
> But I have some questions about using ZCA, especially howto use 
> Component Registries.
> My question is: is there some examples of using ZCA/grokcore.component 
> outside of zope server ?

I'll give you a code example. Perhaps you can contribute a document for 
grok.zope.org (or the grokcore.component documentation) so that others 
can find out how to do it.

You make your python project depend on grokcore.component in its 
setup.py. This should pull in the right dependencies already.

Then in your own code:

from zope.configuration import xmlconfig

def main():
     import mypackage
     xmlconfig.file('configure.zcml', package=mypackage)

This will try to process the 'configure.zcml' file in the package 
'mypackage' (which should be your project's Python package). This code 
should run at startup time of your application.

In configure.zcml, place the following

<configure
     xmlns="http://namespaces.zope.org/zope"
     xmlns:grok="http://namespaces.zope.org/grok">

   <include package="grokcore.component" file="meta.zcml" />

   <grok:grok package="." />

</configure>

This will first make sure that grokcore.component is registered (through 
the include of its 'meta.zcml', and then will grok your own package, 
looking for anything that subclasses from grokcore.component's base 
components (such as grokcore.component.Adapter).

So now you can start using grokcore.component.Adapter and the like in 
your codebase and it should work.

If you have multiple packages, you would have your main package do an 
'include' of all its dependencies. Or you could look at z3c.autoinclude 
which can do it for you based on the dependencies listed in setup.py - 
that should result in a configure.zcml that you shouldn't have to modify 
again.

> I understand that components are registered when modules are groked, and 
> that there are methods like grok.testing.grok/ grok.testing.grok_component
> But I don’t exactly now when and how to use them.

The grokcore.component.testing.grok function allows you to Grok 
individual classes and instances within your tests. If you want to know 
more I'll look for examples too.

I hope this works for you!

Regards,

Martijn



More information about the Grok-dev mailing list