woob.capabilities.base

exception UserError[source]

Bases: Exception

Exception containing an error message for user.

exception FieldNotFound(obj, field)[source]

Bases: Exception

A field isn’t found.

Parameters
class Capability[source]

Bases: object

This is the base class for all capabilities.

A capability may define abstract methods (which raise NotImplementedError) with an explicit docstring to tell backends how to implement them.

Also, it may define some objects, using BaseObject.

class Field(doc, *args, **kwargs)[source]

Bases: object

Field of a BaseObject class.

Parameters
  • doc (str) – docstring of the field

  • args – list of types accepted

  • default – default value of this field. If not specified, NotLoaded is used.

convert(value)[source]

Convert value to the wanted one.

class IntField(doc, **kwargs)[source]

Bases: Field

A field which accepts only int types.

convert(value)[source]

Convert value to the wanted one.

class DecimalField(doc, **kwargs)[source]

Bases: Field

A field which accepts only decimal type.

convert(value)[source]

Convert value to the wanted one.

class FloatField(doc, **kwargs)[source]

Bases: Field

A field which accepts only float type.

convert(value)[source]

Convert value to the wanted one.

class StringField(doc, **kwargs)[source]

Bases: Field

A field which accepts only str strings.

convert(value)[source]

Convert value to the wanted one.

class BytesField(doc, **kwargs)[source]

Bases: Field

A field which accepts only bytes strings.

convert(value)[source]

Convert value to the wanted one.

class BoolField(doc, **kwargs)[source]

Bases: Field

A field which accepts only bool type.

convert(value)[source]

Convert value to the wanted one.

class Enum(*args, **kwargs)[source]

Bases: object

class EnumField(doc, enum, **kwargs)[source]

Bases: Field

convert(value)[source]

Convert value to the wanted one.

empty(value)[source]

Checks if a value is empty (None, NotLoaded or NotAvailable).

Return type

bool

class BaseObject(id='', url=NotLoaded, backend=None)[source]

Bases: object

This is the base class for a capability object.

A capability interface may specify to return several kind of objects, to formalise retrieved information from websites.

As python is a flexible language where variables are not typed, we use a system to force backends to set wanted values on all fields. To do that, we use the Field class and all derived ones.

For example:

class Transfer(BaseObject):
    " Transfer from an account to a recipient.  "

    amount =    DecimalField('Amount to transfer')
    date =      Field('Date of transfer', str, date, datetime)
    origin =    Field('Origin of transfer', int, str)
    recipient = Field('Recipient', int, str)

The docstring is mandatory.

Variables

url – (str) url

id = None
backend = None
property fullid

Full ID of the object, in form ‘ID@backend’.

copy()[source]
set_empty_fields(value, excepts=())[source]

Set the same value on all empty fields.

Parameters
  • value – value to set on all empty fields

  • excepts – if specified, do not change fields listed

iter_fields()[source]

Iterate on the fields keys and values.

Can be overloaded to iterate on other things.

Return type

iter[(key, value)]

to_dict()[source]
classmethod from_dict(values, backend=None)[source]