[Zope] Help! Disabling caching for external method - I think it is Python problem

Satheesh Babu sbabu@tnc.org
Wed, 16 Jan 2002 15:24:01 -0500


This is a multi-part message in MIME format.

------=_NextPart_000_000F_01C19EA1.D32C3EA0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello guys,

Solved it after tons of trial and error. I don't know if it is a bug in
Python or in my code (this should be most likely!).

I've attached my python script - you can run it from command line. Problem
happens where I've marked "ALERT". It works now, but if you take out
the default paramater there, it doesn't - it is as if older values left by
previous function call is taken.

-vsb

"Saying Windows XP is the most stable Windows is like saying asparagus is
the most articulate vegetable" - Dave Barry


-----Original Message-----
From: Dieter Maurer [mailto:dieter@handshake.de]
Sent: Wednesday, January 16, 2002 2:44 PM
To: sbabu@tnc.org
Cc: zope@zope.org
Subject: Re: [Zope] Help! Disabling caching for external method


Satheesh Babu writes:
 > ....
 > However, whenever I try to call it from a DTML method, the results I get
 > back are
 > (I think) cached ones, regardless of the URL1 I'm passing to it.
The results of External Methods are not cached (at least not
by the External Method (or Zope)).
You must have a different problem.

The problem may well be caching -- by the browser.
You can usually detect this, when you call "reload" with the
"Shift" key pressed. When this gives you the correct
anwser, then the problem was caused by HTTP caching.
There is a HowTo on Zope.org explaining how to control
(HTTP) caching.


Dieter

------=_NextPart_000_000F_01C19EA1.D32C3EA0
Content-Type: text/plain;
	name="sitenavi_filter.py"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="sitenavi_filter.py"

"""Sitenavi Filter

make proper URLs for navigation items
depending upon which is the current page, looks at the navigation
list where there is indentation filters the list w.r.t the current
page.

sbabu@tnc.org 12/13/2001
"""

import sys
import string
import os
import time

__version__ =3D '0.0.1'

debug_on =3D 1

def get_displayable_ids(i_par_child, i_child_par, i_look_for, =
i_displayable_ids=3D[]):
    """ returns a list of ids that ought to be displayed
    to get what all ids should be displayed,first print its own =
children,
    then recursively check for parents
    for each parent such found, add all its children to the list"""
    o_displayable_ids =3D i_displayable_ids
    if i_par_child.has_key(i_look_for):
        for child in i_par_child[i_look_for]:
            o_displayable_ids.append(child)
    if not i_child_par.has_key(i_look_for):
        return o_displayable_ids
    for parent in i_child_par[i_look_for]:
        if i_par_child.has_key(parent):
            for peers in i_par_child[parent]:
                o_displayable_ids.append(peers)
        o_displayable_ids =3D get_displayable_ids(i_par_child, =
i_child_par, parent, o_displayable_ids)
    return o_displayable_ids

def make_trees(i_sitenavi):
    """ make the tree structures necessary from the navigation list"""
    sitenavi_by_link =3D {}
    sitenavi_by_id =3D {}
    tree_parent_child =3D {}
    tree_child_parent =3D {}
    len_sitenavi =3D len(i_sitenavi)
    parents_stack=3D[] #temp store for building parent child tuples
    for i in range(0, len_sitenavi):
        (naviid, url, caption, indent) =3D i_sitenavi[i]
        sitenavi_by_link[url] =3D (caption,indent,naviid)
        sitenavi_by_id[naviid] =3D (url,caption,indent)
        if i =3D=3D 0:
            parents_stack.append(naviid)
            continue
        l_ps =3D len(parents_stack)
        if l_ps>0:
            if debug_on > 1:
                print ">>[tree] naviid, url", naviid, url
                print ">>[tree] \tparents",parents_stack
            p_id =3D parents_stack[l_ps-1]
            (p_url,p_caption,p_indent) =3D sitenavi_by_id[p_id]
            if indent > p_indent:
                if not tree_parent_child.has_key(p_id): =
tree_parent_child[p_id] =3D []
                if not tree_child_parent.has_key(naviid): =
tree_child_parent[naviid] =3D []
                tree_parent_child[p_id].append(naviid)
                tree_child_parent[naviid].append(p_id)
                parents_stack.append(naviid)
            elif indent < p_indent:
                while (indent <=3D p_indent):
                    parents_stack.pop()
                    indent =3D indent + 1
                parents_stack.append(naviid)
            else:
                #same level, attach this to the current parent
                parents_stack.pop()
                l_ps =3D len(parents_stack)
                if l_ps>0:
                    p_id =3D parents_stack.pop()
                    (p_url,p_caption,p_indent) =3D sitenavi_by_id[p_id]
                    if not tree_parent_child.has_key(p_id): =
tree_parent_child[p_id] =3D []
                    if not tree_child_parent.has_key(naviid): =
tree_child_parent[naviid] =3D []
                    tree_parent_child[p_id].append(naviid)
                    tree_child_parent[naviid].append(p_id)
                    parents_stack.append(p_id)
                parents_stack.append(naviid)
            if debug_on > 1:
                print "\t",parents_stack
    return (sitenavi_by_link, sitenavi_by_id, tree_parent_child, =
tree_child_parent)



def make_short_url(i_url, i_base, i_siteroot, =
i_default_url=3D'index.html'):
    """Return a shortest possible version of the url"""
    o_url =3D string.replace(i_url,i_base,'')
    o_url =3D string.replace(o_url, i_siteroot,'')
    o_url =3D string.replace(o_url,i_default_url,'')
    o_url =3D string.strip(o_url)
    l_o_url =3D len(o_url)
    #we can assume that only .html files have use of this module
    #the following is to attach / for all folder urls
    if l_o_url > 5:
        if o_url[l_o_url-5:] !=3D '.html':
            if o_url[l_o_url-1] !=3D '/':
                o_url =3D o_url + '/'
    if o_url =3D=3D '':
        o_url =3D '/'
    return o_url


def filtered_navigation_list(i_sitenavi, i_current_page_url, i_base_url, =
i_siteroot_url):
    """Returns a cut down version of the list based upon the refactoring
    """
    if debug_on =3D=3D 1:
        print "-" * 78
        print ">> base=3D", i_base_url
        print ">> siteroot=3D", i_siteroot_url
        print ">> url=3D", i_current_page_url=20
    s_current_page_url =3D make_short_url(i_current_page_url, =
i_base_url, i_siteroot_url)
    if debug_on =3D=3D 1:
        print ">> url=3D", i_current_page_url=20
        print ">> short=3D", s_current_page_url
    #cleanup sitenavi first
    len_sitenavi =3D len(i_sitenavi)
    sitenavi_clean =3D []
    for i in range(0, len_sitenavi):
        (naviid, url, caption, indent) =3D i_sitenavi[i]
        url =3D make_short_url(url, i_base_url, i_siteroot_url)
        sitenavi_clean.append( (naviid, url, caption, indent))
    (sitenavi_by_link, sitenavi_by_id, tree_parent_child, =
tree_child_parent) =3D make_trees(sitenavi_clean)
    displayable_ids =3D []
    dirnm =3D os.path.dirname(s_current_page_url)+'/'
    if sitenavi_by_link.has_key(s_current_page_url):
        current_page_id =3D sitenavi_by_link[s_current_page_url][2]
        #### ALERT ALERT ALERT #####
        #Just take out the last "displayable_ids" and I guarantee that =
you'll loose
        #lot of hair. It opens up some kind of memory retention.
        displayable_ids =3D get_displayable_ids(tree_parent_child, =
tree_child_parent, current_page_id,displayable_ids)
        if debug_on =3D=3D 1:
            print ">> current page id", current_page_id
            print ">> current, navi ", s_current_page_url, =
sitenavi_by_link[s_current_page_url]
            print ">> disp", displayable_ids
    elif sitenavi_by_link.has_key(dirnm):
        current_page_id =3D sitenavi_by_link[dirnm][2]
        displayable_ids =3D get_displayable_ids(tree_parent_child, =
tree_child_parent, current_page_id)
        if debug_on =3D=3D 1:
            print ">> current, navi ", dirnm, sitenavi_by_link[dirnm]
            print ">> disp", displayable_ids
    x_sitenavi =3D []
    for i in range(0, len_sitenavi):
        (naviid, url, caption, indent) =3D i_sitenavi[i]
        if (indent=3D=3D0) or (naviid in displayable_ids):
            x_sitenavi.append(list(i_sitenavi[i]))
    out_sitenavi =3D []
    #to each item, add a lookahead in the indentation
    #we are doing it here because in our Zope, python
    #scripts are not yet supported.
    for i in range(1,len(x_sitenavi)):
        tl =3D x_sitenavi[i-1]
        tl.append(x_sitenavi[i][3])
        out_sitenavi.append(tuple(tl))
    tl =3D x_sitenavi[i]
    tl.append(0) #last item is always zero
    out_sitenavi.append(tuple(tl))
    if debug_on =3D=3D 1:
        for i in out_sitenavi:
            (naviid, url, caption, indent,next) =3D i
            print '%','  '*indent,naviid, url, caption, indent, next
    return out_sitenavi

def test():
    """tester data for the module
    """
    #(id, url, caption, indent)
    #this defines a navigation structure like
    # Home
    # Links
    #   Glossary of terms
    # Teacher resources
    sitenavi =3D [
        ('0', '/inter/lgp/index.html', 'Home', 0)
        , ('1', 'links/index.html', 'Related Links', 0)
        , ('2', 'links/art5773.html', 'Glossary of Terms', 1)
        , ('3', 'resources/index.html', 'Teacher Resources', 0)
    ]=20
    base_url =3D 'http://localhost:8080'
    siteroot_url =3D '/inter/lgp/'

    current_page_url =3D base_url + siteroot_url + =
'resources/index.html'
    filtered_sitenavi =3D filtered_navigation_list(sitenavi, =
current_page_url, base_url, siteroot_url)
    #here it should show
    #Home
    #Links
    #Teacher Resources

    current_page_url =3D base_url + siteroot_url + 'links/index.html'
    filtered_sitenavi =3D filtered_navigation_list(sitenavi, =
current_page_url, base_url, siteroot_url)
    #here it should show
    #Home
    #Links
    # Glossary of terms
    #Teacher Resources

    current_page_url =3D base_url + siteroot_url + =
'resources/index.html'
    filtered_sitenavi =3D filtered_navigation_list(sitenavi, =
current_page_url, base_url, siteroot_url)
    #here it should show
    #Home
    #Links
    #Teacher Resources

    #but if you make the change according to the "ALERT", you will see =
the one for
    #the previous test

if __name__ =3D=3D '__main__': test()

------=_NextPart_000_000F_01C19EA1.D32C3EA0--