File: python-api.txt

package info (click to toggle)
twill 0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,472 kB
  • ctags: 1,916
  • sloc: python: 12,686; java: 98; makefile: 18; sh: 2
file content (149 lines) | stat: -rw-r--r-- 4,646 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
==================
twill's Python API
==================

Using TwillBrowser
Making extensions

twill is essentially a thin shell around the mechanize_ package.  All
twill commands are implemented in the ``commands.py`` file, and
pyparsing_ does the work of parsing the input and converting it into
Python commands (see ``parse.py``).  Interactive shell work and
readline support is implemented via the `cmd`_ module (from the
standard Python library).

Using twill from Python
~~~~~~~~~~~~~~~~~~~~~~~

There are two fairly simple ways to use twill from Python.  (They are
compatible with each other, so you don't need to choose between them;
just use whichever is appropriate.)

The first is to simply import all of the commands in ``commands.py`` and
use them directly from Python.  For example, ::

   from twill.commands import *
   go("http://www.python.org/")
   showforms()

This has the advantage of being very simple, as well as being tied
directly to the documented set of commands in `the commands
reference`_.

However, the functions in ``commands.py`` are too simple for some situations.
In particular, they do not have any return values, so in order to e.g. get
the HTML for a particular page, you will need to talk to the actual "Web
browser" object that twill uses.

To talk to the Web browser directly, call the ``get_browser`` function: ::

   from twill import get_browser
   b = get_browser()

   b.go("http://www.python.org/")
   b.showforms()

This is the second way to use twill from Python, and it is much more flexible.
All of the functions in ``commands.py`` use this same browser object, so
you can mix and match: ::

   from twill import get_browser
   b = get_browser()

   from twill.commands import *
   go("http://www.python.org/")
   b.showforms()

The basic difference is that functions available through the browser object
are intended for use from Python, while ``commands.py`` functions define
the twill *language*.  In fact, all of the functions in ``commands.py``
are small snippets of code wrapped around the browser object.

For more information on the functions exposed by the browser object,
see the **TwillBrowser** section below.

.. _the commands reference: commands.html

TwillBrowser
~~~~~~~~~~~~

...

Extending twill
~~~~~~~~~~~~~~~

Right now twill is very easy to extend: just build a Python module
that exports the functions you want to call, place it in the
PYTHONPATH, and run ``extend_with <modulename>``.  See
``extensions/mailman_sf.py`` for an extension that helps deal
with mailman lists on SourceForge; this extension is used by
``examples/discard-sf-mailman-msgs``.

Notes:

  * If your extension raises ``SystemExit``, twill will stop
    processing the script.  This is a useful way to build in
    conditionals, e.g. see the ``discard-sf-mailman-msgs`` example
    script.

Passing variables from Python into twill
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Suppose you write an extension function, and you want to return some
values from that extension function for later use in filling out forms.
How do you do this?

The simplest way is to simply insert them into the global or local
dictionary, like so: ::

  from twill.namespaces import get_twill_glocals
  global_dict, local_dict = get_twill_glocals()

  global_dict['varname'] = value

You can then use the usual variable access methods to substitute for
varname, e.g. ::

  formvalue 1 field $varname

Using twill in other Python programs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All of the commands available in twill are implemented as top-level functions
in the `twill.commands` module.  For example, to use twill functionality from
another Python program, you can do:

::

   from twill.commands import go, showforms, formclear, fv, submit

   go('http://issola.caltech.edu/~t/qwsgi/qwsgi-demo.cgi/')
   go('./widgets')
   showforms()

   formclear('1')
   fv("1", "name", "test")
   fv("1", "password", "testpass")
   fv("1", "confirm", "yes")
   showforms()

   submit('0')

Note that all arguments need to be strings, at least for the moment.

You can capture command output by passing any write-enabled file handle
to twill.set_output, e.g. ::

   twill.set_output(StringIO())

will send all non-error output into a StringIO() object.

twill also provides a simple wrapper for mechanize_ functionality, in
the `browser.py` module.  This may be useful for twill extensions as
well as for other toolkits, but the API is still unstable.

.. _mechanize: http://wwwsearch.sf.net/
.. _pyparsing: http://pyparsing.sourceforge.net/
.. _darcs: http://abridgegame.org/darcs/

.. _cmd: http://docs.python.org/lib/module-cmd.html