SVN: r23656 - in trunk/quixote: . src

Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Mon, 08 Mar 2004 13:09:22 -0500
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Author: nascheme
Date: 2004-03-08 13:09:22 -0500 (Mon, 08 Mar 2004)
New Revision: 23656

Modified:
   trunk/quixote/_py_htmltext.py
   trunk/quixote/src/_c_htmltext.c
Log:
Allow arbitrary mapping objects to be used as the right hand operand
when formating htmltext objects (i.e. as an argument to the
htmltext.__mod__ method).   The _c_htmltext implemention used to only
allow 'dict' instances.  Change _c_htmltext to allow any mapping.  Also,
instead of accessing every item in the mapping, use PyObject_GetItem()
(aka __getitem__) to retrieve the values as they are required.  That
matches the behavior of PyString_Format().


Modified: trunk/quixote/_py_htmltext.py
===================================================================
--- trunk/quixote/_py_htmltext.py	2004-03-05 21:39:30 UTC (rev 23655)
+++ trunk/quixote/_py_htmltext.py	2004-03-08 18:09:22 UTC (rev 23656)
@@ -72,24 +72,18 @@
     def __mod__(self, args):
         codes = []
         usedict = 0
-        klass = self.__class__
         for format in _format_re.findall(self.s):
             if format[-1] != '%':
                 if format[1] == '(':
                     usedict = 1
                 codes.append(format[-1])
         if usedict:
-            if not hasattr(args, "items"):
-                raise TypeError, "mapping required"
-            wrapped_args = {}
-            for (k, v) in args.items():
-                wrapped_args[k] = _wraparg(klass, v)
-            args = wrapped_args
+            args = _DictWrapper(args)
         else:
             if len(codes) == 1 and not isinstance(args, TupleType):
                 args = (args,)
-            args = tuple([_wraparg(klass, arg) for arg in args])
-        return klass(self.s % args)
+            args = tuple([_wraparg(arg) for arg in args])
+        return self.__class__(self.s % args)
 
     def __add__(self, other):
         if isinstance(other, StringType):
@@ -169,8 +163,15 @@
     def __repr__(self):
         return self.escape(`self.value`)
 
-def _wraparg(klass, arg):
-    if (classof(arg) is klass or
+class _DictWrapper(object):
+    def __init__(self, value):
+        self.value = value
+
+    def __getitem__(self, key):
+        return _wraparg(self.value[key])
+
+def _wraparg(arg):
+    if (classof(arg) is htmltext or
         isinstance(arg, IntType) or
         isinstance(arg, LongType) or
         isinstance(arg, FloatType)):

Modified: trunk/quixote/src/_c_htmltext.c
===================================================================
--- trunk/quixote/src/_c_htmltext.c	2004-03-05 21:39:30 UTC (rev 23655)
+++ trunk/quixote/src/_c_htmltext.c	2004-03-08 18:09:22 UTC (rev 23656)
@@ -23,8 +23,19 @@
 
 #define QuoteWrapper_Check(v)	((v)->ob_type == &QuoteWrapper_Type)
 
+
 typedef struct {
 	PyObject_HEAD
+	PyObject *obj;
+} DictWrapperObject;
+
+static PyTypeObject DictWrapper_Type;
+
+#define DictWrapper_Check(v)	((v)->ob_type == &DictWrapper_Type)
+
+
+typedef struct {
+	PyObject_HEAD
 	int html;
 	char *buf;
 	size_t size;
@@ -179,6 +190,38 @@
 }
 
 static PyObject *
+dict_wrapper_new(PyObject *o)
+{
+	DictWrapperObject *self;
+	self = PyObject_New(DictWrapperObject, &DictWrapper_Type);
+	if (self == NULL)
+		return NULL;
+	Py_INCREF(o);
+	self->obj = o;
+	return (PyObject *)self;
+}
+
+static void
+dict_wrapper_dealloc(DictWrapperObject *self)
+{
+	Py_DECREF(self->obj);
+	PyObject_Del(self);
+}
+
+static PyObject *
+dict_wrapper_subscript(DictWrapperObject *self, PyObject *key)
+{
+	PyObject *v, *w;;
+	v = PyObject_GetItem(self->obj, key);
+	if (v == NULL) {
+		return NULL;
+	}
+	w = quote_wrapper_new(v); 
+	Py_DECREF(v);
+	return w;
+}
+
+static PyObject *
 htmltext_from_string(PyObject *s)
 {
 	/* note, this takes a reference */
@@ -316,21 +359,9 @@
 		}
 	}
 	if (do_dict) {
-		int pos = 0;
-		PyObject *key, *value;
-		wargs = PyDict_New();
-		while (PyDict_Next(args, &pos, &key, &value)) {
-			PyObject *wvalue = wrap_arg(value);
-			if (wvalue == NULL) {
-				Py_DECREF(wargs);
-				return NULL;
-			}
-			if (PyDict_SetItem(wargs, key, wvalue) < 0) {
-				Py_DECREF(wargs);
-				return NULL;
-			}
-			Py_DECREF(wvalue);
-		}
+		wargs = dict_wrapper_new(args);
+		if (wargs == NULL)
+			return NULL;
 	}
 	else if (PyTuple_Check(args)) {
 		long i, n = PyTuple_GET_SIZE(args);
@@ -808,6 +839,30 @@
 	(unaryfunc)quote_wrapper_str,  /*tp_str*/
 };
 
+static PyMappingMethods dict_wrapper_as_mapping = {
+        0, /*mp_length*/
+        (binaryfunc)dict_wrapper_subscript, /*mp_subscript*/
+        0, /*mp_ass_subscript*/
+};
+
+static PyTypeObject DictWrapper_Type = {
+	PyObject_HEAD_INIT(NULL)
+	0,			/*ob_size*/
+	"DictWrapper",		/*tp_name*/
+	sizeof(DictWrapperObject),	/*tp_basicsize*/
+	0,			/*tp_itemsize*/
+	/* methods */
+	(destructor)dict_wrapper_dealloc, /*tp_dealloc*/
+	0,			/*tp_print*/
+	0,			/*tp_getattr*/
+	0,			/*tp_setattr*/
+	0,			/*tp_compare*/
+	0,			/*tp_repr*/
+	0,			/*tp_as_number*/
+	0,			/*tp_as_sequence*/
+	&dict_wrapper_as_mapping,/*tp_as_mapping*/
+};
+
 static PyNumberMethods template_io_as_number = {
 	0, /*nb_add*/
 	0, /*nb_subtract*/