| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
"""Provides helpers for Template.webInput(), a method for importing web |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
__author__ = "Mike Orr <iron@mso.oz.net>" |
|---|
| 16 |
__revision__ = "$Revision: 1.10 $"[11:-2] |
|---|
| 17 |
|
|---|
| 18 |
from Cheetah.Utils.Misc import useOrRaise |
|---|
| 19 |
|
|---|
| 20 |
class NonNumericInputError(ValueError): pass |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
class _Converter: |
|---|
| 26 |
"""A container object for info about type converters. |
|---|
| 27 |
.name, string, name of this converter (for error messages). |
|---|
| 28 |
.func, function, factory function. |
|---|
| 29 |
.default, value to use or raise if the real value is missing. |
|---|
| 30 |
.error, value to use or raise if .func() raises an exception. |
|---|
| 31 |
""" |
|---|
| 32 |
def __init__(self, name, func, default, error): |
|---|
| 33 |
self.name = name |
|---|
| 34 |
self.func = func |
|---|
| 35 |
self.default = default |
|---|
| 36 |
self.error = error |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
def _lookup(name, func, multi, converters): |
|---|
| 40 |
"""Look up a Webware field/cookie/value/session value. Return |
|---|
| 41 |
'(realName, value)' where 'realName' is like 'name' but with any |
|---|
| 42 |
conversion suffix strips off. Applies numeric conversion and |
|---|
| 43 |
single vs multi values according to the comments in the source. |
|---|
| 44 |
""" |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
colon = name.find(':') |
|---|
| 50 |
if colon != -1: |
|---|
| 51 |
longName = name |
|---|
| 52 |
shortName, ext = name[:colon], name[colon+1:] |
|---|
| 53 |
else: |
|---|
| 54 |
longName = shortName = name |
|---|
| 55 |
ext = '' |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
if longName != shortName: |
|---|
| 59 |
values = func(longName, None) or func(shortName, None) |
|---|
| 60 |
else: |
|---|
| 61 |
values = func(shortName, None) |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
if values is None: |
|---|
| 66 |
values = [] |
|---|
| 67 |
elif isinstance(values, str): |
|---|
| 68 |
values = [values] |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
try: |
|---|
| 72 |
converter = converters[ext] |
|---|
| 73 |
except KeyError: |
|---|
| 74 |
fmt = "'%s' is not a valid converter name in '%s'" |
|---|
| 75 |
tup = (ext, longName) |
|---|
| 76 |
raise TypeError(fmt % tup) |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
if converter.func is not None: |
|---|
| 81 |
tmp = values[:] |
|---|
| 82 |
values = [] |
|---|
| 83 |
for elm in tmp: |
|---|
| 84 |
try: |
|---|
| 85 |
elm = converter.func(elm) |
|---|
| 86 |
except (TypeError, ValueError): |
|---|
| 87 |
tup = converter.name, elm |
|---|
| 88 |
errmsg = "%s '%s' contains invalid characters" % tup |
|---|
| 89 |
elm = useOrRaise(converter.error, errmsg) |
|---|
| 90 |
values.append(elm) |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
if multi: |
|---|
| 98 |
return shortName, values |
|---|
| 99 |
if len(values) == 0: |
|---|
| 100 |
return shortName, useOrRaise(converter.default) |
|---|
| 101 |
return shortName, values[0] |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|