PageRenderTime 24ms CodeModel.GetById 15ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

/synaptiks/kde/error.py

https://github.com/sibskull/synaptiks
Python | 105 lines | 43 code | 15 blank | 47 comment | 1 complexity | d2cd2d7a6939ec3b71b4cd9f3a09f699 MD5 | raw file
  1# -*- coding: utf-8 -*-
  2# Copyright (c) 2011, Sebastian Wiesner <lunaryorn@googlemail.com>
  3# All rights reserved.
  4
  5# Redistribution and use in source and binary forms, with or without
  6# modification, are permitted provided that the following conditions are met:
  7
  8# 1. Redistributions of source code must retain the above copyright notice,
  9#    this list of conditions and the following disclaimer.
 10# 2. Redistributions in binary form must reproduce the above copyright
 11#    notice, this list of conditions and the following disclaimer in the
 12#    documentation and/or other materials provided with the distribution.
 13
 14# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 15# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 17# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 18# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 19# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 20# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 21# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 22# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 23# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 24# POSSIBILITY OF SUCH DAMAGE.
 25
 26"""
 27    synaptiks.kde.error
 28    ===================
 29
 30    KDE-specific error handling functions.
 31
 32    This module provides functions to convert common touchpad exceptions into
 33    human-readable, localized and informative error messages.
 34
 35    .. moduleauthor::  Sebastian Wiesner  <lunaryorn@googlemail.com>
 36"""
 37
 38from __future__ import (print_function, division, unicode_literals,
 39                        absolute_import)
 40
 41from PyKDE4.kdecore import ki18nc
 42
 43from synaptiks import ISSUE_TRACKER_URL
 44
 45
 46NO_TOUCHPAD_ERROR_MESSAGE = ki18nc(
 47    '@info NoTouchpadError error message',
 48    '<title>No touchpad found</title>'
 49    '<para>No touchpad was found in this system.  If the system has a '
 50    'touchpad, please make sure that the '
 51    '<application>synaptics</application> driver is properly installed and '
 52    'configured.</para>'
 53    '<para>If your touchpad is not found, though the driver is installed and '
 54    'configured correctly, please compile detailed information about your '
 55    'touchpad hardware and report this issue to the '
 56    '<link url="%1">issue tracker</link>.</para>').subs(ISSUE_TRACKER_URL)
 57
 58
 59VERSION_ERROR_MESSAGE = ki18nc(
 60    '@info XInputVersionError error message',
 61    '<title>Version error</title>'
 62    '<para>The version of the XInput extension installed on your system is '
 63    'too old.  Version %1 was found, but at least version %2 is required.'
 64    '</para>'
 65    '<para>If you want to be able to configure your touchpad, you have to '
 66    'upgrade your system to a recent release of the Xorg display server.  '
 67    'This may likely involve a complete upgrade of your system.  Please '
 68    'excuse this inconvenience, but there is no way to make touchpad '
 69    'configuration work on systems as old as yours.</para>')
 70
 71
 72UNEXPECTED_ERROR_MESSAGE = ki18nc(
 73    '@info error message for unexpected errors',
 74    '<title>Unexpected error occurred</title>'
 75    '<para>An unexpected error occurred: <message>%2</message></para>'
 76    '<para>Please report this issue to the '
 77    '<link url="%1">issue tracker</link>.</para>').subs(ISSUE_TRACKER_URL)
 78
 79
 80def get_localized_error_message(error):
 81    """
 82    Try to find a localized, user-readable error description corresponding to
 83    the given :exc:`~exceptions.Exception` object.
 84
 85    This function needs a existing application object and proper locale
 86    configuration in order to work correctly.  Do not use before creating a
 87    :class:`~PyKDE4.kdeui.KApplication` object!
 88
 89    ``error`` is a subclass of :class:`~exceptions.Exception`.  Of course, not
 90    all possible subclasses are handled by this function, however all
 91    touchpad-related error classes defined in the synaptiks modules are
 92    supported.
 93
 94    Return a :func:`unicode` object containing the localized error message.
 95    """
 96    from synaptiks.touchpad import NoTouchpadError
 97    from synaptiks.x11.input import XInputVersionError
 98    if isinstance(error, NoTouchpadError):
 99        return NO_TOUCHPAD_ERROR_MESSAGE.toString()
100    elif isinstance(error, XInputVersionError):
101        actual = str(error.actual_version)
102        expected = str(error.expected_version)
103        return VERSION_ERROR_MESSAGE.subs(actual).subs(expected).toString()
104    else:
105        return UNEXPECTED_ERROR_MESSAGE.subs(unicode(error)).toString()