Commit 9fa26033 authored by Florian Hatat's avatar Florian Hatat Committed by ntome

Add imports in examples from module.rst

This allows someone reading the doc to quickly use an example from the
doc in its own code without spending too much time figuring out which
imports are required.
parent faf22fe4
...@@ -111,6 +111,9 @@ Module class ...@@ -111,6 +111,9 @@ Module class
Edit ``module.py``. It contains the main class of the module derived from :class:`Module <weboob.tools.backend.Module>` class:: Edit ``module.py``. It contains the main class of the module derived from :class:`Module <weboob.tools.backend.Module>` class::
from weboob.tools.backend import Module
from weboob.capabilities.bank import CapBank
class ExampleModule(Module, CapBank): class ExampleModule(Module, CapBank):
NAME = 'example' # The name of module NAME = 'example' # The name of module
DESCRIPTION = u'Example bank website' # Description of your module DESCRIPTION = u'Example bank website' # Description of your module
...@@ -153,8 +156,9 @@ Available parameters of :class:`Value <weboob.tools.value.Value>` are: ...@@ -153,8 +156,9 @@ Available parameters of :class:`Value <weboob.tools.value.Value>` are:
For example:: For example::
from weboob.tools.backend import Module, BackendConfig
from weboob.capabilities.bank import CapBank
from weboob.tools.value import Value, ValueBool, ValueInt, ValueBackendPassword from weboob.tools.value import Value, ValueBool, ValueInt, ValueBackendPassword
from weboob.tools.backend import BackendConfig
# ... # ...
class ExampleModule(Module, CapBank): class ExampleModule(Module, CapBank):
...@@ -173,6 +177,9 @@ Implement capabilities ...@@ -173,6 +177,9 @@ Implement capabilities
You need to implement each method of all of the capabilities your module implements. For example, in our case:: You need to implement each method of all of the capabilities your module implements. For example, in our case::
from weboob.tools.backend import Module
from weboob.capabilities.bank import CapBank
# ... # ...
class ExampleModule(Module, CapBank): class ExampleModule(Module, CapBank):
# ... # ...
...@@ -274,7 +281,9 @@ Each time you will go on the home page, ``IndexPage`` will be instanced and set ...@@ -274,7 +281,9 @@ Each time you will go on the home page, ``IndexPage`` will be instanced and set
For example, we can now implement some methods in ``ExampleBrowser``:: For example, we can now implement some methods in ``ExampleBrowser``::
class ExampleBrowser(PagesBrowserr): from weboob.browser import PagesBrowser
class ExampleBrowser(PagesBrowser):
# ... # ...
def go_home(self): def go_home(self):
self.home.go() self.home.go()
...@@ -297,6 +306,9 @@ Use it in backend ...@@ -297,6 +306,9 @@ Use it in backend
Now you have a functional browser, you can use it in your class ``ExampleModule`` by defining it with the ``BROWSER`` attribute:: Now you have a functional browser, you can use it in your class ``ExampleModule`` by defining it with the ``BROWSER`` attribute::
from weboob.tools.backend import Module
from weboob.capabilities.bank import CapBank
from .browser import ExampleBrowser from .browser import ExampleBrowser
# ... # ...
...@@ -319,6 +331,9 @@ Login management ...@@ -319,6 +331,9 @@ Login management
When the website requires to be authenticated, you have to give credentials to the constructor of the browser. You can redefine When the website requires to be authenticated, you have to give credentials to the constructor of the browser. You can redefine
the method :func:`create_default_browser <weboob.tools.backend.Module.create_default_browser>`:: the method :func:`create_default_browser <weboob.tools.backend.Module.create_default_browser>`::
from weboob.tools.backend import Module
from weboob.capabilities.bank import CapBank
class ExampleModule(Module, CapBank): class ExampleModule(Module, CapBank):
# ... # ...
def create_default_browser(self): def create_default_browser(self):
...@@ -327,6 +342,9 @@ the method :func:`create_default_browser <weboob.tools.backend.Module.create_def ...@@ -327,6 +342,9 @@ the method :func:`create_default_browser <weboob.tools.backend.Module.create_def
On the browser side, you need to inherit from :func:`LoginBrowser <weboob.browser.browsers.LoginBrowser>` and to implement the function On the browser side, you need to inherit from :func:`LoginBrowser <weboob.browser.browsers.LoginBrowser>` and to implement the function
:func:`do_login <weboob.browser.browsers.LoginBrowser.do_login>`:: :func:`do_login <weboob.browser.browsers.LoginBrowser.do_login>`::
from weboob.browser import LoginBrowser
from weboob.exceptions import BrowserIncorrectPassword
class ExampleBrowser(LoginBrowser): class ExampleBrowser(LoginBrowser):
login = URL('/login', LoginPage) login = URL('/login', LoginPage)
# ... # ...
...@@ -343,6 +361,8 @@ You may provide a custom :func:`do_logout <weboob.browser.browsers.LoginBrowser. ...@@ -343,6 +361,8 @@ You may provide a custom :func:`do_logout <weboob.browser.browsers.LoginBrowser.
Also, your ``LoginPage`` may look like:: Also, your ``LoginPage`` may look like::
from weboob.browser.pages import HTMLPage
class LoginPage(HTMLPage): class LoginPage(HTMLPage):
def login(self, username, password): def login(self, username, password):
form = self.get_form(name='auth') form = self.get_form(name='auth')
...@@ -352,6 +372,9 @@ Also, your ``LoginPage`` may look like:: ...@@ -352,6 +372,9 @@ Also, your ``LoginPage`` may look like::
Then, each method on your browser which needs your user to be authenticated may be decorated by :func:`need_login <weboob.browser.browsers.need_login>`:: Then, each method on your browser which needs your user to be authenticated may be decorated by :func:`need_login <weboob.browser.browsers.need_login>`::
from weboob.browser import LoginBrowser, URL
from weboob.browser import need_login
class ExampleBrowser(LoginBrowser): class ExampleBrowser(LoginBrowser):
accounts = URL('/accounts$', ListPage) accounts = URL('/accounts$', ListPage)
...@@ -368,6 +391,7 @@ decorated method. ...@@ -368,6 +391,7 @@ decorated method.
You can either define it yourself, as a class boolean attribute or as a property, or inherit your class from :class:`LoggedPage <weboob.browser.pages.LoggedPage>`. You can either define it yourself, as a class boolean attribute or as a property, or inherit your class from :class:`LoggedPage <weboob.browser.pages.LoggedPage>`.
In the latter case, remember that Python inheritance requires the :class:`LoggedPage <weboob.browser.pages.LoggedPage>` to be placed first such as in:: In the latter case, remember that Python inheritance requires the :class:`LoggedPage <weboob.browser.pages.LoggedPage>` to be placed first such as in::
from weboob.browser.pages import LoggedPage, HTMLPage
class OnlyForLoggedUserPage(LoggedPage, HTMLPage): class OnlyForLoggedUserPage(LoggedPage, HTMLPage):
# ... # ...
...@@ -393,9 +417,11 @@ construct the object. ...@@ -393,9 +417,11 @@ construct the object.
For example:: For example::
from weboob.browser.pages import LoggedPage, HTMLPage
from weboob.browser.filters.html import Attr from weboob.browser.filters.html import Attr
from weboob.browser.filters.standard import CleanDecimal, CleanText from weboob.browser.filters.standard import CleanDecimal, CleanText
from weboob.capabilities.bank import Account from weboob.capabilities.bank import Account
from weboob.browser.elements import method, ListElement, ItemElement
class ListPage(LoggedPage, HTMLPage): class ListPage(LoggedPage, HTMLPage):
@method @method
...@@ -529,6 +555,9 @@ method associated to the type of the object. ...@@ -529,6 +555,9 @@ method associated to the type of the object.
To define what objects are supported to be filled, and what method to call, define the ``OBJECTS`` To define what objects are supported to be filled, and what method to call, define the ``OBJECTS``
class attribute in your ``ExampleModule``:: class attribute in your ``ExampleModule``::
from weboob.tools.backend import Module
from weboob.capabilities.video import CapVideo
class ExampleModule(Module, CapVideo): class ExampleModule(Module, CapVideo):
# ... # ...
...@@ -540,6 +569,9 @@ The prototype of the function might be:: ...@@ -540,6 +569,9 @@ The prototype of the function might be::
Then, the function might, for each requested fields, fetch the right data and fill the object. For example:: Then, the function might, for each requested fields, fetch the right data and fill the object. For example::
from weboob.tools.backend import Module
from weboob.capabilities.video import CapVideo
class ExampleModule(Module, CapVideo): class ExampleModule(Module, CapVideo):
# ... # ...
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment