dasbus.error module

class AbstractErrorRule[source]

Bases: object

Abstract rule for mapping a Python exception to a DBus error.

abstract get_name(exception_type)[source]

Get a DBus name for the given exception type.

Parameters

exception_type – a type of the Python error

Returns

a name of the DBus error

abstract get_type(error_name)[source]

Get an exception type of the given DBus error.

param error_name: a name of the DBus error :return: a type of the Python error

abstract match_name(error_name)[source]

Is this rule matching the given DBus error?

Parameters

error_name – a name of the DBus error

Returns

True or False

abstract match_type(exception_type)[source]

Is this rule matching the given exception type?

Parameters

exception_type – a type of the Python error

Returns

True or False

exception DBusError[source]

Bases: Exception

A default DBus error.

class DefaultErrorRule(default_type, default_namespace)[source]

Bases: AbstractErrorRule

Default rule for mapping a Python exception to a DBus error.

get_name(exception_type)[source]

Get a DBus name for the given exception type.

get_type(error_name)[source]

Get an exception type of the given DBus error.

match_name(error_name)[source]

Is this rule matching the given DBus error?

match_type(exception_type)[source]

Is this rule matching the given exception type?

class ErrorMapper[source]

Bases: object

Class for mapping Python exceptions to DBus errors.

add_rule(rule: AbstractErrorRule)[source]

Add a rule to the error mapper.

The new rule will have a higher priority than the rules already contained in the error mapper.

Parameters

rule (an instance of AbstractErrorRule) – an error rule

get_error_name(exception_type)[source]

Get a DBus name of the Python exception.

Try to find a matching rule in the error mapper. If a rule matches the given exception type, use the rule to get the name of the DBus error.

The rules in the error mapper are processed in the reversed order to respect the priority of the rules.

Parameters

exception_type (a subclass of Exception) – a type of the Python error

Returns

a name of the DBus error

Raises

LookupError – if no name is found

get_exception_type(error_name)[source]

Get a Python exception type of the DBus error.

Try to find a matching rule in the error mapper. If a rule matches the given name of a DBus error, use the rule to get the type of a Python exception.

The rules in the error mapper are processed in the reversed order to respect the priority of the rules.

Parameters

error_name – a name of the DBus error

Returns

a type of the Python exception

Return type

a subclass of Exception

Raises

LookupError – if no type is found

reset_rules()[source]

Reset rules in the error mapper.

Reset the error rules to the initial state. All rules will be replaced with the default ones.

class ErrorRule(exception_type, error_name)[source]

Bases: AbstractErrorRule

Rule for mapping a Python exception to a DBus error.

get_name(exception_type)[source]

Get a DBus name for the given exception type.

get_type(error_name)[source]

Get an exception type of the given DBus error.

match_name(error_name)[source]

Is this rule matching the given DBus error?

match_type(exception_type)[source]

Is this rule matching the given exception type?

get_error_decorator(error_mapper)[source]

Generate a decorator for DBus errors.

Create a function for decorating Python exception classes. The decorator will add a new rule to the given error mapper that will map the class to the specified error name.

Definition of the decorator:

decorator(error_name, namespace=())

The decorator accepts a name of the DBus error and optionally a namespace of the DBus name. The namespace will be used as a prefix of the DBus name.

Usage:

# Create an error mapper.
error_mapper = ErrorMapper()

# Create a decorator for DBus errors and use it to map
# the class ExampleError to the name my.example.Error.
dbus_error = create_error_decorator(error_mapper)

@dbus_error("my.example.Error")
class ExampleError(DBusError):
    pass
Parameters

error_mapper – an error mapper

Returns

a decorator