Blame dbus/exceptions.py

Packit 130fc8
"""D-Bus exceptions."""
Packit 130fc8
Packit 130fc8
# Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
Packit 130fc8
#
Packit 130fc8
# Permission is hereby granted, free of charge, to any person
Packit 130fc8
# obtaining a copy of this software and associated documentation
Packit 130fc8
# files (the "Software"), to deal in the Software without
Packit 130fc8
# restriction, including without limitation the rights to use, copy,
Packit 130fc8
# modify, merge, publish, distribute, sublicense, and/or sell copies
Packit 130fc8
# of the Software, and to permit persons to whom the Software is
Packit 130fc8
# furnished to do so, subject to the following conditions:
Packit 130fc8
#
Packit 130fc8
# The above copyright notice and this permission notice shall be
Packit 130fc8
# included in all copies or substantial portions of the Software.
Packit 130fc8
#
Packit 130fc8
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 130fc8
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 130fc8
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 130fc8
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Packit 130fc8
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
Packit 130fc8
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Packit 130fc8
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit 130fc8
# DEALINGS IN THE SOFTWARE.
Packit 130fc8
Packit 130fc8
__all__ = ('DBusException', 'MissingErrorHandlerException',
Packit 130fc8
           'MissingReplyHandlerException', 'ValidationException',
Packit 130fc8
           'IntrospectionParserException', 'UnknownMethodException',
Packit 130fc8
           'NameExistsException')
Packit 130fc8
Packit 130fc8
from dbus._compat import is_py3
Packit 130fc8
Packit 130fc8
Packit 130fc8
class DBusException(Exception):
Packit 130fc8
Packit 130fc8
    include_traceback = False
Packit 130fc8
    """If True, tracebacks will be included in the exception message sent to
Packit 130fc8
    D-Bus clients.
Packit 130fc8
Packit 130fc8
    Exceptions that are not DBusException subclasses always behave
Packit 130fc8
    as though this is True. Set this to True on DBusException subclasses
Packit 130fc8
    that represent a programming error, and leave it False on subclasses that
Packit 130fc8
    represent an expected failure condition (e.g. a network server not
Packit 130fc8
    responding)."""
Packit 130fc8
Packit 130fc8
    def __init__(self, *args, **kwargs):
Packit 130fc8
        name = kwargs.pop('name', None)
Packit 130fc8
        if name is not None or getattr(self, '_dbus_error_name', None) is None:
Packit 130fc8
            self._dbus_error_name = name
Packit 130fc8
        if kwargs:
Packit 130fc8
            raise TypeError('DBusException does not take keyword arguments: %s'
Packit 130fc8
                            % ', '.join(kwargs.keys()))
Packit 130fc8
        Exception.__init__(self, *args)
Packit 130fc8
Packit 130fc8
    def __unicode__(self):
Packit 130fc8
        """Return a unicode error"""
Packit 130fc8
        # We can't just use Exception.__unicode__ because it chains up weirdly.
Packit 130fc8
        # https://code.launchpad.net/~mvo/ubuntu/quantal/dbus-python/lp846044/+merge/129214
Packit 130fc8
        if len(self.args) > 1:
Packit 130fc8
            s = unicode(self.args)
Packit 130fc8
        else:
Packit 130fc8
            s = ''.join(self.args)
Packit 130fc8
Packit 130fc8
        if self._dbus_error_name is not None:
Packit 130fc8
            return '%s: %s' % (self._dbus_error_name, s)
Packit 130fc8
        else:
Packit 130fc8
            return s
Packit 130fc8
Packit 130fc8
    def __str__(self):
Packit 130fc8
        """Return a str error"""
Packit 130fc8
        s = Exception.__str__(self)
Packit 130fc8
        if self._dbus_error_name is not None:
Packit 130fc8
            return '%s: %s' % (self._dbus_error_name, s)
Packit 130fc8
        else:
Packit 130fc8
            return s
Packit 130fc8
Packit 130fc8
    def get_dbus_message(self):
Packit 130fc8
        if len(self.args) > 1:
Packit 130fc8
            if is_py3:
Packit 130fc8
                s = str(self.args)
Packit 130fc8
            else:
Packit 130fc8
                s = unicode(self.args)
Packit 130fc8
        else:
Packit 130fc8
            s = ''.join(self.args)
Packit 130fc8
Packit 130fc8
        if isinstance(s, bytes):
Packit 130fc8
            return s.decode('utf-8', 'replace')
Packit 130fc8
Packit 130fc8
        return s
Packit 130fc8
Packit 130fc8
    def get_dbus_name(self):
Packit 130fc8
        return self._dbus_error_name
Packit 130fc8
Packit 130fc8
class MissingErrorHandlerException(DBusException):
Packit 130fc8
Packit 130fc8
    include_traceback = True
Packit 130fc8
Packit 130fc8
    def __init__(self):
Packit 130fc8
        DBusException.__init__(self, "error_handler not defined: if you define a reply_handler you must also define an error_handler")
Packit 130fc8
Packit 130fc8
class MissingReplyHandlerException(DBusException):
Packit 130fc8
Packit 130fc8
    include_traceback = True
Packit 130fc8
Packit 130fc8
    def __init__(self):
Packit 130fc8
        DBusException.__init__(self, "reply_handler not defined: if you define an error_handler you must also define a reply_handler")
Packit 130fc8
Packit 130fc8
class ValidationException(DBusException):
Packit 130fc8
Packit 130fc8
    include_traceback = True
Packit 130fc8
Packit 130fc8
    def __init__(self, msg=''):
Packit 130fc8
        DBusException.__init__(self, "Error validating string: %s"%msg)
Packit 130fc8
Packit 130fc8
class IntrospectionParserException(DBusException):
Packit 130fc8
Packit 130fc8
    include_traceback = True
Packit 130fc8
Packit 130fc8
    def __init__(self, msg=''):
Packit 130fc8
        DBusException.__init__(self, "Error parsing introspect data: %s"%msg)
Packit 130fc8
Packit 130fc8
class UnknownMethodException(DBusException):
Packit 130fc8
Packit 130fc8
    include_traceback = True
Packit 130fc8
    _dbus_error_name = 'org.freedesktop.DBus.Error.UnknownMethod'
Packit 130fc8
Packit 130fc8
    def __init__(self, method):
Packit 130fc8
        DBusException.__init__(self, "Unknown method: %s"%method)
Packit 130fc8
Packit 130fc8
class NameExistsException(DBusException):
Packit 130fc8
Packit 130fc8
    include_traceback = True
Packit 130fc8
Packit 130fc8
    def __init__(self, name):
Packit 130fc8
        DBusException.__init__(self, "Bus name already exists: %s"%name)