1.2.2.3. redirect – Base Plugin Class for Handling Redirections

class lib.browser.plugins.redirect.BaseRedirectionPlugin(url_match=None, page_match=None, parser=<function passthrough_str at 0x3bf31e8>)

Provides a simple framework for handling pages that require a redirection with a hope to reduce the necessity for so much boilerplate code. You don’t have to extend this class to handle redirections, but it might make things easier.

__init__(url_match=None, page_match=None, parser=<function passthrough_str at 0x3bf31e8>)

Keyword arguments:

url_match
A string, regex object (using match), or callable used to check if a page url can potentially be a page that needs redirection.
page_match
A string, regex object (using match), or callable used to check if a page source (decoded by parser) can potentially be a page that needs redirection.
parser
A parser function that should be applied to the page data before sending it to handle_redirect().
_is_valid_url(url)

One can override this function as an alternative to providing a url_match value via the constructor. If neither is specified, this just returns True.

_is_valid_page(source)

One can override this function as an alternative to providing a page_match value via the constructor. If neither is specified, this just returns True.

load_page(plugin, browser, base_function, url, *args, **kwargs)

Calls handle_redirect() if there is both a url and page match, otherwise, it simply passes through.

handle_redirect(plugin, browser, url, source, *args, **kwargs)

Should return the value given by browser.load_page with the additional arguments, *args and **kwargs (modified if desired). If it returns None or raises a PageRedirectionError, the redirect is canceled. Here’s an example handle_redirect method:

def handle_redirect(plugin, browser, url, parsed, *args, **kwargs):
    "For this example, we'll say the plugin's parser is lxml"
    return browser.load_page(parsed.xpath("//a")[0].attrib["href"],
                             *args, **kwargs)
handle_redirect(plugin, browser, url, source, *args, **kwargs)

Should return the value given by browser.load_page with the additional arguments, *args and **kwargs (modified if desired). If it returns None or raises a PageRedirectionError, the redirect is canceled. Here’s an example handle_redirect method:

def handle_redirect(plugin, browser, url, parsed, *args, **kwargs):
    "For this example, we'll say the plugin's parser is lxml"
    return browser.load_page(parsed.xpath("//a")[0].attrib["href"],
                             *args, **kwargs)
load_page(plugin, browser, base_function, url, *args, **kwargs)

Calls handle_redirect() if there is both a url and page match, otherwise, it simply passes through.

class lib.browser.plugins.redirect.BrowserMetaRefreshHander(max_seconds=None)

Handles pages using the deprecated html meta refresh feature.

__init__(max_seconds=None)
handle_redirect(plugin, browser, base_url, source, *args, **kwargs)
class lib.browser.plugins.redirect.PageRedirectionError
__init__()

Previous topic

1.2.2.2. decorators – Decorators for Writing Plugins and Extending Pluggable

Next topic

1.2.2.4. cookies – Automatic Handling of Browser Cookies

This Page