API : Widget and Template, needs details

1 view
Skip to first unread message

David Bernard

unread,
Jan 15, 2006, 11:25:59 AM1/15/06
to turbo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Context (Proof of Concept):
- ---------------------------

* I try to write widgets (simple and composite) without Kid (use
formencode.htmlgen)
* I try to use TurboHtmlPy as Template Engine to render widget

I create a test controllers available at
http://dwayneb.free.fr/turbohtmlpy/controllers.py

(I also rewrite turbogears/widgets/base.py,results available soon, but
it's not need for the test)

Results:
- --------
in the table, poc is used when widget are wrote with formencode.htmlgen

PageRenderer Form def other widgets def display
kid kid kid OK
kid kid poc OK
kid poc poc OK
kid poc kid FAILED
htmlpy kid kid FAILED
htmlpy kid poc FAILED
htmlpy poc poc OK
html poc kid FAILED

urls:
1: http://localhost:8080/?engine=kid&form=full_kid .... OK
2: http://localhost:8080/?engine=kid&form=form_kid .... OK
3: http://localhost:8080/?engine=kid&form=full_poc .... OK
4: http://localhost:8080/?engine=kid&form=form_poc .... FAILED
5: http://localhost:8080/?engine=htmlpy&form=full_kid . FAILED
6: http://localhost:8080/?engine=htmlpy&form=form_poc . FAILED
7: http://localhost:8080/?engine=htmlpy&form=form_kid . FAILED
8: http://localhost:8080/?engine=htmlpy&form=full_poc . OK

When failed Widgets are displayed as string :
<generator object at 0xb75e214c>

Cause:
- ------
Lake of specification for Widgets.insert() method (and same for
TemplatePlugin.transform()).
"...the output is ElementTree Elements..."

So my implementation, I translate this into :
* a ElementTree.Element
* or a list/tuple of ElementTree.Element

But for kid return a "generator" (or stream into kid'api)

Suggestions:
- ------------
* details the api and be provide tools for simple integration with
TemplatePlugin implementation
* the return of methods, is a ElementTree.Element or a list/tuple of
ElementTree.Element, then I suggest to patch TurboKid (see below for a
basic patch, maybe optimisation are possible when widget and page use
the same template engine). This solution remove from each engine to have
special traitement for kid.

For information :
- -----------------
* generator/stream are iterator, that produce Element without children
* to translate a generator/stream into an ElementTree.Element, the
following code seems to work :

from kid.pull import ElementStream
el = ElementStream(stream).expand()

- --
- --------------------------------------------------------------
David "Dwayne" Bernard Freelance Developer
mailto:dwa...@java-fan.com
\|/ http://dwayne.java-fan.com
- --o0O @.@ O0o-------------------------------------------------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDyneXN11itWi/tc4RAmhrAJ96afniKC9rzJ6K/3fWuIr+dh2M0wCeMMCi
uAY4ICYAG9oxHaEtZHsyPgw=
=nK9c
-----END PGP SIGNATURE-----

David Bernard

unread,
Jan 16, 2006, 11:13:55 AM1/16/06
to turbo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kevin could provide an opinion / help ?

David Bernard a écrit :
> Context (Proof of Concept):


> ---------------------------
>
> * I try to write widgets (simple and composite) without Kid (use
> formencode.htmlgen)
> * I try to use TurboHtmlPy as Template Engine to render widget
>
> I create a test controllers available at
> http://dwayneb.free.fr/turbohtmlpy/controllers.py
>
> (I also rewrite turbogears/widgets/base.py,results available soon, but
> it's not need for the test)
>
> Results:

> --------
> in the table, poc is used when widget are wrote with formencode.htmlgen
>
> PageRenderer Form def other widgets def display
> kid kid kid OK
> kid kid poc OK
> kid poc poc OK
> kid poc kid FAILED
> htmlpy kid kid FAILED
> htmlpy kid poc FAILED
> htmlpy poc poc OK
> html poc kid FAILED
>
> urls:
> 1: http://localhost:8080/?engine=kid&form=full_kid .... OK
> 2: http://localhost:8080/?engine=kid&form=form_kid .... OK
> 3: http://localhost:8080/?engine=kid&form=full_poc .... OK
> 4: http://localhost:8080/?engine=kid&form=form_poc .... FAILED
> 5: http://localhost:8080/?engine=htmlpy&form=full_kid . FAILED
> 6: http://localhost:8080/?engine=htmlpy&form=form_poc . FAILED
> 7: http://localhost:8080/?engine=htmlpy&form=form_kid . FAILED
> 8: http://localhost:8080/?engine=htmlpy&form=full_poc . OK
>
> When failed Widgets are displayed as string :
> <generator object at 0xb75e214c>
>
> Cause:

> ------
> Lake of specification for Widgets.insert() method (and same for
> TemplatePlugin.transform()).
> "...the output is ElementTree Elements..."
>
> So my implementation, I translate this into :
> * a ElementTree.Element
> * or a list/tuple of ElementTree.Element
>
> But for kid return a "generator" (or stream into kid'api)
>
> Suggestions:

> ------------
> * details the api and be provide tools for simple integration with
> TemplatePlugin implementation
> * the return of methods, is a ElementTree.Element or a list/tuple of
> ElementTree.Element, then I suggest to patch TurboKid (see below for a
> basic patch, maybe optimisation are possible when widget and page use
> the same template engine). This solution remove from each engine to have
> special traitement for kid.
>
> For information :

> -----------------
> * generator/stream are iterator, that produce Element without children
> * to translate a generator/stream into an ElementTree.Element, the
> following code seems to work :
>
> from kid.pull import ElementStream
> el = ElementStream(stream).expand()
>
> --

> --------------------------------------------------------------
> David "Dwayne" Bernard Freelance Developer
> mailto:dwa...@java-fan.com
> \|/ http://dwayne.java-fan.com

> --o0O @.@ O0o-------------------------------------------------

- --
- --------------------------------------------------------------
David "Dwayne" Bernard Freelance Developer (Java)


mailto:dwa...@java-fan.com
\|/ http://dwayne.java-fan.com
- --o0O @.@ O0o-------------------------------------------------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDy8ZDN11itWi/tc4RAlacAKCIkwUU848S2wSZd4XH9O0nsmJTTQCeKg8M
ELeSbiWdm5StDAxwX2xtMIg=
=j7NU
-----END PGP SIGNATURE-----

Kevin Dangoor

unread,
Jan 16, 2006, 12:17:01 PM1/16/06
to turbo...@googlegroups.com
On 1/15/06, David Bernard <dwa...@java-fan.com> wrote:
> When failed Widgets are displayed as string :
> <generator object at 0xb75e214c>
>
> Cause:
> - ------
> Lake of specification for Widgets.insert() method (and same for
> TemplatePlugin.transform()).
> "...the output is ElementTree Elements..."
>
> So my implementation, I translate this into :
> * a ElementTree.Element
> * or a list/tuple of ElementTree.Element
>
> But for kid return a "generator" (or stream into kid'api)
>
> Suggestions:
> - ------------
> * details the api and be provide tools for simple integration with
> TemplatePlugin implementation
> * the return of methods, is a ElementTree.Element or a list/tuple of
> ElementTree.Element, then I suggest to patch TurboKid (see below for a
> basic patch, maybe optimisation are possible when widget and page use
> the same template engine). This solution remove from each engine to have
> special traitement for kid.

There has been talk of having a template plugin base class. That could
implement the transform method (basically parsing out the rendered
result into Elements). Of course, many template engines aren't going
to produce valid XML. Maybe the better way to go is have
widget.insert() call template engine.transform if available or call
XML(render())

Kevin

David Bernard

unread,
Jan 16, 2006, 1:04:55 PM1/16/06
to TurboGears
My PluginTemplate works in the other way, produce Elements (nativ
format) in transform and flatten the tree of Element to string into
render.

My question is about what must be return by engine.transform() and
widget.insert()

Both are important because, I've got widget that doesn't use engine to
provide the insert method.

Thanks

Kevin Dangoor

unread,
Jan 17, 2006, 10:34:58 AM1/17/06
to turbo...@googlegroups.com

I see. It's always interesting to hear of the unexpected things people
think of :)

You're right that the transform documentation is just plain wrong.

In [2]:t = kid.Template("<html><p>Data</p></html>")

In [3]:t.transform()
Out[3]:<generator object at 0x5dc670>

In [4]:list(t.transform())
Out[4]:
[(1, <Element u'html' at 0x5c0d10>),
(1, <Element u'p' at 0x5c0da0>),
(3, u'Data'),
(2, <Element u'p' at 0x5c0da0>),
(2, <Element u'html' at 0x5c0d10>)]

David Stanek probably knows offhand what the first part of those tuples are.

Kevin

--
Kevin Dangoor
Author of the Zesty News RSS newsreader

email: k...@blazingthings.com
company: http://www.BlazingThings.com
blog: http://www.BlueSkyOnMars.com

David Bernard

unread,
Jan 17, 2006, 11:25:38 AM1/17/06
to turbo...@googlegroups.com
I submitted the patch
http://trac.turbogears.org/turbogears/ticket/403

to fix the problem on TurboKid.

From my understand, the first number is used to tell is the Element
must be used as event type (in Pull terminology) like START_TAG,
END_TAG... and Element provide by the generator are only used to store
tag's name and its attributes, but not its children Elements.

Regards

FYI : I expect to release ASAP a fork of Widget, created without Kid and
my feedback about this approach.

Kevin Dangoor a écrit :

--

--------------------------------------------------------------
David "Dwayne" Bernard Freelance Developer
mailto:dwa...@java-fan.com
\|/ http://dwayne.java-fan.com

--o0O @.@ O0o-------------------------------------------------

signature.asc
Reply all
Reply to author
Forward
0 new messages