------ part of utility.py -------------------------------------------------

def _dprintline(intro,data):
    """
        convenience function for _dprint() to print a debug message

	with a little code to improve clarity on HTML pages
    """
    if os.environ.has_key("GATEWAY_INTERFACE"): print "<br>"
    print "%s %s" % (intro,data)
    if os.environ.has_key("GATEWAY_INTERFACE"): print "<br>"


def _dprint( *args):
    """
        print function toggled on or off by global Debug variable
        _dprint must always be called by a locally defined debug function, e.g.:
            def dprint(x):
                if debug:
                    apply(utility_dprint,args)

        to ensure the correct details are output

#        Enhanced to ensure reasonable output of unicode objects whose value
#        is not prinatble under the current locale.  if an UnicodeError
#        Exception occurs then try printing using __repr__ rather than __str__
#        This should force printing of the unicode values, e.g. u'\u3f08\u3f09'

        Now automatically prints unicode objects in their representation format

    """
#    print "into _dprint, args=", args,"type(args)",type(args)
    if os.environ.has_key("GATEWAY_INTERFACE"): print "<br>"
    filename = "?"
    caller = "?"
    lineno = 0
    classname = "?."
    cargs = []
    cvarargs = ""
    cvarkw = ""
    clocs = {}
    diagmsg = " (no last frame)"
    lastframe = inspect.currentframe().f_back
    if lastframe:
        largs,lvarargs,lvarkw,llocs = inspect.getargvalues(lastframe)
        if 0:
            print "largs",largs
            print "#lvarargs",lvarargs
            print "#lvarkw",lvarkw,"."
            print "#llocs",llocs
        callerframe = lastframe.f_back
        diagmsg = " (no caller frame)"
        if callerframe:
            diagmsg = ""
            filename = callerframe.f_code.co_filename
            caller = callerframe.f_code.co_name
            lineno = callerframe.f_lineno
            try:
                classname = callerframe.f_locals["self"].__class__.__name__ + "."
            except:
                classname = ""
            filename = os.path.split(filename)[1]

    intro = "! %s:%d in %s%s%s: " % (filename,lineno,classname,caller,diagmsg)
    if 0 and len(args) == 1 and type(args[0]) == type({}):
            # DISABLED FOR NOW
            # this is a dictionary by itself, so do a printdictionary style
        aname = largs[0]
        avalue = llocs[aname]
        keys = avalue.keys()
        keys.sort()
        _dprintline(intro,"Dictionary %s has %d items" % (aname,len(keys)))
        for k in keys:
                # and of course there is only a single arge
            _dprintline(intro,"  %s[%s] = %s" % (aname,k,printable(avalue[k])))

    elif args:
        data = []
        for arg in args: data.append(printable(arg))
        _dprintline(intro," ".join(data))
    else:
            # now handle special parameterless behaviour - print the caller's
            # parameters and arguments
        cargs,cvarargs,cvarkw,clocs = inspect.getargvalues(callerframe)
        if 0:
            print "#varargs",cvarargs
            print "#varkw",cvarkw,"."
            print "#locs",clocs
        for a in cargs:
            _dprintline(intro,"%s = %s" % (a,printable(clocs[a])))
        if cvarargs in clocs:
            _dprintline(intro,"*%s = %s" % (cvarargs,printable(clocs[cvarargs])))
        if cvarkw in clocs:
            for v in clocs[cvarkw]:
                _dprintline(intro,"**%s = %s" % (v,printable(clocs[cvarkw][v])))

------ part of utility.py -------------------------------------------------
