PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/synaptiks/kde/error.py

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