Mini Shell
3
��^[ � @ s� d dl Z d dlmZ d dlmZ d dlmZ d dlZejdd� d dl m
Z
e je�Z
dd d
gZG dd� de�ZG dd
� d
e�ZG d
d � d e�ZdS )� N)�partial)�DBUS_FLAG_NONE)�Signal�Gioz2.0)r �DBusObserverError�DBusObserver�GLibMonitoringc @ s e Zd ZdZdS )r z'Exception class for the DBus observers.N)�__name__�
__module__�__qualname__�__doc__� r
r
�/usr/lib/python3.6/observer.pyr ( s c @ sH e Zd ZdZeeddfdd��Zedd� �Zedd� �Zed d
� �Z dS )r z4The low-level DBus monitoring library based on GLib.Nc C sT d}d}|rt | j|f fd�}|r4t | j|f fd�}tj|||||�}t | j||�S )z,Watch a service name on the DBus connection.N)� user_data)r �_name_appeared_callback�_name_vanished_callbackr Zbus_watch_name_on_connection�
_unwatch_name) �cls�
connection�name�flagsZ
name_appearedZ
name_vanishedZname_appeared_closureZname_vanished_closure�registration_idr
r
r �
watch_name0 s( zGLibMonitoring.watch_namec C s |\}}||f|�� dS )zCallback for watch_name..Nr
)r r r Z
name_ownerr �callback�
callback_argsr
r
r r Q s z&GLibMonitoring._name_appeared_callbackc C s |\}}||� dS )zCallback for watch_name.Nr
)r r r r r r r
r
r r Z s z&GLibMonitoring._name_vanished_callbackc C s t j|� dS )z5Stops watching a service name on the DBus connection.N)r Zbus_unwatch_name)r r r r
r
r r c s zGLibMonitoring._unwatch_name)
r r
r r �classmethodr r r r r r
r
r
r r - s c @ s� e Zd ZdZefdd�Zedd� �Zedd� �Zedd � �Z ed
d� �Z
dd
� Zdd� Zdd� Z
dd� Zdd� Zdd� Zdd� Zdd� Zdd� Zdd� Zd S )!r a4 Base class for DBus observers.
This class is recommended to use only to watch the availability
of a service on DBus. It doesn't provide any support for accessing
objects provided by the service.
Usage:
.. code-block:: python
# Create the observer and connect to its signals.
observer = DBusObserver(SystemBus, "org.freedesktop.NetworkManager")
def callback1(observer):
print("Service is available!")
def callback2(observer):
print("Service is unavailable!")
observer.service_available.connect(callback1)
observer.service_unavailable.connect(callback2)
# Connect to the service once it is available.
observer.connect_once_available()
# Disconnect the observer.
observer.disconnect()
c C s2 || _ || _d| _t� | _t� | _|| _g | _dS )z�Creates a DBus service observer.
:param message_bus: a message bus
:param service_name: a DBus name of a service
FN)�_message_bus�
_service_name�_is_service_availabler �_service_available�_service_unavailable�_monitoring�_subscriptions)�selfZmessage_bus�service_nameZ
monitoringr
r
r �__init__� s zDBusObserver.__init__c C s | j S )zReturns a DBus name.)r )r# r
r
r r$ � s zDBusObserver.service_namec C s | j S )zThe proxy can be accessed.)r )r# r
r
r �is_service_available� s z!DBusObserver.is_service_availablec C s | j S )z�Signal that emits when the service is available.
Signal emits this class as an argument. You have to
call the watch method to activate the signals.
)r )r# r
r
r �service_available� s zDBusObserver.service_availablec C s | j S )z�Signal that emits when the service is unavailable.
Signal emits this class as an argument. You have to
call the watch method to activate the signals.
)r )r# r
r
r �service_unavailable� s z DBusObserver.service_unavailablec C s | j � dS )z�Connect to the service once it is available.
The observer is not connected to the service until it
emits the service_available signal.
N)�_watch)r# r
r
r �connect_once_available� s z#DBusObserver.connect_once_availablec C s | j � | jr| j� dS )z�Disconnect from the service.
Disconnect from the service if it is connected and stop
watching its availability.
N)�_unwatchr&