|
From: <sub...@co...> - 2005-11-19 23:41:18
|
Author: ianb
Date: 2005-11-19 23:41:10 +0000 (Sat, 19 Nov 2005)
New Revision: 1301
Modified:
FormEncode/trunk/docs/news.txt
FormEncode/trunk/formencode/api.py
FormEncode/trunk/formencode/compound.py
FormEncode/trunk/formencode/schema.py
Log:
Added some methods to inspect all the messages that can be created
Modified: FormEncode/trunk/docs/news.txt
===================================================================
--- FormEncode/trunk/docs/news.txt 2005-11-19 23:32:29 UTC (rev 1300)
+++ FormEncode/trunk/docs/news.txt 2005-11-19 23:41:10 UTC (rev 1301)
@@ -4,6 +4,9 @@
SVN trunk
---------
+* Added an ``.all_messages()`` method to all validators, primarily
+ intended to be used for documentation purposes.
+
* Changed preferred name of ``StringBoolean`` to ``StringBool`` (to go
with ``bool`` and ``validators.Bool``). Old alias still available.
Modified: FormEncode/trunk/formencode/api.py
===================================================================
--- FormEncode/trunk/formencode/api.py 2005-11-19 23:32:29 UTC (rev 1300)
+++ FormEncode/trunk/formencode/api.py 2005-11-19 23:41:10 UTC (rev 1301)
@@ -129,6 +129,43 @@
% (e, msgName, self._messages.get(msgName), kw,
', '.join(self._messages.keys())))
+ def all_messages(self):
+ """
+ Return a dictionary of all the messages of this validator, and
+ any subvalidators if present. Keys are message names, values
+ may be a message or list of messages. This is really just
+ intended for documentation purposes, to show someone all the
+ messages that a validator or compound validator (like Schemas)
+ can produce.
+
+ @@: Should this produce a more structured set of messages, so
+ that messages could be unpacked into a rendered form to see
+ the placement of all the messages? Well, probably so.
+ """
+ msgs = self._messages.copy()
+ for v in self.subvalidators():
+ inner = v.all_messages()
+ for key, msg in inner:
+ if key in msgs:
+ if msgs[key] == msg:
+ continue
+ if isinstance(msgs[key], list):
+ msgs[key].append(msg)
+ else:
+ msgs[key] = [msgs[key], msg]
+ else:
+ msgs[key] = msg
+ return msgs
+
+ def subvalidators(self):
+ """
+ Return any validators that this validator contains. This is
+ not useful for functional, except to inspect what values are
+ available. Specifically the ``.all_messages()`` method uses
+ this to accumulate all possible messages.
+ """
+ return []
+
class _Identity(Validator):
def __repr__(self):
return 'validators.Identity'
Modified: FormEncode/trunk/formencode/compound.py
===================================================================
--- FormEncode/trunk/formencode/compound.py 2005-11-19 23:32:29 UTC (rev 1300)
+++ FormEncode/trunk/formencode/compound.py 2005-11-19 23:41:10 UTC (rev 1301)
@@ -56,6 +56,9 @@
return self.attempt_convert(value, state,
from_python)
+ def subvalidators(self):
+ return self.validators
+
class Any(CompoundValidator):
"""
Modified: FormEncode/trunk/formencode/schema.py
===================================================================
--- FormEncode/trunk/formencode/schema.py 2005-11-19 23:32:29 UTC (rev 1300)
+++ FormEncode/trunk/formencode/schema.py 2005-11-19 23:41:10 UTC (rev 1301)
@@ -280,6 +280,12 @@
add_pre_validator = declarative.classinstancemethod(add_pre_validator)
+ def subvalidators(self):
+ result = []
+ result.extend(self.pre_validators)
+ result.extend(self.chained_validators)
+ result.extend(self.fields.values())
+ return result
def format_compound_error(v, indent=0):
if isinstance(v, Exception):
|