From 343d71b495df803dfbef3dc7875d96ef4f8588ab Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Fri, 20 Jan 2012 23:41:29 +0100 Subject: [PATCH] core: introduce CoreCriticalException. When this one is fired while trying to use a core lib, then the exception is raised to the user. This is actually used for window provider, while trying to create the window. + if an import error is detected, just warn the user about a missing module. --- kivy/core/__init__.py | 19 ++++++++++++++++++- kivy/core/window/window_pygame.py | 16 ++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/kivy/core/__init__.py b/kivy/core/__init__.py index ca23233bc..495dbb5b2 100644 --- a/kivy/core/__init__.py +++ b/kivy/core/__init__.py @@ -22,6 +22,9 @@ import os import kivy from kivy.logger import Logger +class CoreCriticalException(Exception): + pass + if 'KIVY_DOC' in os.environ: # stub for sphinx generation def core_select_lib(category, llist, create_instance=False): @@ -55,10 +58,24 @@ else: cls = cls() return cls + except ImportError as e: + Logger.warning('%s: Unable to use <%s> as %s' + 'provider' % (category.capitalize(), option, category)) + Logger.warning('%s: Associated module are missing' % + (category.capitalize())) + Logger.debug('', exc_info=e) + + except CoreCriticalException as e: + Logger.error('%s: Unable to use <%s> as %s' + 'provider' % (category.capitalize(), option, category)) + Logger.error('%s: The module raised an important error' % + (category.capitalize())) + raise + except Exception as e: Logger.warning('%s: Unable to use <%s> as %s' 'provider' % (category.capitalize(), option, category)) - Logger.debug('', exc_info = e) + Logger.debug('', exc_info=e) Logger.critical('%s: Unable to find any valuable %s provider ' 'at all!' % (category.capitalize(), category.capitalize())) diff --git a/kivy/core/window/window_pygame.py b/kivy/core/window/window_pygame.py index ad9b6f71e..c940604a5 100644 --- a/kivy/core/window/window_pygame.py +++ b/kivy/core/window/window_pygame.py @@ -4,8 +4,11 @@ Window Pygame: windowing provider based on Pygame __all__ = ('WindowPygame', ) -from . import WindowBase +# fail early if possible +import pygame +from . import WindowBase +from kivy.core import CoreCriticalException import os import sys from os.path import exists @@ -15,12 +18,6 @@ from kivy.logger import Logger from kivy.base import stopTouchApp, EventLoop from kivy.clock import Clock -try: - import pygame -except: - Logger.warning('WinPygame: Pygame is not installed !') - raise - # late binding glReadPixels = GL_RGB = GL_UNSIGNED_BYTE = None @@ -45,7 +42,10 @@ class WindowPygame(WindowBase): if sys.platform == 'linux2': self.flags |= pygame.RESIZABLE - pygame.display.init() + try: + pygame.display.init() + except pygame.error, e: + raise CoreCriticalException(e.message) multisamples = Config.getint('graphics', 'multisamples')