ldifvar.py 7.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
'''Inserting values with the 'ldifvar' tag

    The 'ldifvar' tag is used to type-safely insert values into LDIF
    text.  The 'ldifvar' tag is similar to the 'var' tag, except that
    it replaces text formatting parameters with LDIF type information.

    The sqlvar tag has the following attributes:

      attr -- The name of the attribute to insert. As with other
              DTML tags.

      type -- The data type of the value to be inserted.  This
              attribute is required and may be one of 'string',
              'int', 'float', or 'nb'.  The 'nb' data type indicates a
              string that must have a length that is greater than 0.

      optional -- A flag indicating that a value is optional.  If a
                  value is optional and is not provided (or is blank
                  when a non-blank value is expected), then the string
                  'null' is inserted.

    For example, given the tag::

      <dtml-ldifvar x attr=objectClass type=nb optional>

    if the value of 'x' is::
      
      inetOrgPerson

    then the text inserted is:

      objectClass: inetOrgPerson

    however, if x is ommitted or an empty string, then the value
    inserted is ''.
'''
#__rcs_id__='$Id: sqlvar.py,v 1.14.10.2 2005/09/02 23:02:00 tseaver Exp $'

############################################################################
#     Copyright
#
#       Copyright 1996 Digital Creations, L.C., 910 Princess Anne
#       Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
#       rights reserved.
#
############################################################################
#__version__='$Revision: 1.14.10.2 $'[11:-2]

from DocumentTemplate.DT_Util import ParseError, parse_params, name_param
from string import find, split, join, atoi, atof
import sha
StringType=type('')

str=__builtins__['str']

class LDIFVar:
    name='ldifvar'

    def __init__(self, args):
        args = parse_params(args, name='', expr='', type=None, optional=1)

        name,expr=name_param(args,'ldifvar',1)
        if expr is None: expr=name
        else: expr=expr.eval
        self.__name__, self.expr = name, expr

        self.args=args
        if not args.has_key('type'):
            raise ParseError, ('the type attribute is required', 'dtvar')
        t=args['type']
        if not valid_type(t):
            raise ParseError, ('invalid type, %s' % t, 'dtvar')

    def render(self, md):
        name=self.__name__
        args=self.args
        t=args['type']
        try:
            expr=self.expr
            if type(expr) is type(''): v=md[expr]
            else: v=expr(md)
        except:
            if args.has_key('optional') and args['optional']:
                return
            if type(expr) is not type(''):
                raise
            raise ValueError, 'Missing input variable, <em>%s</em>' % name

        if v is None:
            return ''

        if t=='int':
            try:
                if type(v) is StringType:
                    if v[-1:]=='L':
                        v=v[:-1]
                    atoi(v)
                else: v=str(int(v))
            except:
                if not v and args.has_key('optional') and args['optional']:
                    return
                raise ValueError, (
                    'Invalid integer value for <em>%s</em>' % name)
        elif t=='float':
            try:
                if type(v) is StringType:
                    if v[-1:]=='L':
                        v=v[:-1]
                    atof(v)
                else: v=str(float(v))
            except:
                if not v and args.has_key('optional') and args['optional']:
                    return
                raise ValueError, (
                    'Invalid floating-point value for <em>%s</em>' % name)

        else:
            if not isinstance(v, (str, unicode)):
                v=str(v)
            if not v and t=='nb':
                if args.has_key('optional') and args['optional']:
                    return
                else:
                    raise ValueError, (
                        'Invalid empty string value for <em>%s</em>' % name)

        return v

    __call__=render

class LDIFLine:
    name='ldifline'

    def __init__(self, args):
        args = parse_params(args, name='', expr='', attr='', type=None, optional=1)

        name,expr=name_param(args,'ldifvar',1)
        if expr is None: expr=name
        else: expr=expr.eval
        self.__name__, self.expr = name, expr

        self.args=args
        if not args.has_key('type'):
            raise ParseError, ('the type attribute is required', 'ldifattr')
        t=args['type']
        if not valid_type(t):
            raise ParseError, ('invalid type, %s' % t, 'dtvar')
        
        if not args.has_key('attr'):
            raise ParseError, ('the attr attribute is required', 'ldifattr')
        a=args['attr']

    def render(self, md):
        name=self.__name__
        args=self.args
        t=args['type']
        a=args['attr']
        default = '%s:' % (a)
        try:
            expr=self.expr
            if type(expr) is type(''): v=md[expr]
            else: v=expr(md)
        except:
            if args.has_key('optional') and args['optional']:
                return default
            if type(expr) is not type(''):
                raise
            raise ValueError, 'Missing input variable, <em>%s</em>' % name

        if v is None:
            if args.has_key('optional') and args['optional']:
                return default
            else:
                raise ValueError, 'Missing input variable, <em>%s</em>' % name
        if a in ['',None]:
            return default

        if t=='int':
            try:
                if type(v) is StringType:
                    if v[-1:]=='L':
                        v=v[:-1]
                    atoi(v)
                else: v=str(int(v))
            except:
                if not v and args.has_key('optional') and args['optional']:
                    return default
                raise ValueError, (
                    'Invalid integer value for <em>%s</em>' % name)
        elif t=='float':
            try:
                if type(v) is StringType:
                    if v[-1:]=='L':
                        v=v[:-1]
                    atof(v)
                else: v=str(float(v))
            except:
                if not v and args.has_key('optional') and args['optional']:
                    return default
                raise ValueError, (
                    'Invalid floating-point value for <em>%s</em>' % name)

        else:
            if not isinstance(v, (str, unicode)):
                v=str(v)
            if not v and t=='nb':
                if args.has_key('optional') and args['optional']:
                    return default
                else:
                    raise ValueError, (
                        'Invalid empty string value for <em>%s</em>' % name)

        return '%s: %s' % (a, v)

    __call__=render

valid_type={'int':1, 'float':1, 'string':1, 'nb': 1}.has_key