コード例 #1
0
    class Test3Implementation(object):
        def __init__(self):
            self.module_properties_changed = Signal()
            self.a_changed = Signal()
            self.b_changed = Signal()

            self._a = 1
            self._b = 2

        @property
        def a(self):
            return self._a

        def set_a(self, a):
            self._a = a
            self.a_changed.emit()

        @property
        def b(self):
            return self._b

        @b.setter
        def b(self, b):
            self._b = b
            self.b_changed.emit()

        def do_external_changes(self, a, b):
            self.set_a(a)
            self.b = b

        def do_secret_changes(self, a, b):
            self._a = a
            self._b = b
コード例 #2
0
ファイル: server.py プロジェクト: yurchor/dasbus
class Register(object):
    """The implementation of the user register."""
    def __init__(self):
        self._users = []
        self._users_changed = Signal()

    @property
    def users(self):
        """The list of users."""
        return self._users

    @property
    def users_changed(self):
        """Signal the user list change."""
        return self._users_changed

    def register_user(self, user: User):
        """Register a new user."""
        if any(u for u in self.users if u.name == user.name):
            raise InvalidUser("User {} exists.".format(user.name))

        self._users.append(user)
        self._users_changed.emit()
コード例 #3
0
ファイル: observer.py プロジェクト: dhodovsk/dasbus
class DBusObserver(object):
    """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:

    # 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()
    """
    def __init__(self, message_bus, service_name, monitoring=GLibMonitoring):
        """Creates an DBus service observer.

        :param message_bus: a message bus
        :param service_name: a DBus name of a service
        """
        self._message_bus = message_bus
        self._service_name = service_name
        self._is_service_available = False

        self._service_available = Signal()
        self._service_unavailable = Signal()

        self._monitoring = monitoring
        self._subscriptions = []

    @property
    def service_name(self):
        """Returns a DBus name."""
        return self._service_name

    @property
    def is_service_available(self):
        """The proxy can be accessed."""
        return self._is_service_available

    @property
    def service_available(self):
        """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.
        """
        return self._service_available

    @property
    def service_unavailable(self):
        """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.
        """
        return self._service_unavailable

    def connect_once_available(self):
        """Connect to the service once it is available.

        The observer is not connected to the service until it
        emits the service_available signal.
        """
        self._watch()

    def disconnect(self):
        """Disconnect from the service.

        Disconnect from the service if it is connected and stop
        watching its availability.
        """
        self._unwatch()

        if self.is_service_available:
            self._disable_service()

    def _watch(self):
        """Watch the service name on DBus."""
        subscription = self._monitoring.watch_name(
            self._message_bus.connection, self.service_name, DBUS_FLAG_NONE,
            self._service_name_appeared_callback,
            self._service_name_vanished_callback)

        self._subscriptions.append(subscription)

    def _unwatch(self):
        """Stop to watch the service name on DBus."""
        while self._subscriptions:
            callback = self._subscriptions.pop()
            callback()

    def _enable_service(self):
        """Enable the service."""
        self._is_service_available = True
        self._service_available.emit(self)

    def _disable_service(self):
        """Disable the service."""
        self._is_service_available = False
        self._service_unavailable.emit(self)

    def _service_name_appeared_callback(self, *args):
        """Callback for the watch method."""
        if not self.is_service_available:
            self._enable_service()

    def _service_name_vanished_callback(self, *args):
        """Callback for the watch method."""
        if self.is_service_available:
            self._disable_service()

    def __str__(self):
        """Returns a string version of this object."""
        return self._service_name

    def __repr__(self):
        """Returns a string representation."""
        return "{}({})".format(self.__class__.__name__, self._service_name)