Debian Codegraph @

Cheetah/Utils/WebInputMixin.py

raw symbols SCIP
1 """Provides helpers for Template.webInput(), a method for importing web
2 transaction variables in bulk. See the docstring of webInput for full details.
3 """
4
5 from .Misc import useOrRaise
6
7
8 class NonNumericInputError(ValueError):
9 pass
10
11 ##################################################
12 # PRIVATE FUNCTIONS AND CLASSES
13
14
15 class _Converter:
16 """A container object for info about type converters.
17 .name, string, name of this converter (for error messages).
18 .func, function, factory function.
19 .default, value to use or raise if the real value is missing.
20 .error, value to use or raise if .func() raises an exception.
21 """
22 def __init__(self, name, func, default, error):
23 self.name = name
24 self.func = func
25 self.default = default
26 self.error = error
27
28
29 def _lookup(name, func, multi, converters):
30 """Look up a Webware field/cookie/value/session value. Return
31 '(realName, value)' where 'realName' is like 'name' but with any
32 conversion suffix strips off. Applies numeric conversion and
33 single vs multi values according to the comments in the source.
34 """
35 # Step 1 -- split off the conversion suffix from 'name'; e.g. "height:int".
36 # If there's no colon, the suffix is "". 'longName' is the name with the
37 # suffix, 'shortName' is without.
38 # XXX This implementation assumes "height:" means "height".
39 colon = name.find(':')
40 if colon != -1:
41 longName = name
42 shortName, ext = name[:colon], name[colon+1:] # noqa: E226,E501 missing whitespace around operator
43 else:
44 longName = shortName = name
45 ext = ''
46
47 # Step 2 -- look up the values by calling 'func'.
48 if longName != shortName:
49 values = func(longName, None) or func(shortName, None)
50 else:
51 values = func(shortName, None)
52 # 'values' is a list of strings, a string or None.
53
54 # Step 3 -- Coerce 'values' to a list of zero, one or more strings.
55 if values is None:
56 values = []
57 elif isinstance(values, str):
58 values = [values]
59
60 # Step 4 -- Find a _Converter object or raise TypeError.
61 try:
62 converter = converters[ext]
63 except KeyError:
64 fmt = "'%s' is not a valid converter name in '%s'"
65 tup = (ext, longName)
66 raise TypeError(fmt % tup)
67
68 # Step 5 -- if there's a converter func, run it on each element.
69 # If the converter raises an exception, use or raise 'converter.error'.
70 if converter.func is not None:
71 tmp = values[:]
72 values = []
73 for elm in tmp:
74 try:
75 elm = converter.func(elm)
76 except (TypeError, ValueError):
77 tup = converter.name, elm
78 errmsg = "%s '%s' contains invalid characters" % tup
79 elm = useOrRaise(converter.error, errmsg)
80 values.append(elm)
81 # 'values' is now a list of strings, ints or floats.
82
83 # Step 6 -- If we're supposed to return a multi value, return the list
84 # as is. If we're supposed to return a single value and the list is
85 # empty, return or raise 'converter.default'. Otherwise, return the
86 # first element in the list and ignore any additional values.
87 if multi:
88 return shortName, values
89 if len(values) == 0:
90 return shortName, useOrRaise(converter.default)
91 return shortName, values[0]
92
93 # vim: sw=4 ts=4 expandtab