Re: [Tutor] psp_site mmods

Magnus Lycka magnus at thinkware.se
Wed Jun 23 16:18:20 EDT 2004


Reed L. O'Brien wrote:
> Yes the demo site was working.  I am trying to add functionality.
> If you'd like to browse the current code you can at:
> http://snake.rokomworldwide.com/cgi-bin/trac.cgi/browser/trunk/

Ok, now I understand a bit more:

def results(req, searchWords):
    global answer
    if not (searchWords):
        #req.write
        answer = "Nothing to search for. Nothing Found"
                                                                                                                            
    handle = SwishE.new('/usr/local/swish-e/index.swish-e')
    search = handle.search('')
    results = search.execute(searchWords)
    files = [r.getproperty('swishdocpath') for r in results]
                                                                                                                            
#    req.write("The files found are: %s" % files)
    answer = "The files found are: %s" % files

    return _any_page(req, 'results')

There seems to be two bugs here. 

First of all, even if you go into the if-block, you will continue 
to do the search after that, replacing the first "answer" every
time. You need to use else. (I'd also get rid of the not, and make 
the "if" more "positive".

Secondly, "answer" should be included in the vars parameter of
the psp.PSP instance if I understand this correctly. You should
pass it to "_any_page". Avoid globals. I've added a named parameter
to _any_page for that. Note that existing callers of _any_page
can go on like before, no change needed there.

def results(req, searchWords):
    if (searchWords):
        handle = SwishE.new('/usr/local/swish-e/index.swish-e')
        search = handle.search('')
        results = search.execute(searchWords)
        files = [r.getproperty('swishdocpath') for r in results]
        answer = "The files found are: %s" % files
    else:
        answer = "Nothing to search for. Nothing Found"
    return _any_page(req, 'results', my_vars={'answer':answer})

def _any_page(req, name, vars={}):
    body_tmpl = _tmpl_path('%s_body.html' % name)

    vars.update({"menu": _menu(req, hlight=name),
                 "body": psp.PSP(req, body_tmpl)})

    main_tmpl = _tmpl_path(MAIN_TMPL)

    return psp.PSP(req, main_tmpl, vars=vars)

I still think it's clever to factor out the swishE stuff in a
separate module. No big deal for this small program, but code
tends to grow and get more complex, and then it's a good thing
to keep different things, such as dependencies on different
3rd party modules separate if it's easy to achieve.

-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list