Probably unwisely... I have added the following mix-in class to my 
quixote.directory module. It enables me to dynamically 'reload' modules using 
Graham Fawcett's rtemplate (not quite a full reload of the module more like a 
private reload). The mix-in should also work with reload(mymodule) as well 
which would be a public reload. 

It fills a missing piece for me in my Quixote activities as I like to write my 
gui (web pages) as PTL classes rather than modules and using classes that get 
set as long lived attributes (default behavior of Directory class) does not 
work well with rtemplate. Hence _q_refresh. Using _q_refresh enables me to very 
quickly prototype and make changes to my gui without the usual stop/start of 
servers. Not sure if I would run it in production but very useful during 
development. I am posting in the hope that someone else will find it useful 
like I found rtemplate useful.

I have added this code to the end of my quixote/directory.py file 
-----------code start------------------------------------------------

class Refreshing(object):
    """
    A mix-in class that provides the _q_refresh() method.  _q_refresh()
    is called if a component name appears in the _q_exports list but is
    not an instance attribute. But unlike _q_resolve, _q_refresh does not
    set the returned instance as an attribute of the parent, therefore the
    instance is shortlived and a new instance is created each time it is called.
    _q_refresh is expected to return the component object.
    
    _q_refresh is ideal for those who prefer to place ui code at the class level
    rather than at the module level and still want to dynamically reload
    modules/classes without the usual stop/start of the SCGI|Medusa|FCGI 
server..

    Basically when _q_refresh is used in combination with reload(mymodule),
    mymodule.MyUIClass() is dynamically refreshed without restarting the
    application server.

    Graham Fawcett's rtemplate is a good companion to _q_refresh.
    (google ---> site:mail.mems-exchange.org rtemplate)
    """

    def _q_refresh(self, name):
        return None

    def _q_translate(self, component):
        """(component: [string]) -> name, object

        Same as _q_translate in Directory class except:
        1. does not setattr(...)
        2. returns a tuple of name and obj
        """
        obj = None
        name = super(Refreshing, self)._q_translate(component)
        if name is not None and not hasattr(self, name):
            obj = self._q_refresh(name)
            #setattr(self, name, obj)
        return name, obj

    def _q_traverse(self, path):
        """(path: [string]) -> object

        Traverse a path and return the result.
        """
        assert len(path) > 0
        component = path[0]
        path = path[1:]
        # get a name and an obj from the returned tupple
        name, obj = self._q_translate(component)
        if obj is not None:     # if we already have the obj don't go looking 
for it
            pass
        elif name is not None:
            obj = getattr(self, name)
        else:
            obj = self._q_lookup(component)
            
        if obj is None:
            raise TraversalError(private_msg=('directory %r has no component '
                                              '%r' % (self, component)))
        if path:
            return obj._q_traverse(path)
        elif callable(obj):
            return obj()
        else:
            return obj

-------------code end---------------------------------

Regards,

Tristan
_______________________________________________
Quixote-users mailing list
[email protected]
http://mail.mems-exchange.org/mailman/listinfo/quixote-users

Reply via email to