Author: bbangert
Date: 2006-01-26 12:53:50 -0700 (Thu, 26 Jan 2006)
New Revision: 1543

Modified:
   FormEncode/trunk/formencode/validators.py
Log:
Patch applied, and updated with more complete docstring and emptystring 
condition along with encoding option. Closes Ticket #66

Modified: FormEncode/trunk/formencode/validators.py
===================================================================
--- FormEncode/trunk/formencode/validators.py   2006-01-26 18:53:31 UTC (rev 
1542)
+++ FormEncode/trunk/formencode/validators.py   2006-01-26 19:53:50 UTC (rev 
1543)
@@ -982,6 +982,43 @@
     def empty_value(self, value):
         return ''
 
+class UnicodeString(String):
+    """
+    Converts things to unicode string, this is a specialization of
+    the String class.
+    
+    In addition to the String arguments, an encoding argument is also
+    accepted. By default the encoding will be utf-8.
+    
+    All converted strings are returned as Unicode strings.
+    
+    ::
+    
+        >>> UnicodeString().from_python(None)
+        u''
+        >>> UnicodeString().from_python([])
+        u''
+        >>> UnicodeString(encoding='utf-7').from_python('Ni Ni Ni')
+        u'Ni Ni Ni'
+    """
+    
+    encoding = 'utf-8'
+    
+    def _from_python(self, value, state):
+        if value:
+            # If it is already an unicode string...
+            if isinstance(value, unicode):
+                # ... we return it as-is.
+                return value
+            # Otherwise we convert it.
+            return unicode(value, self.encoding)
+        if value == 0:
+            return unicode(value, self.encoding)
+        return unicode("", self.encoding)
+
+    def empty_value(self, value):
+        return unicode("", self.encoding)
+
 class Set(FancyValidator):
 
     """



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
FormEncode-CVS mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/formencode-cvs

Reply via email to