|
From: <sub...@co...> - 2005-12-19 18:41:15
|
Author: ianb
Date: 2005-12-19 18:41:06 +0000 (Mon, 19 Dec 2005)
New Revision: 1432
Modified:
FormEncode/trunk/formencode/api.py
FormEncode/trunk/formencode/variabledecode.py
Log:
Added option to variable_encode to not include the --repetitions variables, and added an option to Invalid.unpack_errors(True) that returns a flat dictionary that also excludes those --repetitions variables
Modified: FormEncode/trunk/formencode/api.py
===================================================================
--- FormEncode/trunk/formencode/api.py 2005-12-19 18:39:06 UTC (rev 1431)
+++ FormEncode/trunk/formencode/api.py 2005-12-19 18:41:06 UTC (rev 1432)
@@ -63,12 +63,17 @@
# val += " (value: %s)" % repr(self.value)
return val
- def unpack_errors(self):
+ def unpack_errors(self, encode_variables=False):
"""
Returns the error as a simple data structure -- lists,
dictionaries, and strings.
+
+ If ``encode_variables`` is true, then this will return a flat
+ dictionary, encoded with variable_encode
"""
if self.error_list:
+ assert not encode_variables, (
+ "You can only encode dictionary errors")
assert not self.error_dict
result = []
for item in self.error_list:
@@ -84,8 +89,13 @@
result[name] = item
else:
result[name] = item.unpack_errors()
+ if encode_variables:
+ import variabledecode
+ result = variabledecode.variable_encode(result, add_repetitions=False)
return result
else:
+ assert not encode_variables, (
+ "You can only encode dictionary errors")
return self.msg
Modified: FormEncode/trunk/formencode/variabledecode.py
===================================================================
--- FormEncode/trunk/formencode/variabledecode.py 2005-12-19 18:39:06 UTC (rev 1431)
+++ FormEncode/trunk/formencode/variabledecode.py 2005-12-19 18:41:06 UTC (rev 1432)
@@ -104,7 +104,11 @@
return result
+<<<<<<< .mine
+def variable_encode(d, prepend='', result=None, add_repetitions=True):
+=======
def variable_encode(d, prepend='', result=None, dict_char='.', list_char='-'):
+>>>>>>> .r1430
"""
Encodes a nested structure into a flat dictionary.
"""
@@ -118,15 +122,16 @@
name = key
else:
name = "%s%s%s" % (prepend, dict_char, key)
- variable_encode(value, name, result)
+ variable_encode(value, name, result, add_repetitions)
elif isinstance(d, list):
for i in range(len(d)):
variable_encode(d[i], "%s%s%i" % (prepend, list_char, i), result)
- if prepend:
- repName = '%s--repetitions' % prepend
- else:
- repName = '__repetitions__'
- result[repName] = str(len(d))
+ if add_repetitions:
+ if prepend:
+ repName = '%s--repetitions' % prepend
+ else:
+ repName = '__repetitions__'
+ result[repName] = str(len(d))
else:
result[prepend] = d
return result
|