mò mã¸Ec@sdZdkZdkZdkZdkZdkZdklZdkl Z dk l Z l Z dk Z dZd efd „ƒYZd efd „ƒYZd efd„ƒYZdfd„ƒYZd„Zdeifd„ƒYZdefd„ƒYZdS(sUStateful programmatic WWW navigation, after Perl's WWW::Mechanize. Copyright 2003-2006 John J. Lee Copyright 2003 Andy Lester (original Perl code) This code is free software; you can redistribute it and/or modify it under the terms of the BSD or ZPL 2.1 licenses (see the file COPYING.txt included with the distribution). N(s UserAgent(sDefaultFactory(sresponse_seek_wrapperscloseable_responseiiitbtBrowserStateErrorcBstZRS(N(t__name__t __module__(((t3/data/zmath/zope/lib/python/mechanize/_mechanize.pyRstLinkNotFoundErrorcBstZRS(N(RR(((RRstFormNotFoundErrorcBstZRS(N(RR(((RRstHistorycBs;tZdZd„Zd„Zd„Zd„Zd„ZRS(sT Though this will become public, the implied interface is not yet stable. cCs g|_dS(N(tselft_history(R((Rt__init__ scCs|ii||fƒdS(N(RR tappendtrequesttresponse(RR R ((Rtadd"scCsw|}xd|djp |djoIy|iiƒ\}}Wntj ot dƒ‚nX|d8}q W||fS(Nisalready at start of historyi( t _responseR tntNoneRR tpopR t IndexErrorR(RRRR R ((Rtback$scCs |i2dS(N(RR (R((Rtclear-scCs@x2|iD]'\}}|dj o|iƒq q W|i2dS(sb If nothing has been added, .close should work. >>> history = History() >>> history.close() Under some circumstances response can be None, in that case this method should not raise an exception. >>> history.add(None, None) >>> history.close() N(RR R R Rtclose(RR R ((RR/s   (RRt__doc__R RRRR(((RRs    cCsÍy |i}Wntj o d}nXy |i}Wntj o d}nXd}t|ddƒ}|o |ƒ}nt|i |i ƒ|i ƒ||ƒ}t |ƒ}|o|i |ƒn|S(Ntget_data(R tcodetAttributeErrorRtmsgtdatatgetattrRtcloseable_responsetfptinfotgeturltresponse_seek_wrappertset_data(R RRRR((Rtupgrade_responseCs"     ' tResponseUpgradeProcessorcBstZdZd„ZRS(NicCs$t|dƒpt|ƒ}n|S(NR(thasattrR R$(RR R ((Rt any_response_s(RRt handler_orderR'(((RR%\stBrowserc BsitZdZeiiƒZeedt( ttextR Rt __class__RRR!R-tstrtjoin(RRL((Rt__str__Þs    cCsti|iƒS(s¦Return a copy of the current response. The returned object has the same interface as the object returned by .open() (or urllib2.urlopen()). N(RIRR(R((RR êscCs¸t|dƒot|dƒo t|dƒptdƒ‚nd|_t|dƒpt|ƒ}nt|dƒpt|ƒ}nti|ƒ}||_ |i i |i ƒdS(s3Replace current response with (a copy of) response.R R!treadsnot a response objecttseekRN( R&R t ValueErrorRRR-R"R$RIRR5RH(RR ((RRHós0  cCs-|idjotdƒ‚n|iiƒS(sGet URL of current document.snot viewing any documentN(RRRRR!(R((RR!scCsW|idjotdƒ‚n|idj o|iiƒn|i|idtƒS(s4Reload current document, and return response object.sno URL has yet been .open()edR@N(RR RRRRR7RG(R((Rtreload s icCsS|idj o|iiƒn|ii||iƒ\|_}|i |ƒ|S(s{Go back n steps in history, and return response object. n: go back this number of steps (default 1 step) N( RRRRR RRR R RH(RRR ((RRs ! cCs|iiƒdS(N(RR R(R((Rt clear_history scKsO|iƒptdƒ‚n|iiƒ}|o|i||Sn|SdS(s4Return iterable over links (mechanize.Link objects).snot viewing HTMLN(Rt viewing_htmlRR5tlinkstkwdst _filter_links(RRXRW((RRW#s cCs*|iƒptdƒ‚n|iiƒS(suReturn iterable over forms. The returned form objects implement the ClientForm.HTMLForm interface. snot viewing HTMLN(RRVRR5tforms(R((RRZ-s cCs*|idjotdƒ‚n|iiS(s7Return whether the current response contains HTML data.snot viewing any documentN(RRRRR5tis_html(R((RRV7scCs*|idjotdƒ‚n|iiS(RKsnot viewing any documentN(RRRRR5tencoding(R((RR\=scCs'|iƒptdƒ‚n|iiS(sÉReturn title, or None if there is no title element in the document. Tags are stripped or textified as described in docs for PullParser.get_text() method of pullparser module. snot viewing HTMLN(RRVRR5ttitle(R((RR]Cs cCsh|iƒptdƒ‚n|d jo*|d jo|d jotdƒ‚n|}x|i ƒD]n}|d j o||ijoqgn|d j o||ƒ oqgn|o|d8}qgn||_ PqgWg}|d j o|i d|ƒn|d j o|i d|ƒn|d j o|i d|ƒndi |ƒ}td|ƒ‚d S( sâSelect an HTML form for input. This is a bit like giving a form the "input focus" in a browser. If a form is selected, the Browser object supports the HTMLForm interface, so you can call methods like .set_value(), .set(), and .click(). At least one of the name, predicate and nr arguments must be supplied. If no matching form is found, mechanize.FormNotFoundError is raised. If name is specified, then the form must have the indicated name. If predicate is specified, then the form must match that function. The predicate function is passed the HTMLForm as its single argument, and should return a boolean value indicating whether the form matched. nr, if supplied, is the sequence number of the form (where 0 is the first). Note that control 0 is the first form matching all the other arguments (if supplied); it is not necessarily the first control in the form. snot viewing HTMLs6at least one argument must be supplied to specify formis name '%s's predicate %ssnr %ds, sno form matching N(RRVRtnameRt predicatetnrRStorig_nrRZR-t descriptionR ROR(RR^R_R`R-RaRb((Rt select_formNs4 '      cCs÷|idjo|Sn|iƒ}|iiƒ}|ddgjo|Sn| o|iidƒ o|Sn|iow|ddgjod|djo |dj oIt i |ii ƒƒ}|d d}t i |ƒ}|id|ƒn|S(NthttpthttpstRefereriÿÿÿÿRK(RK(RR RRAR<toriginal_schemetorigin_requestt has_headert_handle_refererR;R:tpartst urlunparsetrefererR+(RR RhRgRkRmR<((Rt_add_referer_headers 8cOs?|iƒptdƒ‚n|ii||Ž}|i|ƒS(s0See ClientForm.HTMLForm.click for documentation.snot viewing HTMLN( RRVRR-tclicktargsRXR Rn(RRpRXR ((RRo•s  cOs|i|i||ŽƒS(sŽSubmit current form. Arguments are as for ClientForm.HTMLForm.click(). Return value is same as for Browser.open(). N(RR9RoRpRX(RRpRX((RtsubmitœscKsm|iƒptdƒ‚n|p|i|}n|otdƒ‚n|i|iƒ}|i |ƒS(s§Find a link and return a Request object for it. Arguments are as for .find_link(), except that a link may be supplied as the first argument. snot viewing HTMLs2either pass a Link, or keyword arguments, not bothN( RRVRtlinkt find_linkRXRSR.t absolute_urlR Rn(RRrRXR ((Rt click_link¦s cKs|i|i||ƒS(s‡Find a link and .open() it. Arguments are as for .click_link(). Return value is same as for Browser.open(). N(RR9RuRrRX(RRrRX((Rt follow_link¸scKsFy#|i|iiƒ|iƒSWntj otƒ‚nXdS(s©Find a link in current page. Links are returned as mechanize.Link objects. # Return third link that .search()-matches the regexp "python" # (by ".search()-matches", I mean that the regular expression method # .search() is used, rather than .match()). find_link(text_regex=re.compile("python"), nr=2) # Return first http link in the current page that points to somewhere # on python.org whose link text (after tags have been removed) is # exactly "monty python". find_link(text="monty python", url_regex=re.compile("http.*python.org")) # Return first link with exactly three HTML attributes. find_link(predicate=lambda link: len(link.attrs) == 3) Links include anchors (), image maps (), and frames (,