[twill] fv_match suggestion (with patch included)

Andy Chambers andychambers2002 at yahoo.co.uk
Mon Oct 9 03:21:04 PDT 2006

Hi all,

Would anyone else find it useful if fv_match worked a little more like
fv_multi so that you could fill in control fields with the same name
with different values by adding extra args on the end of the command?


   fv_match form control arg1 arg2 arg3

...would set the first control to ar1, the second to arg2, the third
(and any remaining controls) to arg3.

I think it would be backwards compatible with the current version since
if the number of arguments is less than the number of matches, it just
uses the last argument for all remaining matches.  It has undergone
minimal testing but I'll be using it to test my web app at work so I'll
keep the list informed of any problems I find.

Andy Chambers
-------------- next part --------------
--- formfill.py.orig	2006-10-09 10:30:54.000000000 +0100
+++ formfill.py	2006-10-09 11:02:49.000000000 +0100
@@ -23,12 +23,15 @@
 __all__ = [ 'fv_match', 'fv_multi', 'fv_multi_sub' ]
-def fv_match(formname, regexp, value):
+def fv_match(formname, regexp, *values):
     >> fv_match <formname> <field regexp> <value>
-    Set value of *all* form fields with a name that matches the given
-    regular expression.
+    Set value of form fields with a name that matches the given
+    regular expression in the order that they appear with the 
+    corresponding value from values.  If there are more matches than
+    values, the last value is used for all remaining matches
     (Unlike 'formvalue' or 'fv', this will not complain about multiple
@@ -45,7 +48,7 @@
     matches = [ ctl for ctl in form.controls if regexp.search(str(ctl.name)) ]
     if matches:
-        print '-- matches %d' % (len(matches),)
+        print '-- matches %d, args %d' % (len(matches), len(values))
         n = 0
         for control in matches:
@@ -53,8 +56,11 @@
             if control.readonly:
+            try:
+            	twill.utils.set_form_control_value(control, values[n])
+	    except IndexError e:
+	    	twill.utils.set_form_control_value(control, values[-1])
             n += 1
-            twill.utils.set_form_control_value(control, value)
         print 'set %d values total' % (n,)

More information about the twill mailing list