[wwwsearch-commits] r17828 - wwwsearch/ClientForm/trunk
jjlee at codespeak.net
jjlee at codespeak.net
Sat Sep 24 17:12:00 CEST 2005
Author: jjlee
Date: Sat Sep 24 17:11:59 2005
New Revision: 17828
Modified:
wwwsearch/ClientForm/trunk/ClientForm.py
Log:
Add label arguments to HTMLForm.clear(), .add_file(), .set_value() and .get_value()
Modified: wwwsearch/ClientForm/trunk/ClientForm.py
==============================================================================
--- wwwsearch/ClientForm/trunk/ClientForm.py (original)
+++ wwwsearch/ClientForm/trunk/ClientForm.py Sat Sep 24 17:11:59 2005
@@ -927,20 +927,17 @@
class Control:
"""An HTML form control.
- An HTMLForm contains a sequence of Controls. HTMLForm delegates lots of
- things to Control objects, and most of Control's methods are, in effect,
- documented by the HTMLForm docstrings.
-
- The Controls in an HTMLForm can be got at via the HTMLForm.find_control
- method or the HTMLForm.controls attribute.
+ An HTMLForm contains a sequence of Controls. The Controls in an HTMLForm
+ are accessed using the HTMLForm.find_control method or the
+ HTMLForm.controls attribute.
Control instances are usually constructed using the ParseFile /
- ParseResponse functions, so you can probably ignore the rest of this
- paragraph. A Control is only properly initialised after the fixup method
- has been called. In fact, this is only strictly necessary for ListControl
- instances. This is necessary because ListControls are built up from
- ListControls each containing only a single item, and their initial value(s)
- can only be known after the sequence is complete.
+ ParseResponse functions. If you use those functions, you can ignore the
+ rest of this paragraph. A Control is only properly initialised after the
+ fixup method has been called. In fact, this is only strictly necessary for
+ ListControl instances. This is necessary because ListControls are built up
+ from ListControls each containing only a single item, and their initial
+ value(s) can only be known after the sequence is complete.
The types and values that are acceptable for assignment to the value
attribute are defined by subclasses.
@@ -2209,7 +2206,7 @@
form = forms[0]
form["query"] = "Python"
- form.set("lots", "nr_results")
+ form.find_control("nr_results").get("lots").selected = True
response = urllib2.urlopen(form.click())
@@ -2248,9 +2245,10 @@
named "1" and "2" (because the OPTION element's value HTML attribute
defaults to the element contents).
- To set, clear or toggle individual list items, use the set and toggle
- methods. To set the whole value, do as for any other control:use indexing
- or the set_/get_value methods.
+ To select, deselect or otherwise manipulate individual list items, use the
+ HTMLForm.find_control() and ListControl.get() methods. To set the whole
+ value, do as for any other control:use indexing or the set_/get_value
+ methods.
Example:
@@ -2273,7 +2271,7 @@
To check whether a control has an item, or whether an item is selected,
respectively:
- "cheddar" in form.possible_items("cheeses")
+ "cheddar" in [item.name for item in form.find_control("cheeses").items]
"cheddar" in form["cheeses"] # (or "cheddar" in form.get_value("cheeses"))
Note that some list items may be disabled (see below).
@@ -2287,11 +2285,9 @@
in the order they were listed in the HTML.
List items (hence list values, too) can be referred to in terms of list
- item labels rather than list item names. Currently, this is only possible
- for SELECT controls (this is a bug). To use this feature, use the by_label
- arguments to the various HTMLForm methods. Note that it is *item* names
- (hence ListControl values also), not *control* names, that can be referred
- to by label.
+ item labels rather than list item names. To use this feature, use the
+ by_label arguments to the various HTMLForm methods. Note that each item
+ may have several labels.
The question of default values of OPTION contents, labels and values is
somewhat complicated: see SelectControl.__doc__ and
@@ -2316,21 +2312,17 @@
form.clear("cheeses")
- When you want to do several things with a single control, or want to do
- less common things, like changing which controls and items are disabled,
- you can get at a particular control:
+ More examples:
control = form.find_control("cheeses")
control.disabled = False
control.readonly = False
- control.set_item_disabled(False, "gruyere")
- control.set("gruyere")
+ control.get("gruyere").disabled = True
+ control.items[0].selected = True
- Most methods on HTMLForm just delegate to the contained controls, so see
- the docstrings of the various Control classes for further documentation.
- Most of these delegating methods take name, type, kind, id and nr arguments
- to specify the control to be operated on: see
- HTMLForm.find_control.__doc__.
+ See the various Control classes for further documentation. Many methods
+ take name, type, kind, id, label and nr arguments to specify the control to
+ be operated on: see HTMLForm.find_control.__doc__.
ControlNotFoundError (subclass of ValueError) is raised if the specified
control can't be found. This includes occasions where a non-ListControl
@@ -2367,53 +2359,41 @@
-------------------------
Most of the these methods have very similar arguments. See
- HTMLForm.find_control.__doc__ for details of the name, type, kind and nr
- arguments. See above for a description of by_label.
+ HTMLForm.find_control.__doc__ for details of the name, type, kind, label
+ and nr arguments. See above for a description of by_label. Note that
+ by_label refers to *item* labels; label refers to *control* labels (the
+ naming and the odd position of 'label' in argument lists is unfortunate,
+ but necessary for backwards-compatibility with version 0.1).
def find_control(self,
name=None, type=None, kind=None, id=None, predicate=None,
- nr=None)
+ nr=None, label=None)
get_value(name=None, type=None, kind=None, id=None, nr=None,
- by_label=False)
+ by_label=False, label=None)
set_value(value,
name=None, type=None, kind=None, id=None, nr=None,
- by_label=False)
-
- set_all_readonly(readonly)
-
+ by_label=False, label=None)
- Methods applying only to ListControls:
+ clear_all()
+ clear(name=None, type=None, kind=None, id=None, nr=None, label=None)
- possible_items(name=None, type=None, kind=None, id=None, nr=None,
- by_label=False)
-
- set(selected, item_name,
- name=None, type=None, kind=None, id=None, nr=None,
- by_label=False)
- toggle(item_name,
- name=None, type=None, id=None, nr=None,
- by_label=False)
-
- set_single(selected,
- name=None, type=None, kind=None, id=None, nr=None,
- by_label=False)
- toggle_single(name=None, type=None, kind=None, id=None, nr=None,
- by_label=False)
+ set_all_readonly(readonly)
Method applying only to FileControls:
add_file(file_object,
content_type="application/octet-stream", filename=None,
- name=None, id=None, nr=None)
+ name=None, id=None, nr=None, label=None)
Methods applying only to clickable controls:
- click(name=None, type=None, id=None, nr=0, coord=(1,1))
- click_request_data(name=None, type=None, id=None, nr=0, coord=(1,1))
- click_pairs(name=None, type=None, id=None, nr=0, coord=(1,1))
+ click(name=None, type=None, id=None, nr=0, coord=(1,1), label=None)
+ click_request_data(name=None, type=None, id=None, nr=0, coord=(1,1),
+ label=None)
+ click_pairs(name=None, type=None, id=None, nr=0, coord=(1,1), label=None)
"""
@@ -2547,7 +2527,8 @@
def get_value(self,
name=None, type=None, kind=None, id=None, nr=None,
- by_label=False):
+ by_label=False,
+ label=None):
"""Return value of control.
If only name and value arguments are supplied, equivalent to
@@ -2555,7 +2536,7 @@
form[name]
"""
- c = self.find_control(name, type, kind, id, nr=nr)
+ c = self.find_control(name, type, kind, id, label, nr=nr)
if by_label:
try:
meth = c.get_value_by_label
@@ -2568,7 +2549,8 @@
return c.value
def set_value(self, value,
name=None, type=None, kind=None, id=None, nr=None,
- by_label=False):
+ by_label=False,
+ label=None):
"""Set value of control.
If only name and value arguments are supplied, equivalent to
@@ -2576,7 +2558,7 @@
form[name] = value
"""
- c = self.find_control(name, type, kind, id, nr=nr)
+ c = self.find_control(name, type, kind, id, label, nr=nr)
if by_label:
try:
meth = c.set_value_by_label
@@ -2602,14 +2584,14 @@
control.clear()
def clear(self,
- name=None, type=None, kind=None, id=None, nr=None):
- """Clear the value attributes of all controls in the form.
+ name=None, type=None, kind=None, id=None, nr=None, label=None):
+ """Clear the value attribute of a control.
- As a result, the affected controls will not be successful until a value
+ As a result, the affected control will not be successful until a value
is subsequently set. AttributeError is raised on readonly controls.
"""
- c = self.find_control(name, type, kind, id, nr=nr)
+ c = self.find_control(name, type, kind, id, label, nr=nr)
c.clear()
More information about the wwwsearch-commits
mailing list