[wwwsearch-commits] r17629 - wwwsearch/ClientForm/trunk/examples

jjlee at codespeak.net jjlee at codespeak.net
Sat Sep 17 22:26:53 CEST 2005


Author: jjlee
Date: Sat Sep 17 22:26:51 2005
New Revision: 17629

Modified:
   wwwsearch/ClientForm/trunk/examples/example.html
   wwwsearch/ClientForm/trunk/examples/example.py
Log:
Update examples

Modified: wwwsearch/ClientForm/trunk/examples/example.html
==============================================================================
--- wwwsearch/ClientForm/trunk/examples/example.html	(original)
+++ wwwsearch/ClientForm/trunk/examples/example.html	Sat Sep 17 22:26:51 2005
@@ -11,9 +11,10 @@
 
  <input type="checkbox" name="eggs" value="spam"></input>
 
- <select multiple name="cheeses">
-  <option name="mozz">Mozzarella</option>
-  <option name="caerphilly">Caerphilly</option>
+<label for="chz">Please select a cheese</label>
+ <select multiple name="cheeses" id="chz">
+  <option value="mozz">Mozzarella</option>
+  <option value="caerphilly">Caerphilly</option>
   <option>gouda</option>
   <option>gorgonzola</option>
   <option>parmesan</option>
@@ -35,7 +36,8 @@
 
  <input type="radio" name="smelly"></input>
 
- <select single name="favorite_cheese">
+<label for="fchz" value="What's your favourite cheese?" />
+ <select single name="favorite_cheese" id="fchz">
   <option>cheddar</option>
   <option>brie</option>
   <option>leicester</option>

Modified: wwwsearch/ClientForm/trunk/examples/example.py
==============================================================================
--- wwwsearch/ClientForm/trunk/examples/example.py	(original)
+++ wwwsearch/ClientForm/trunk/examples/example.py	Sat Sep 17 22:26:51 2005
@@ -2,7 +2,8 @@
 
 import ClientForm
 import urllib2
-request = urllib2.Request("http://wwwsearch.sf.net/ClientForm/example.html")
+#request = urllib2.Request("http://wwwsearch.sf.net/ClientForm/example.html")
+request = urllib2.Request("http://localhost/example.html")
 response = urllib2.urlopen(request)
 forms = ClientForm.ParseResponse(response)
 response.close()
@@ -12,28 +13,86 @@
 form = forms[0]
 print form  # very useful!
 
+# A 'control' is a graphical HTML form widget: a text entry box, a
+# dropdown 'select' list, a checkbox, etc.
+
 # Indexing allows setting and retrieval of control values
 original_text = form["comments"]  # a string, NOT a Control instance
 form["comments"] = "Blah."
 
 # Controls that represent lists (checkbox, select and radio lists) are
-# ListControls.  Their values are sequences of list item names.
+# ListControl instances.  Their values are sequences of list item names.
 # They come in two flavours: single- and multiple-selection:
-print form.possible_items("cheeses")
 form["favorite_cheese"] = ["brie"]  # single
 form["cheeses"] = ["parmesan", "leicester", "cheddar"]  # multi
+#  equivalent, but more flexible:
+form.set_value(["parmesan", "leicester", "cheddar"], name="cheeses")
+
+# A couple of notes about list controls and HTML:
+
+# 1. List controls correspond to either a single SELECT element, or
+# multiple INPUT elements.  Items correspond to either OPTION or INPUT
+# elements.  For example, this is a SELECT control, named "control1":
+
+#    <select name="control1">
+#     <option>foo</option>
+#     <option value="1">bar</option>
+#    </select>
+
+# and this is a CHECKBOX control, named "control2":
+
+#    <input type="checkbox" name="control2" value="foo" id="cbe1">
+#    <input type="checkbox" name="control2" value="bar" id="cbe2">
+
+# You know the latter is a single control because all the name attributes
+# are the same.
+
+# 2. Item names are the strings that go to make up the value that should
+# be returned to the server.  These strings come from various different
+# pieces of text in the HTML.  The HTML standard and the ClientForm
+# docstrings explain in detail, but playing around with an HTML file,
+# ParseFile() and 'print form' is very useful to understand this!
+
+# You can also get the Control instances from inside the form...
+control = form.find_control("cheeses", type="select")
+print control.name, control.value, control.type
+control.value = ["mascarpone", "curd"]
+# ...and the Item instances from inside the Control
+item = control.get("curd")
+print item.name, item.selected, item.id, item.attrs
+item.selected = False
+
+# HTMLForm.controls is a list of all controls in the form
+for control in form.controls:
+    if control.value == "inquisition": sys.exit()
+
+# Control.items is a list of all Item instances in the control
+for item in form.find_control("cheeses").items:
+    print item.name
+
+# Some list control examples:
+# Many methods have a by_label argument, allowing specification of list
+# items by label instead of by name.  Sometimes labels are
+# easier to maintain than names, sometimes the other way around.
+form.set_value(["Mozzarella", "Caerphilly"], "cheeses", by_label=True)
 #  is the "parmesan" item of the "cheeses" control selected?
+print 1
 print "parmesan" in form["cheeses"]
 #  does cheeses control have a "caerphilly" item?
-print "caerphilly" in form.possible_items("cheeses")
-
-# Sometimes one wants to set or clear individual items in a list:
+print "caerphilly" in [item.name for item in form.find_control("cheeses").items]
+# Sometimes one wants to set or clear individual items in a list, rather
+# than setting the whole .value:
 #  select the item named "gorgonzola" in the first control named "cheeses"
-form.set(True, "gorgonzola", "cheeses")
-# You can be more specific: supply at least one of name, type, kind, id
-# and nr (most other methods on HTMLForm take the same form of arguments):
+form.find_control("cheeses").get("gorgonzola").selected = True
+# You can be more specific (and many other methods take similar arguments):
 #  deselect "edam" in third CHECKBOX control
-form.set(False, "edam", type="checkbox", nr=2)
+form.find_control(type="checkbox", nr=2).get("edam").selected = False
+#  deselect item labelled "Mozzarella" in control with id "chz"
+form.find_control(id="chz").get("Mozzarella", by_label=True).selected = False
+# As for list items, controls may also be referred to by label:
+#  select "edam" in control with label that has a *substring* "Cheeses"
+#  (eg., a label "Please select a cheese" would match).
+form.find_control(label="select a cheese").get("emmenthal").selected = True
 
 # You can explicitly say that you're referring to a ListControl:
 #  set whole value (rather than just one item of) "cheeses" ListControl
@@ -48,12 +107,19 @@
 form.set_value("rhubarb rhubarb", kind="text")
 form.set_value(["spam"], kind="singlelist")
 
+# You can find controls with a general predicate function:
+#  find first control with attribute named "whatever"
+def control_has_caerphilly(control):
+    for item in control.items:
+        if item.name == "caerphilly": return True
+form.find_control(kind="list", predicate=control_has_caerphilly)
+
 # Often, a single checkbox (a CHECKBOX control with a single item) is
 # present.  In that case, the name of the single item isn't of much
-# interest, so it's useful to be able to check and uncheck the box
-# without using the item name:
-form.set_single(True, "smelly")  # check
-form.set_single(False, "smelly")  # uncheck
+# interest, so it's a good idea to check and uncheck the box without
+# using the item name:
+form.find_control("smelly").items[0].selected = True  # check
+form.find_control("smelly").items[0].selected = False  # uncheck
 
 # Add files to FILE controls with .add_file().  Only call this multiple
 # times if the server is expecting multiple files.
@@ -63,22 +129,6 @@
 #   what the filename is
 #form.add_file(open("data.txt"), "text/plain", "data.txt")
 
-# Many methods have a by_label argument, allowing specification of list
-# items by label instead of by name.  At the moment, only SelectControl
-# supports this argument (this will be fixed).  Sometimes labels are
-# easier to maintain than names, sometimes the other way around.
-form.set_value(["Mozzarella", "Caerphilly"], "cheeses", by_label=True)
-
-# It's also possible to get at the individual controls inside the form.
-# This is useful for calling several methods in a row on a single control,
-# and for the less common operations.  The methods are quite similar to
-# those on HTMLForm:
-control = form.find_control("cheeses", type="select")
-print control.value, control.name, control.type
-print control.possible_items()
-control.value = ["mascarpone", "curd"]
-control.set(True, "limburger")
-
 # All Controls may be disabled (equivalent of greyed-out in browser)
 control = form.find_control("comments")
 print control.disabled
@@ -89,18 +139,14 @@
 # convenience method, used here to make all controls writable (unless
 # they're disabled):
 form.set_all_readonly(False)
-# ListControl items may also be disabled (setting a disabled item is not
-# allowed, but clearing one is allowed):
+# Items may also be disabled (selecting or de-selecting a disabled item is
+# not allowed):
 control = form.find_control("cheeses")
-print control.get_item_disabled("emmenthal")
-control.set_item_disabled(True, "emmenthal")
+print control.get("emmenthal").disabled
+control.get("emmenthal").disabled = True
 #  enable all items in control
 control.set_all_items_disabled(False)
 
-# HTMLForm.controls is a list of all controls in the form
-for control in form.controls:
-    if control.value == "inquisition": sys.exit()
-
 request2 = form.click()  # urllib2.Request object
 try:
     response2 = urllib2.urlopen(request2)


More information about the wwwsearch-commits mailing list