ok, I've checked in an initial implementation of phillip's proposal, complete with documentation... even faster than a 2-day xml parser :)

documentation is in http://svn.osafoundation.org/chandler/trunk/chandler/application/schema_api.txt, copied here below. I'm working on the block-specific stuff now. Bryan, I think you might be pretty interested in this stuff as well.

Alec

Template Classes
================
It is possible to create a template class for your items, so that it and all 
other items can be described in a fairly declarative format.

The easiest way to do this is to use Item's default template() class method.
To create a template for your class, just reference the class method
``template`` on the Item class, and save it for later.

PersonT = Person.template

Now you can create a person template like this:
PersonT().install(parcel)

The advantage of the template system is that you can define your own
specialized template method, and take advantage of child relationships.

In the simplest case, all your class has to do is define template_child_attrs
to list the names of the child attributes.
class Person(Item):
    template_child_attrs = ['sons', 'daughters']

The base class Item has a default template() implementation which will use
template_child_attrs to ensure the children appear in the repository as 
children of the item. Now you could write

    PersonT(name='Dad',
            sons=[PersonT(name='junior')], 
            daughters=[PersonT(name='little sis')]
           ).install(parcel)

Specialized versions of the template method could also be used to define 
domain-specific syntax. 

For instance, a template for a parent with exactly one son, and one daughter:

    @classmethod
    def americanfamily(cls, son=None, daughter=None, **attrs):
	sons = daughters = None
        if son: sons=[son]
	if daughter: daughters=[daughter]
        return schema.ItemTemplate(cls, menuname, ['sons', 'daughters'], 
                                   sons=sons, daughters=daughters, **attrs)

Then you just define a template reference for family members:
FamilyMemberT = Person.americanfamily

And use it in installParcels():
    FamilyMemberT('Dad',
                  FamilyMemberT('junior'),
                  FamilyMemberT('little sis')).install(parcel)




Alec Flett wrote:
ok folks - Phillip has set me straight.. or at least, he came up with a scheme, in python, that provides a reasonable compact declaration-style mechanism for childblocks that isn't absurdly verbose, and meets most of the criteria I outlined before :)

Lesson learned from today: when phillip says "but you can do that easily in python" remember his understanding of python is much deeper and his definition of "easily" is probably a lot different than yours!

I'm working on a revised version of his original proposal...

Alec

Phillip J. Eby wrote:
At 02:19 PM 8/10/2005 -0700, Alec Flett wrote:

from somewhere.cpia_templates import Menu, MenuItem

Menu("FileMenu", _('File'), [
    Menu("NewMenu", _('New...'), [
        MenuItem("NewMessageItem", _("Message"), ...),
        MenuItem("NewNoteItem",    _("Note"), ...),
    ])
]).install(parcel)
Wow. If you can explain a little more about how to do this, I'll throw away all of my XmlForCpia work - seriously :)

do you mean that "Menu" is just a template wrapper around the Menu class, or that we're somehow making the Menu class also implement this template class?

i.e. is this something like, in somewhere.cpia_templates:

from osaf.framework import Blocks
Menu = template(Blocks.Menu)?

You could do it that way, although I think what you'd want is to have a 'template' classmethod on Item that created template instances.  Then somewhere.cpia_templates would be defined more like:

    Menu = Blocks.Menu.template

etc.






_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Open Source Applications Foundation "Dev" mailing list http://lists.osafoundation.org/mailman/listinfo/dev

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Open Source Applications Foundation "Dev" mailing list
http://lists.osafoundation.org/mailman/listinfo/dev

Reply via email to