DTUtil.py :  » Web-Server » SkunkWEB » skunkweb-3.4.4 » pylibs » DT » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Server » SkunkWEB 
SkunkWEB » skunkweb 3.4.4 » pylibs » DT » DTUtil.py
#  
#  Copyright (C) 2001 Andrew T. Csillag <drew_csillag@geocities.com>
#  
#      You may distribute under the terms of either the GNU General
#      Public License or the SkunkWeb License, as specified in the
#      README file.
#   
import sys
import string
import base64
import types
import DTExcept

def debacktick(v):
    '''removes backticks from [0] and [-1] if they are there'''
    if len(v) and (v[0]==v[-1]=='`'):
        return v[1:-1]
    return v

def htmlquote(s):
    l=[]
    for i in s:
        if i=='&': l.append('&amp;')
        elif i=='<': l.append('&lt;')
        elif i=='>': l.append('&gt;')
        # elif i=='\n': l.append('<BR>')
        elif i=='"': l.append('&quot;')
        else: l.append(i)
    return string.join(l, '')

def latinquote(s):
    """Escapes all characters >=160 to numeric escape."""
    l = []
    for i in s:
  if ord(i) >=160: l.append("&#%03d;" % ord(i))
  else: l.append(i)
    return string.join(l, '')

def fullquote(s):
    """Escapes all characters to %XX format."""
    l = []
    for i in s:
        l.append( "%%%02x" % ord(i) )
    return string.join(l, '')

def b64(s):
    return base64.encodestring(s)

def pseudoApply(nds, tup=(), dict={}, tcol=None, kwcol=None):
    retdict={}
    
    lennds=len(nds)
    lentup=len(tup)

    keywordables={}
    adjustkwcol=adjusttcol=0

    if kwcol is not None:
        adjustkwcol=1
        retdict[kwcol]={}
    if tcol is not None:
        adjusttcol=1
        retdict[tcol]=()
        
    #make a dict of argument names for later perusal
    for i in nds:
        if type(i)==types.TupleType:
            keywordables[i[0]]=1
        else:
            keywordables[i]=1
            
    #handle tuple arguments
    for i in range(min(lennds, lentup)):
        if type(nds[i])==types.TupleType:
            retdict[nds[i][0]]=tup[i]
        else:
            retdict[nds[i]]=tup[i]

    #oops, too many args
    if lentup>lennds:
        if tcol is None:
            raise TypeError, 'too many arguments; expected %d, got %d' % (
                lentup, lennds)
        else:
            retdict[tcol]=tup[lennds:]

    #handle keyword arguments
    for k,v in dict.items():
        if retdict.has_key(k):
            raise TypeError, 'keyword parameter redefined'
        elif not keywordables.has_key(k):
            if kwcol is None:
                raise TypeError, 'unexpected keyword argument: %s' % k
            else:
                retdict[kwcol][k]=v
        else:
            retdict[k]=v

    #handle default arguments
    for i in nds:
        if type(i)==types.TupleType:
            if not retdict.has_key(i[0]):
                val=i[1]
                #some hacks to make this fit into the old way
                if val=='None':
                    val=None
                elif val=='""':
                    val=''
                elif (type(val)==types.StringType
                    and len(val)>1
                    and val[0]==val[-1]=='"'): #dumb quoted string
                    val=val[1:-1]
                #end of bullshit hacks
                retdict[i[0]]=val

    #make an adjustment for length comparison
    adjustedretdictlen=len(retdict)-(adjusttcol+adjustkwcol)

    #did we get enough args?
    if lennds > adjustedretdictlen:
        #print retdict
        raise TypeError, 'not enough arguments; expected %d, got %d' % (
            lennds, adjustedretdictlen)

    #return
    #print retdict
    return retdict

def tagCall(tag, argspec, tcol=None, kwcol=None):
    t,d = tag.tupargs, tag.dictargs
    try:
        return pseudoApply ( argspec, t, d, tcol, kwcol )
    except:
        raise DTExcept.DTCompileError ( tag, 'error parsing arguments: %s\n'
             'args are %s, tuple args are %s, dictargs are %s' % 
             ( sys.exc_info()[1], argspec, t, d ) )
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.