Пример #1
0
def dbus_init() -> DBusConnection:
    """Returns a new connection to the session bus, instance of
	jeepney's :class:`DBusConnection` class. This connection can
	then be passed to various SecretStorage functions, such as
	:func:`~secretstorage.collection.get_default_collection`.

	It can be used as conext manager that closes the D-Bus socket
	automatically on exit.

	Example of usage:

	.. code-block:: python

	   with secretstorage.dbus_init() as conn:
		   collection = secretstorage.get_default_collection(conn)
		   items = collection.search_items({'application': 'myapp'})

	.. versionchanged:: 3.0
	   Before the port to Jeepney, this function returned an
	   instance of :class:`dbus.SessionBus` class.

	.. versionchanged:: 3.1
	   This function no longer accepts any arguments.
	"""
    try:
        connection = connect_and_authenticate()
        add_match_rules(connection)
        return ClosingDBusConnectionWrapper(connection)
    except KeyError as ex:
        # os.environ['DBUS_SESSION_BUS_ADDRESS'] may raise it
        reason = "Environment variable {} is unset".format(ex.args[0])
        raise SecretServiceNotAvailableException(reason) from ex
    except (ConnectionError, ValueError) as ex:
        raise SecretServiceNotAvailableException(str(ex)) from ex
Пример #2
0
def dbus_init(main_loop=True, use_qt_loop=False):
	"""Returns new SessionBus_. If `main_loop` is :const:`True` and no
	D-Bus main loop is registered yet, registers a default main loop
	(PyQt5 main loop if `use_qt_loop` is :const:`True`, otherwise GLib
	main loop).

	.. _SessionBus: https://www.freedesktop.org/wiki/IntroductionToDBus/#buses

	.. note::
	   Qt uses GLib main loops on UNIX-like systems by default, so one
	   will rarely need to set `use_qt_loop` to :const:`True`.
	"""
	if main_loop and not dbus.get_default_main_loop():
		if use_qt_loop:
			from dbus.mainloop.pyqt5 import DBusQtMainLoop
			DBusQtMainLoop(set_as_default=True)
		else:
			from dbus.mainloop.glib import DBusGMainLoop
			DBusGMainLoop(set_as_default=True)
	try:
		return dbus.SessionBus()
	except dbus.exceptions.DBusException as e:
		if e.get_dbus_name() in (DBUS_NOT_SUPPORTED,
		DBUS_EXEC_FAILED, DBUS_NO_REPLY, DBUS_ACCESS_DENIED):
			raise SecretServiceNotAvailableException(
				e.get_dbus_message())
		raise
Пример #3
0
def bus_get_object(bus, name, object_path):
    """A wrapper around :meth:`SessionBus.get_object` that raises
	:exc:`~secretstorage.exceptions.SecretServiceNotAvailableException`
	when appropriate."""
    try:
        return bus.get_object(name, object_path, introspect=False)
    except dbus.exceptions.DBusException as e:
        if e.get_dbus_name() in (DBUS_SERVICE_UNKNOWN, DBUS_EXEC_FAILED,
                                 DBUS_NO_REPLY):
            raise SecretServiceNotAvailableException(e.get_dbus_message())
        raise
Пример #4
0
 def function_out(*args, **kwargs):
     try:
         return function_in(*args, **kwargs)
     except dbus.exceptions.DBusException as e:
         if e.get_dbus_name() == DBUS_UNKNOWN_METHOD:
             raise ItemNotFoundException('Item does not exist!')
         if e.get_dbus_name() == DBUS_NO_SUCH_OBJECT:
             raise ItemNotFoundException(e.get_dbus_message())
         if e.get_dbus_name() in (DBUS_NO_REPLY, DBUS_NOT_SUPPORTED):
             raise SecretServiceNotAvailableException(
                 e.get_dbus_message())
         raise
Пример #5
0
 def send_and_get_reply(self, msg: Message) -> Any:
     try:
         return self._connection.send_and_get_reply(msg, unwrap=True)
     except DBusErrorResponse as resp:
         if resp.name in (DBUS_UNKNOWN_METHOD, DBUS_NO_SUCH_OBJECT):
             raise ItemNotFoundException('Item does not exist!') from resp
         elif resp.name in (DBUS_SERVICE_UNKNOWN, DBUS_EXEC_FAILED,
                            DBUS_NO_REPLY):
             data = resp.data
             if isinstance(data, tuple):
                 data = data[0]
             raise SecretServiceNotAvailableException(data) from resp
         raise
Пример #6
0
def dbus_init() -> DBusConnection:
	"""Returns a new connection to the session bus, instance of
	jeepney's :class:`DBusConnection` class. This connection can
	then be passed to various SecretStorage functions, such as
	:func:`~secretstorage.collection.get_default_collection`.

	.. warning::
	   The D-Bus socket will not be closed automatically. You can
	   close it manually using the :meth:`DBusConnection.close` method,
	   or you can use the :class:`contextlib.closing` context manager:

	   .. code-block:: python

	      from contextlib import closing
	      with closing(dbus_init()) as conn:
	          collection = secretstorage.get_default_collection(conn)
	          items = collection.search_items({'application': 'myapp'})

	   However, you will not be able to call any methods on the objects
	   created within the context after you leave it.

	.. versionchanged:: 3.0
	   Before the port to Jeepney, this function returned an
	   instance of :class:`dbus.SessionBus` class.

	.. versionchanged:: 3.1
	   This function no longer accepts any arguments.
	"""
	try:
		connection = open_dbus_connection()
		add_match_rules(connection)
		return connection
	except KeyError as ex:
		# os.environ['DBUS_SESSION_BUS_ADDRESS'] may raise it
		reason = "Environment variable {} is unset".format(ex.args[0])
		raise SecretServiceNotAvailableException(reason) from ex
	except (ConnectionError, ValueError) as ex:
		raise SecretServiceNotAvailableException(str(ex)) from ex
Пример #7
0
def dbus_init() -> DBusConnection:
    """Returns a new connection to the session bus, instance of
	:class:`jeepney.DBusConnection` instance. This connection can
	then be passed to various SecretStorage functions, such as
	:func:`~secretstorage.collection.get_default_collection`.

	.. versionchanged:: 3.0
	   Before the port to Jeepney, this function returned an
	   instance of :class:`dbus.SessionBus` class.

	.. versionchanged:: 3.1
	   This function no longer accepts any arguments.
	"""
    try:
        connection = connect_and_authenticate()
        add_match_rules(connection)
        return connection
    except KeyError as ex:
        # os.environ['DBUS_SESSION_BUS_ADDRESS'] may raise it
        reason = "Environment variable {} is unset".format(ex.args[0])
        raise SecretServiceNotAvailableException(reason) from ex
    except (ConnectionError, ValueError) as ex:
        raise SecretServiceNotAvailableException(str(ex)) from ex