quixote html.py,1.12,1.13

Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]>
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Update of /home/cvs/quixote
In directory hewson:/tmp/cvs-serv2200

Modified Files:
	html.py 
Log Message:
Make htmltext.__mod__ work is dictionaries for real.  The other approach
could not possibly have worked (think '%(x)s %(x)r' % ...).


Index: html.py
===================================================================
RCS file: /home/cvs/quixote/html.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- html.py	15 Oct 2002 18:54:01 -0000	1.12
+++ html.py	15 Oct 2002 20:25:56 -0000	1.13
@@ -46,7 +46,7 @@
 
 import sys
 import urllib
-from types import UnicodeType, TupleType, StringType
+from types import UnicodeType, TupleType, StringType, IntType, FloatType
 import re
 
 if sys.hexversion < 0x20200b1:
@@ -130,30 +130,22 @@
                 if format[1] == '(':
                     usedict = 1
                 codes.append(format[-1])
-        if usedict:
-            keys = args.keys()
-            values = [args[k] for k in keys]
-        else:
-            if len(codes) == 1 and not isinstance(args, TupleType):
-                values = (args,)
-            else:
-                values = args
-        quoted_values = []
-        for value, code in zip(values, codes):
-            if classof(value) is self.__class__:
-                quoted_values.append(value)
-            elif code == 's':
-                quoted_values.append(self.quote(str(value)))
-            elif code == 'r':
-                quoted_values.append(_QuotedRepr(value, self.quote))
-            elif code == 'c':
-                raise ValueError, "htmltext does not allow '%c' format"
+        def wraparg(arg):
+            if (classof(arg) is self.__class__ or
+                isinstance(arg, IntType) or
+                isinstance(arg, FloatType)):
+                # ints, floats, and htmltext are okay
+                return arg
             else:
-                quoted_values.append(value)
+                # everything is gets wrapped
+                return _QuoteWrapper(arg, self.quote)
         if usedict:
-            args = dict(zip(keys, quoted_values))
+            for (k, v) in args.items():
+                args[k] = wraparg(v)
         else:
-            args = tuple(quoted_values)
+            if len(codes) == 1 and not isinstance(args, TupleType):
+                args = (args,)
+            args = tuple(map(wraparg, args))
         return self.__class__(self.s % args)
 
     def __add__(self, other):
@@ -218,11 +210,17 @@
     def capitalize(self):
         return self.__class__(self.s.capitalize())
 
-class _QuotedRepr:
-    # helper for htmltext class
+class _QuoteWrapper:
+    # helper for htmltext class __mod__
+
+    __slots__ = ['value', 'quote']
+
     def __init__(self, value, quote):
         self.value = value
         self.quote = quote
+
+    def __str__(self):
+        return self.quote(str(self.value))
 
     def __repr__(self):
         return self.quote(`self.value`)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.