Modules¶
dcar.address¶
Server Addresses.
-
class
dcar.address.Address(address='session')[source]¶ Bases:
objectThis class represents addresses of a message bus.
See also:
An
Addressobject can be used as an iterator which yields tuples with the first element being the name of a transport and the second a dict with the parameters.- Parameters
address (str) – can be one of the case-insensitive names
'system','session', or'starter'or a valid D-Bus server address
-
property
bus_type¶ Return the bus type:
'system','session', orNone.
dcar.auth¶
Authentication to a message bus.
Supported mechanisms:
EXTERNAL
DBUS_COOKIE_SHA1
ANONYMOUS
-
dcar.auth.authenticate(sock, unix_fds)[source]¶ Authenticate to a message bus.
The passing of unix file descriptors will only be negotiated if
unix_fdsisTrue.- Parameters
- Returns
the GUID of the server and a
boolthat indicates whether unix file descriptor passing is possible (True) or not (False)- Return type
- Raises
AuthenticationError – if authentication failed
dcar.introspection¶
Introspection module.
New in version 0.3.0.
-
class
dcar.introspection.Data(bus, name, path, xml)[source]¶ Bases:
objectIntrospection data.
If not provided, an instance of this class gets the XML data by calling org.freedesktop.DBus.Introspectable.Introspect.
The XML data is parsed and a data structure is created with a mapping from the names of members of the interfaces to a list of objects of type
Method,Signal, orProperty.- Access to objects:
data['member name']data['interface name', 'member name']
- Parameters
- Raises
RuntimeError – if
busis not a connecteddcar.BusobjectValidationError – if
nameorpathare not valid
-
property
xml¶ Return XML data.
-
class
dcar.introspection.Method(name, interface, in_signature, out_signature, no_reply)¶ Bases:
tupleRepresentation of an interface member of type method.
-
in_signature¶ Alias for field number 2
-
interface¶ Alias for field number 1
-
name¶ Alias for field number 0
-
no_reply¶ Alias for field number 4
-
out_signature¶ Alias for field number 3
-
-
class
dcar.introspection.Property(name, interface, signature, read, write, changed_signal)¶ Bases:
tupleRepresentation of an interface member of type property.
-
changed_signal¶ Alias for field number 5
-
interface¶ Alias for field number 1
-
name¶ Alias for field number 0
-
read¶ Alias for field number 3
-
signature¶ Alias for field number 2
-
write¶ Alias for field number 4
-
dcar.bus¶
Connection to message bus.
-
class
dcar.bus.Bus(address='session')[source]¶ Bases:
objectRepresentation of a client’s connection to a message bus.
An instance of this class is the central point for a client to interact with a message bus. It can be used as a context manager. On entering the runtime context the
connect()method will be called, on exiting thedisconnect()method.-
property
address¶ Return the actual address the client is connected to.
-
property
bus_type¶ Return the bus type:
'system','session', orNone.New in version 0.3.0.
-
property
connected¶ Return whether the client is connected.
-
property
unique_name¶ Return the unique name of the client’s connection.
-
property
guid¶ Return the GUID of the server.
-
property
unix_fds_enabled¶ Return whether passing of unix file descriptors is enabled.
-
property
error¶ Return transport error or
None.This error is set when the client loses the connection to the message bus because of this error.
-
connect()[source]¶ Connect to message bus.
- Raises
AuthenticationError – if authentication failed
OSError – if connection failed
-
block(timeout=None)[source]¶ Blocks until
send-loopandrecv-loopare finished or timeout is reached.The
send-loopandrecv-loopthreads are started when connecting to the D-Bus.- Parameters
timeout (float) – timeout value in seconds (
Nonemeans no timeout)
Changed in version 0.2.0: Add parameter
timeout
-
send_message(msg, timeout=None)[source]¶ Send a message.
Normally
method_call(),method_return(),send_error()oremit_signal()should be used.- Parameters
- Returns
return values of the message call if a reply is expected else
None- Return type
- Raises
TransportError – if the message could not be sent
-
method_call(object_path, interface, method_name, destination, *, sender=None, signature=None, args=(), timeout=25.0, no_auto_start=False, allow_interactive_authorization=False)[source]¶ Send a message of type METHOD_CALL.
- Parameters
object_path (str) – object path (required)
interface (str) – interface name
method_name (str) – method name (required)
destination (str) – name of the destination’s connection
sender (str) – name of the sender’s connection
signature (str) – D-Bus types signature of the IN arguments
args (tuple) – the IN arguments
timeout (float) –
None= no timeout,0= no reply expected and> 0= timeout in secondsno_auto_start (bool) – header flag
allow_interactive_authorization (bool) – header flag
- Returns
return values of the method call if a reply is expected else
None- Return type
- Raises
TransportError – if the message could not be sent
-
method_return(reply_serial, destination, *, sender=None, signature=None, args=())[source]¶ Send a message of type METHOD_RETURN.
- Parameters
reply_serial (int) – serial of the message for which this is a reply (required)
destination (str) – name of the destination’s connection
sender (str) – name of the sender’s connection
signature (str) – D-Bus types signature of the OUT arguments of the called method
args (tuple) – the OUT arguments of the called method
- Raises
TransportError – if the message could not be sent
-
send_error(error_name, reply_serial, destination, *, sender=None, signature=None, args=())[source]¶ Send a message of type ERROR.
- Parameters
error_name (str) – error name (required)
reply_serial (int) – serial of the message for which this is a reply (required)
destination (str) – name of the destination’s connection
sender (str) – name of the sender’s connection
signature (str) – D-Bus types signature of the arguments
args (tuple) – the arguments
- Raises
TransportError – if the message could not be sent
-
emit_signal(object_path, interface, signal_name, destination=None, *, sender=None, signature=None, args=())[source]¶ Send a message of type SIGNAL.
- Parameters
object_path (str) – object path (required)
interface (str) – interface name (required)
signal_name (str) – signal name (required)
destination (str) – name of the destination’s connection
sender (str) – name of the sender’s connection
signature (str) – D-Bus types signature of the arguments
args (tuple) – the arguments
- Raises
TransportError – if the message could not be sent
-
register_signal(rule, handler, unicast=False, timeout=25.0)[source]¶ Register a signal.
The handler function must take one parameter: a
MessageInfoobject.Note
The handler functions for incoming method calls and signals will be executed in a separate thread sequentially.
- Parameters
- Returns
ID of the signal
- Return type
- Raises
RegisterError – if the signal could not be registered
TransportError – if the AddMatch message could not be sent
-
unregister_signal(reg_id, timeout=25.0)[source]¶ Unregister a signal.
- Parameters
reg_id (int) – ID returned by
register_signal()timeout (float) – timeout in seconds
- Raises
TransportError – if RemoveMatch message could not be sent
-
register_method(object_path, interface, method_name, handler, signature=None)[source]¶ Register a method.
The handler function must take two parameters: a
Busobject and aMessageInfoobject.Note
The handler functions for incoming method calls and signals will be executed in a separate thread sequentially.
-
unregister_method(meth_id)[source]¶ Unregister a method.
- Parameters
meth_id (int) – ID returned by
register_method()
-
property
dcar.const¶
Some constants from dbus source files.
-
dcar.const.MAJOR_PROTOCOL_VERSION= b'\x01'¶ Major protocol version
-
dcar.const.MIN_HEADER_SIZE= 16¶ Minimum header size
-
dcar.const.MAX_MESSAGE_LEN= 134217728¶ Maximum message length
-
dcar.const.MAX_ARRAY_LEN= 67108864¶ Maximum array length
-
dcar.const.MAX_NAME_LEN= 255¶ Maximum name length
-
dcar.const.MAX_SIGNATURE_LEN= 255¶ Maximum signature length
-
dcar.const.MAX_MATCH_RULE_LEN= 1024¶ Maximum match rule length
-
dcar.const.MAX_MATCH_RULE_ARG_NUM= 63¶ Maximum number of match rule arguments
-
dcar.const.MAX_NESTING_DEPTH= 32¶ Maximum nesting depth of arrays and structs
-
dcar.const.MAX_VARIANT_NESTING_DEPTH= 64¶ Maximum nesting depth of variants
-
dcar.const.MAX_MSG_UNIX_FDS= 33554432¶ Maximum number of unix file descriptors
-
dcar.const.LOCAL_PATH= '/org/freedesktop/DBus/Local'¶ Reserved local path
-
dcar.const.LOCAL_INTERFACE= 'org.freedesktop.DBus.Local'¶ Reserved local interface
-
dcar.const.DEFAULT_TIMEOUT_VALUE= 25.0¶ Default timeout when waiting for a reply
dcar.errors¶
Errors module.
-
exception
dcar.errors.AddressError[source]¶ Bases:
dcar.errors.ErrorRaised for errors in server addresses.
-
exception
dcar.errors.AuthenticationError[source]¶ Bases:
dcar.errors.ErrorRaised when authentication failed.
-
exception
dcar.errors.TransportError[source]¶ Bases:
dcar.errors.ErrorRaised for transport related errors.
-
exception
dcar.errors.ValidationError[source]¶ Bases:
dcar.errors.ErrorRaised when validation failed.
-
exception
dcar.errors.RegisterError[source]¶ Bases:
dcar.errors.ErrorRaised when a signal or method could not be registered.
-
exception
dcar.errors.MessageError[source]¶ Bases:
dcar.errors.ErrorRaised for errors in messages.
-
exception
dcar.errors.DBusError[source]¶ Bases:
dcar.errors.MessageErrorRaised for errors from ERROR messages.
-
exception
dcar.errors.SignatureError[source]¶ Bases:
dcar.errors.MessageErrorRaised for errors in signatures.
-
exception
dcar.errors.TooLongError[source]¶ Bases:
dcar.errors.MessageErrorRaised when a message, an array, a name etc. is too long.
dcar.marshal¶
Marshal/unmarshal D-Bus Messages.
See:
Note
Instances of the type classes should be used through the
types mapping which uses D-Bus type codes as keys.
-
dcar.marshal.types¶ Mapping of D-Bus type codes to type class instances
-
class
dcar.marshal.Type(name)[source]¶ Bases:
objectBase class.
- Parameters
name (str) – type name
-
marshal(raw, data, signature=None)[source]¶ Marshal an object of this type.
The data must be of an appropriate type according to the column Python IN in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Raises
MessageError – if the data could not be marshalled
-
unmarshal(raw, signature=None)[source]¶ Unmarshal an object of this type.
The returned data will be of a type according to the column Python OUT in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Returns
unmarshalled data
- Raises
MessageError – if the data could not be unmarshalled
-
class
dcar.marshal.Fixed(name, struct_code)[source]¶ Bases:
dcar.marshal.TypeClass for fixed types.
-
marshal(raw, data, signature=None)[source]¶ Marshal an object of this type.
The data must be of an appropriate type according to the column Python IN in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Raises
MessageError – if the data could not be marshalled
-
unmarshal(raw, signature=None)[source]¶ Unmarshal an object of this type.
The returned data will be of a type according to the column Python OUT in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Returns
unmarshalled data
- Raises
MessageError – if the data could not be unmarshalled
-
-
class
dcar.marshal.Boolean[source]¶ Bases:
dcar.marshal.FixedClass for booleans.
-
marshal(raw, data, signature=None)[source]¶ Marshal an object of this type.
The data must be of an appropriate type according to the column Python IN in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Raises
MessageError – if the data could not be marshalled
-
unmarshal(raw, signature=None)[source]¶ Unmarshal an object of this type.
The returned data will be of a type according to the column Python OUT in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Returns
unmarshalled data
- Raises
MessageError – if the data could not be unmarshalled
-
-
class
dcar.marshal.UnixFd[source]¶ Bases:
dcar.marshal.FixedClass for unix file descriptors.
-
marshal(raw, data, signature=None)[source]¶ Marshal an object of this type.
The data must be of an appropriate type according to the column Python IN in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Raises
MessageError – if the data could not be marshalled
-
unmarshal(raw, signature=None)[source]¶ Unmarshal an object of this type.
The returned data will be of a type according to the column Python OUT in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Returns
unmarshalled data
- Raises
MessageError – if the data could not be unmarshalled
-
-
class
dcar.marshal.StringLike(name, len_type, validate_func)[source]¶ Bases:
dcar.marshal.TypeClass for string like types.
- Parameters
-
marshal(raw, data, signature=None)[source]¶ Marshal an object of this type.
The data must be of an appropriate type according to the column Python IN in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Raises
MessageError – if the data could not be marshalled
-
unmarshal(raw, signature=None)[source]¶ Unmarshal an object of this type.
The returned data will be of a type according to the column Python OUT in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Returns
unmarshalled data
- Raises
MessageError – if the data could not be unmarshalled
-
class
dcar.marshal.Container(name, alignment)[source]¶ Bases:
dcar.marshal.TypeBase class for container types.
-
class
dcar.marshal.Variant[source]¶ Bases:
dcar.marshal.ContainerClass for a D-Bus Variant.
-
marshal(raw, data, signature=None)[source]¶ Marshal an object of this type.
The data must be of an appropriate type according to the column Python IN in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Raises
MessageError – if the data could not be marshalled
-
unmarshal(raw, signature=None)[source]¶ Unmarshal an object of this type.
The returned data will be of a type according to the column Python OUT in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Returns
unmarshalled data
- Raises
MessageError – if the data could not be unmarshalled
-
-
class
dcar.marshal.Array[source]¶ Bases:
dcar.marshal.ContainerClass for a D-Bus Array.
-
marshal(raw, data, signature)[source]¶ Marshal an object of this type.
The data must be of an appropriate type according to the column Python IN in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Raises
MessageError – if the data could not be marshalled
-
unmarshal(raw, signature)[source]¶ Unmarshal an object of this type.
The returned data will be of a type according to the column Python OUT in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Returns
unmarshalled data
- Raises
MessageError – if the data could not be unmarshalled
-
-
class
dcar.marshal.Struct(name='STRUCT')[source]¶ Bases:
dcar.marshal.ContainerClass for a D-Bus Struct.
-
marshal(raw, data, signature)[source]¶ Marshal an object of this type.
The data must be of an appropriate type according to the column Python IN in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Raises
MessageError – if the data could not be marshalled
-
unmarshal(raw, signature)[source]¶ Unmarshal an object of this type.
The returned data will be of a type according to the column Python OUT in the types summary.
The
signatureparameter is only used in the classesArray,Struct, andDictEntry.- Parameters
- Returns
unmarshalled data
- Raises
MessageError – if the data could not be unmarshalled
-
-
class
dcar.marshal.DictEntry[source]¶ Bases:
dcar.marshal.StructClass for a D-Bus DictEntry.
-
dcar.marshal.marshal(raw, data, signature)[source]¶ Marshal objects.
The elements of the data tuple must be of appropriate types according to the column Python IN in the types summary.
- Parameters
- Raises
MessageError – if the data could not be marshalled
-
dcar.marshal.unmarshal(raw, signature)[source]¶ Unmarshal objects.
The elements of the returned tuple will be of types according to the column Python OUT in the types summary.
- Parameters
- Returns
tuple of unmarshalled data
- Return type
- Raises
MessageError – if the data could not be unmarshalled
dcar.message¶
D-Bus Message.
See: Message Protocol
-
class
dcar.message.Byteorder(value)[source]¶ Bases:
dcar.message.EnumReprMixin,enum.EnumEnumeration of byteorders.
-
LITTLE= b'l'¶
-
BIG= b'B'¶
-
NATIVE= b'l'¶
-
-
class
dcar.message.MessageType(value)[source]¶ Bases:
dcar.message.EnumReprMixin,enum.EnumEnumeration of message types.
-
INVALID= b'\x00'¶
-
METHOD_CALL= b'\x01'¶
-
METHOD_RETURN= b'\x02'¶
-
ERROR= b'\x03'¶
-
SIGNAL= b'\x04'¶
-
-
class
dcar.message.HeaderFlag(value)[source]¶ Bases:
enum.IntFlagEnumeration of header flags.
-
NONE= 0¶
-
NO_REPLY_EXPECTED= 1¶
-
NO_AUTO_START= 2¶
-
ALLOW_INTERACTIVE_AUTHORIZATION= 4¶
-
-
class
dcar.message.HeaderField(value)[source]¶ Bases:
dcar.message.EnumReprMixin,enum.EnumEnumeration of header fields.
-
PATH= 1¶
-
INTERFACE= 2¶
-
MEMBER= 3¶
-
ERROR_NAME= 4¶
-
REPLY_SERIAL= 5¶
-
DESTINATION= 6¶
-
SENDER= 7¶
-
SIGNATURE= 8¶
-
UNIX_FDS= 9¶
-
-
class
dcar.message.HeaderFields[source]¶ Bases:
objectFields in a message header.
-
signature= 'a(yv)'¶
-
check(message_type)[source]¶ Check if required header fields are present and validate them.
- Parameters
message_type (MessageType) – the message type
- Raises
ValidationError – if validation failed
MessageError – if required fields are missing
-
-
class
dcar.message.Message(message_type, flags, fields, body)[source]¶ Bases:
objectA D-Bus message.
Objects of this type must be treated as immutable.
- Parameters
message_type (MessageType) – type of this message
flags – or’ed
HeaderFlags,0, orNonefields (HeaderFields) – header fields
body (tuple) – the data for the body of this message
- Raises
ValidationError – if any validation fails
TypeError – if there is any argument of the wrong type
-
property
info¶ Return a
MessageInfoobject.Only available for messages of type METHOD_CALL and SIGNAL.
-
property
reply_expected¶ Return
Trueif a reply is expected.
-
property
reply_serial¶ Return the reply serial from the header fields.
-
property
sender¶ Return the sender from the header fields.
-
classmethod
from_bytes(raw)[source]¶ Create a new message object from bytes.
- Parameters
raw (RawData) – raw message data
- Raises
MessageError – if the message could not be created
-
to_bytes()[source]¶ Convert this message to bytes.
- Raises
MessageError – if the message could not be converted
-
class
dcar.message.MessageInfo(serial, args, path, interface, member, sender, no_reply_expected, allow_interactive_authorization, is_signal)¶ Bases:
tupleObjects of this type will be passed to handler functions registered with
register_signal()andregister_method().header flag
-
args¶ arguments as a tuple
-
interface¶ interface name
-
is_signal¶ Trueif signal
-
member¶ member (signal or method) name
-
no_reply_expected¶ header flag
-
path¶ object path
-
sender¶ name of the sender’s connection
-
serial¶ message serial
dcar.raw¶
Raw message data.
dcar.remote¶
Remote module.
New in version 0.3.0.
-
class
dcar.remote.RemoteObject(bus, name, path, xml=None)[source]¶ Bases:
objectAn instance of this class is a proxy for an object on the D-Bus.
Methods, signals, and properties can be accessed as attributes with the same names as on the D-Bus. If a name occurs in more than one interface, only one method, signal, or property can be accessed as an attribute. To access the shadowed names subscription syntax must be used:
obj['interface name', 'member name'].Calling methods and accessing properties (depending on the access attribute) is done in the normal way:
obj.SomeMethod()(returns a single value, a tuple orNone)obj.SomeProperty(only with read access)obj.SomeProperty = value(only with write access)
Signal handlers can be set by assigning a function that takes a
MessageInfoobject as its only argument:obj.SomeSignal = function. The handler can be removed by assigningNone.See also: Types summary
- Parameters
- Raises
RuntimeError – if
busis not a connecteddcar.BusobjectValidationError – if
nameorpathare not valid
-
property
xml¶ Return XML data.
-
class
dcar.remote.DBus(bus)[source]¶ Bases:
dcar.remote.RemoteObjectConvenience subclass of
RemoteObject.- Parameters:
name = ‘org.freedesktop.DBus’
path = ‘/org/freedesktop/DBus’
See: Section Message Bus Messages in the D-Bus specification.
-
class
dcar.remote.Notifications(bus)[source]¶ Bases:
dcar.remote.RemoteObjectConvenience subclass of
RemoteObject.- Parameters:
name = ‘org.freedesktop.Notifications’
path = ‘/org/freedesktop/Notifications’
See: Desktop Notifications Specification and Kitsiso.
-
class
dcar.remote.PowerManagement(bus)[source]¶ Bases:
dcar.remote.RemoteObjectConvenience subclass of
RemoteObject.- Parameters:
name = ‘org.freedesktop.PowerManagement’
path = ‘/org/freedesktop/PowerManagement’
-
class
dcar.remote.Login1(bus)[source]¶ Bases:
dcar.remote.RemoteObjectConvenience subclass of
RemoteObject.- Parameters:
name = ‘org.freedesktop.login1’
path = ‘/org/freedesktop/login1’
See: Section The Manager Object in the logind documentation.
dcar.router¶
Message routing.
-
class
dcar.router.Router(bus)[source]¶ Bases:
objectClass for routing in- and outgoing messages.
-
outgoing(msg, timeout)[source]¶ Handle outgoing messages.
- Parameters
- Returns
return values of a message call if a reply is expected or
None- Return type
- Raises
TransportError – if the message could not be sent
MessageError – if the message could not be marshalled
-
-
class
dcar.router.MatchRule(object_path: str = None, interface: str = None, signal_name: str = None, sender: str = None, path_namespace: str = None, destination: str = None, arg0namespace: str = None)[source]¶ Bases:
objectMatch rule for signals.
All parameters are explained in the D-Bus specification.
-
class
dcar.router.Registry[source]¶ Bases:
objectBase class for registries.
-
class
dcar.router.Signals[source]¶ Bases:
dcar.router.RegistrySignals registry.
An
itemfor this type’sadd()method is aMatchRule(see also:register_signal()).-
params= ('msginfo',)¶ handler parameters
-
-
class
dcar.router.Methods[source]¶ Bases:
dcar.router.RegistryMethods registry.
An
itemfor this type’sadd()method is a tuple(object_path, interface, method_name)(see also:register_method()).-
params= ('bus', 'msginfo')¶ handler parameters
-
dcar.signature¶
D-Bus type signature.
-
class
dcar.signature.Signature(sig)[source]¶ Bases:
objectA signature.
The signature string will be parsed into a list of complete types. Each complete type is a tuple with the fist element being its signature. The second element is
Nonefor all basic (i.e. fixed and string-like) types, an empty list for variants, and a list of complete types for arrays, structs, and dict entries.A
Signatureobject can be used as an iterator which yields tuples of complete types.- Parameters
sig (str) – D-Bus type signature
- Raises
SignatureError – if there is a problem with the signature
dcar.transports¶
Transports for message bus connections.
-
class
dcar.transports.Transport(params, router)[source]¶ Bases:
objectBase class.
-
property
error¶ Return the error which caused disconnection or
None.
-
property
-
class
dcar.transports.UnixTransport(params, router)[source]¶ Bases:
dcar.transports.TransportTransport that uses a unix domain socket.
It supports the passing of file descriptors.
-
class
dcar.transports.TcpTransport(params, router)[source]¶ Bases:
dcar.transports.TransportTransport that uses a TCP socket.
-
class
dcar.transports.NonceTcpTransport(params, router)[source]¶ Bases:
dcar.transports.TcpTransportTransport that uses a nonce-authenticated TCP socket.
dcar.validate¶
Validation functions.
The is_valid_* functions return True if the argument is valid,
otherwise False.
The validate_* functions raise a ValidationError if
validation failed or else return the argument unchanged.