[Checkins] SVN: five.pt/trunk/ Add somewhat evil method to hook into grok. Unfortunately grok makes it extremely hard to do this more cleanly.

Vincent Fretin vincent.fretin at gmail.com
Wed Apr 14 10:27:13 EDT 2010


On Wed, Apr 14, 2010 at 12:06 PM, Wichert Akkerman <wichert at wiggy.net> wrote:
> Log message for revision 110841:
>  Add somewhat evil method to hook into grok. Unfortunately grok makes it extremely hard to do this more cleanly.
>
> Changed:
>  U   five.pt/trunk/CHANGES.txt
>  U   five.pt/trunk/src/five/pt/patches.py
>
> -=-
> Modified: five.pt/trunk/CHANGES.txt
> ===================================================================
> --- five.pt/trunk/CHANGES.txt   2010-04-14 09:48:30 UTC (rev 110840)
> +++ five.pt/trunk/CHANGES.txt   2010-04-14 10:06:30 UTC (rev 110841)
> @@ -1,6 +1,11 @@
>  Changelog
>  =========
>
> +?.? - unreleased
> +~~~~~~~~~~~~~~~~
> +
> +- Basic support for five.grok templates. [wichert]
> +
>  0.8 - 2010-01-05
>  ~~~~~~~~~~~~~~~~
>
>
> Modified: five.pt/trunk/src/five/pt/patches.py
> ===================================================================
> --- five.pt/trunk/src/five/pt/patches.py        2010-04-14 09:48:30 UTC (rev 110840)
> +++ five.pt/trunk/src/five/pt/patches.py        2010-04-14 10:06:30 UTC (rev 110841)
> @@ -81,3 +81,26 @@
>  ZopeViewPageTemplateFile.__get__ = get_bound_template
>  PageTemplateFile.__call__ = call_template
>  PageTemplateFile.macros = property(get_macros)
> +
> +try:
> +    from five.grok.components import ZopeTwoPageTemplate
> +
> +    _tpf  = FiveViewPageTemplateFile(__file__)
> +    class GrokViewAwarePageTemplateFile(ViewPageTemplateFile):
> +        def pt_getContext(self, *args, **kw):
> +            global _tpf
> +            return _tpf.pt_getContext(*args, **kw)
> +        def pt_render(self, namespace, **kw):
> +            if "args" in namespace:
> +                del namespace["args"]
> +            context=namespace.pop("context")
> +            request=namespace.pop("request")
> +            view=namespace["view"]
> +            return self.__call__(_ob=view, context=context, request=request, **namespace)
> +
> +    def setFromFilename(self, filename, _prefix=None):
> +        self._template = GrokViewAwarePageTemplateFile(filename, _prefix)
> +    ZopeTwoPageTemplate.setFromFilename = setFromFilename
> +except ImportError:
> +    pass
> +

Hi Wichert,

It's great you are fixing five.grok+chameleon.

I think with the change you did, you don't have the "static" variable
in the namespace, Am I wrong?
because you don't call pt_grokContext which call actually getNamespace
which call view.default_namespace (where static is defined), and
view.namespace (where you can give special variable to the template)

I didn't test it, so maybe it works and I got lost in the code. :)
If you don't have the static variable, we can change the code to:

    _tpf  = FiveViewPageTemplateFile(__file__)
    class GrokViewAwarePageTemplateFile(ViewPageTemplateFile):
        def pt_getContext(self, *args, **kw):
            global _tpf
            c = {}
            if hasattr(self, 'pt_grokContext'):
                c.update(self.pt_grokContext)
            c.update(_tpf.pt_getContext(*args, **kw))
            return c
        def pt_render(self, namespace, **kw):
            if "args" in namespace:
                del namespace["args"]
            context=namespace.pop("context")
            request=namespace.pop("request")
            view=namespace["view"]
            return self.__call__(_ob=view, context=context,
request=request, **namespace)


But more I read the code, more I'm lost. Is the pt_render and
pt_getContext really executed here ? because the
ViewPageTemplateFile.__call__  doesn't use these methods AFAICS
I'm not sure. Maybe the code should only be:

class GrokViewAwarePageTemplateFile(ViewPageTemplateFile):
        def _pt_get_context(self, instance, request, kwargs={}):
            c = super(ViewPageTemplateFile, self)._pt_get_context()
            if hasattr(self, 'pt_grokContext'):
                c.update(self.pt_grokContext)
            return c

?

Vincent


More information about the checkins mailing list