diff --git a/glcontext/__init__.py b/glcontext/__init__.py index 8f7bd29..1b3e762 100644 --- a/glcontext/__init__.py +++ b/glcontext/__init__.py @@ -3,21 +3,11 @@ __version__ = '2.3.7' -def default_backend(): - """Get default backend based on the detected platform. - Supports detecting an existing context and standalone contexts. - If no context if found for the platform we return the linux backend. - - Example:: - - # Get the available backend - backend = get_default_backend(standalone=False/True) - # Create an opengl 3.3 context or detect the currently active - # context requiring at least opengl 3.3 support. - ctx = backend(330) +def get_default_backend_name(): + """Get the default backend name based on the detected platform. Returns: - A backend object for creating and/or detecting context + str: The name of the default backend ('wgl', 'x11', or 'darwin') """ PLATFORMS = {'windows', 'linux', 'darwin'} @@ -32,23 +22,52 @@ def default_backend(): target = 'linux' if target == 'windows': - return _wgl() + return 'wgl' if target == 'linux': - return _x11() + return 'x11' if target == 'darwin': - return _darwin() + return 'darwin' raise ValueError("Cannot find suitable default backend") +def default_backend(): + """Get default backend based on the detected platform. + Supports detecting an existing context and standalone contexts. + If no context if found for the platform we return the linux backend. + + Example:: + + # Get the available backend + backend = get_default_backend(standalone=False/True) + # Create an opengl 3.3 context or detect the currently active + # context requiring at least opengl 3.3 support. + ctx = backend(330) + + Returns: + A backend object for creating and/or detecting context + """ + return get_backend_by_name(get_default_backend_name()) + + def get_backend_by_name(name: str): """Request a specific backend by name""" - if name == 'egl': - return _egl() - - raise ValueError("Cannot find supported backend: '{}'".format(name)) + BACKENDS = { + 'wgl': _wgl, + 'x11': _x11, + 'darwin': _darwin, + 'egl': _egl, + } + + backend = BACKENDS.get(name) + if backend: + return backend() + + raise ValueError("Cannot find supported backend: '{}'. Supported backends: {}".format( + name, ', '.join(BACKENDS.keys()) + )) def _wgl():