quixote html.py,1.11,1.12

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-serv1914

Modified Files:
	html.py 
Log Message:
Support mappings as arguments to __mod__.


Index: html.py
===================================================================
RCS file: /home/cvs/quixote/html.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- html.py	15 Oct 2002 17:50:12 -0000	1.11
+++ html.py	15 Oct 2002 18:54:01 -0000	1.12
@@ -73,10 +73,8 @@
     
     classof = type
 
-
 _format_codes = 'diouxXeEfFgGcrs%'
-_format_re = re.compile(r'%%[^%s]*([%s])' % (_format_codes, _format_codes))
-
+_format_re = re.compile(r'%%[^%s]*[%s]' % (_format_codes, _format_codes))
 
 class htmltext(object):
     """
@@ -125,23 +123,38 @@
         return hash(self.s)
 
     def __mod__(self, args):
-        codes = [code[-1] for code in _format_re.findall(self.s)
-                    if code != '%']
-        if len(codes) == 1 and not isinstance(args, TupleType):
-            args = (args,)
-        quoted_args = []
-        for arg, code in zip(args, codes):
-            if classof(arg) is self.__class__:
-                quoted_args.append(arg)
+        codes = []
+        usedict = 0
+        for format in _format_re.findall(self.s):
+            if format[-1] != '%':
+                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_args.append(self.quote(str(arg)))
+                quoted_values.append(self.quote(str(value)))
             elif code == 'r':
-                quoted_args.append(_QuotedRepr(arg, self.quote))
+                quoted_values.append(_QuotedRepr(value, self.quote))
             elif code == 'c':
                 raise ValueError, "htmltext does not allow '%c' format"
             else:
-                quoted_args.append(arg)
-        return self.__class__(self.s % tuple(quoted_args))
+                quoted_values.append(value)
+        if usedict:
+            args = dict(zip(keys, quoted_values))
+        else:
+            args = tuple(quoted_values)
+        return self.__class__(self.s % args)
 
     def __add__(self, other):
         if isinstance(other, StringType):
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.