[wwwsearch-commits] r18303 - wwwsearch/ClientForm/trunk

jjlee at codespeak.net jjlee at codespeak.net
Sun Oct 9 03:25:17 CEST 2005


Author: jjlee
Date: Sun Oct  9 03:25:14 2005
New Revision: 18303

Modified:
   wwwsearch/ClientForm/trunk/ClientForm.py
   wwwsearch/ClientForm/trunk/test.py
Log:
Add id argument to .get(); Minor improvements to docs and tests

Modified: wwwsearch/ClientForm/trunk/ClientForm.py
==============================================================================
--- wwwsearch/ClientForm/trunk/ClientForm.py	(original)
+++ wwwsearch/ClientForm/trunk/ClientForm.py	Sun Oct  9 03:25:14 2005
@@ -1434,8 +1434,11 @@
             " ".join(["%s=%r" % (k, v) for k, v in attrs])
             )
 
-def disambiguate(items, nr, name, label):
-    msg = "name=%r label=%r" % (name, label)
+def disambiguate(items, nr, **kwds):
+    msgs = []
+    for key, value in kwds.items():
+        msgs.append("%s=%r" % (key, value))
+    msg = " ".join(msgs)
     if not items:
         raise ItemNotFoundError(msg)
     if nr is None:
@@ -1560,7 +1563,8 @@
         else:
             return False
 
-    def get_items(self, name=None, label=None, exclude_disabled=False):
+    def get_items(self, name=None, label=None, id=None,
+                  exclude_disabled=False):
         """Return matching items by name or label.
 
         For argument docs, see the docstring for .get()
@@ -1570,6 +1574,8 @@
             raise TypeError("item name must be string-like")
         if label is not None and not isstringlike(label):
             raise TypeError("item label must be string-like")
+        if id is not None and not isstringlike(id):
+            raise TypeError("item id must be string-like")
         items = []  # order is important
         compat = self._form.backwards_compat
         for o in self.items:
@@ -1584,32 +1590,43 @@
                         break
                 else:
                     continue
+            if id is not None and o.id != id:
+                continue
             items.append(o)
         return items
 
-    def get(self, name=None, label=None, nr=None, exclude_disabled=False):
+    def get(self, name=None, label=None, id=None, nr=None,
+            exclude_disabled=False):
         """Return item by name or label, disambiguating if necessary with nr.
 
         All arguments must be passed by name, with the exception of 'name',
         which may be used as a positional argument.
 
+        If name is specified, then the item must have the indicated name.
+
+        If label is specified, then the item must have a label whose
+        whitespace-compressed text substring-matches the indicated label
+        string (eg. label="please choose" will match
+        "  Do  please  choose an item ").
+
+        If id is specified, then the item must have the indicated id.
+
         nr is an optional 0-based index of the items matching the query.
 
         If nr is the default None value and more than item is found, raises
         AmbiguityError (unless the HTMLForm instance's backwards_compat
         attribute is true).
 
-        If no item is found, raises ItemNotFoundError.
-
-        If items are found but nr is specified and not found, raises KeyError.
+        If no item is found, or if items are found but nr is specified and not
+        found, raises ItemNotFoundError.
 
         Optionally excludes disabled items.
 
         """
         if nr is None and self._form.backwards_compat:
             nr = 0  # :-/
-        items = self.get_items(name, label, exclude_disabled)
-        return disambiguate(items, nr, name, label)
+        items = self.get_items(name, label, id, exclude_disabled)
+        return disambiguate(items, nr, name=name, label=label, id=id)
 
     def _get(self, name, by_label=False, nr=None, exclude_disabled=False):
         # strictly for use by deprecated methods
@@ -2903,10 +2920,13 @@
         nr, if supplied, is the sequence number of the control (where 0 is the
         first).  Note that control 0 is the first control matching all the
         other arguments (if supplied); it is not necessarily the first control
-        in the form.
+        in the form.  If no nr is supplied, AmbiguityError is raised if
+        multiple controls match the other arguments (unless the
+        .backwards-compat attribute is true).
 
         If label is specified, then the control must have this label.  Note
         that radio controls and checkboxes never have labels: their items do.
+
         """
         if ((name is None) and (type is None) and (kind is None) and
             (id is None) and (label is None) and (predicate is None) and

Modified: wwwsearch/ClientForm/trunk/test.py
==============================================================================
--- wwwsearch/ClientForm/trunk/test.py	(original)
+++ wwwsearch/ClientForm/trunk/test.py	Sun Oct  9 03:25:14 2005
@@ -1756,8 +1756,9 @@
         self.assertRaises(ItemNotFoundError, c.set, False, "oops")
         self.assertRaises(TypeError, c.set, False, ["value_value"])
         reset_deprecations()
+
         # tests for multiple identical values
-        
+
         attrs = {"type": "this is ignored",
                  "name": "name_value",
                  "value": "value_value",
@@ -2756,6 +2757,9 @@
         reset_deprecations()
 
     def test_select_control_nr_and_label(self):
+        for compat in [False, True]:
+            self._test_select_control_nr_and_label(compat)
+    def _test_select_control_nr_and_label(self, compat):
         f = StringIO("""\
 <form>
     <select multiple name="form.grocery">
@@ -2766,7 +2770,7 @@
 </form>
 """)
         form = ClientForm.ParseFile(f, "http://example.com/",
-                                    backwards_compat=True)[0]
+                                    backwards_compat=compat)[0]
         ctl = form.find_control("form.grocery")
         # ordinary case
         self.assertEqual(ctl.get("p", nr=1).id, "3")
@@ -2786,6 +2790,10 @@
         # nr too high again
         self.assertRaises(ItemNotFoundError, ctl.get, label="a", nr=3)
 
+        self.assertEqual(ctl.get(id="2").id, "2")
+        self.assertRaises(ItemNotFoundError, ctl.get, id="4")
+        self.assertRaises(ItemNotFoundError, ctl.get, id="4")
+
     def test_label_whitespace(self):
         for compat in [False, True]:
             f = StringIO("""\


More information about the wwwsearch-commits mailing list