Skip to content

Commit

Permalink
Support setting backend parameters using env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Mar 22, 2020
1 parent ad1e7a8 commit 4793341
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@

# Change Log

## 2.0.1

* Support setting backend arguments using environment variables.
* `GLCONTEXT_GLVERSION` for setting opengl version
* `GLCONTEXT_LINUX_LIBGL` for specifying libgl name
* `GLCONTEXT_LINUX_LIBX11` for specifying libx11 name
* `GLCONTEXT_LINUX_LIBEGL` for specifying libegl name
* `GLCONTEXT_WIN_LIBGL` for specifying dll name
* x11: More details in error messages

## 2.0.0

Support passing in values to backends for more detailed
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ Parameters
* `libgl` (`str`): Name of gl library to load (default: `libGL.so`)
* `libegl` (`str`): Name of gl library to load (default: `libEGL.so`)

## Environment Variables

Environment variables can be set to configure backends.
These will get first priority if defined.

```bash
# Override OpenGL version code. For example: 410 (for opengl 4.1)
GLCONTEXT_GLVERSION
# Override libgl on linux. For example: libGL.1.so
GLCONTEXT_LINUX_LIBGL
# Override libx11 on linux. For exampleØ libX11.x.so
GLCONTEXT_LINUX_LIBX11
# Override libegl on linux. For exampleØ libEGL.x.so
GLCONTEXT_LINUX_LIBEGL
# Override gl dll on windows. For example: opengl32_custom.dll
GLCONTEXT_WIN_LIBGL
```

## Running tests

```
Expand Down
42 changes: 33 additions & 9 deletions glcontext/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
__version__ = '2.0.0'
import os

__version__ = '2.1.0'


def default_backend():
Expand Down Expand Up @@ -54,33 +56,48 @@ def _wgl():
from glcontext import wgl

def create(*args, **kwargs):
supported_args = ['glversion', 'mode', 'libgl']
return wgl.create_context(**_strip_kwargs(kwargs, supported_args))
_apply_env_var(kwargs, 'glversion', 'GLCONTEXT_GLVERSION', arg_type=int)
_apply_env_var(kwargs, 'libgl', 'GLCONTEXT_WIN_LIBGL')
kwargs = _strip_kwargs(kwargs, ['glversion', 'mode', 'libgl'])
return wgl.create_context(**kwargs)

return create


def _x11():
"""Create x11 backend"""
from glcontext import x11

def create(*args, **kwargs):
supported_args = ['glversion', 'mode', 'libgl', 'libx11']
return x11.create_context(**_strip_kwargs(kwargs, supported_args))
_apply_env_var(kwargs, 'glversion', 'GLCONTEXT_GLVERSION', arg_type=int)
_apply_env_var(kwargs, 'libgl', 'GLCONTEXT_LINUX_LIBGL')
_apply_env_var(kwargs, 'libx11', 'GLCONTEXT_LINUX_LIBX11')
kwargs = _strip_kwargs(kwargs, ['glversion', 'mode', 'libgl', 'libx11'])
return x11.create_context(**kwargs)

return create


def _darwin():
"""Create darwin/cgl context"""
from glcontext import darwin

def create(*args, **kwargs):
supported_args = ['mode']
return darwin.create_context(**_strip_kwargs(kwargs, supported_args))
return darwin.create_context(**_strip_kwargs(kwargs, ['mode']))

return create


def _egl():
from glcontext import egl

def create(*args, **kwargs):
supported_args = ['glversion', 'mode', 'libgl', 'libegl']
return egl.create_context(**_strip_kwargs(kwargs, supported_args))
_apply_env_var(kwargs, 'glversion', 'GLCONTEXT_GLVERSION', arg_type=int)
_apply_env_var(kwargs, 'libgl', 'GLCONTEXT_LINUX_LIBGL')
_apply_env_var(kwargs, 'libegl', 'GLCONTEXT_LINUX_LIBEGL')
kwargs = _strip_kwargs(kwargs, ['glversion', 'mode', 'libgl', 'libegl'])
return egl.create_context(**kwargs)

return create


Expand All @@ -95,3 +112,10 @@ def _strip_kwargs(kwargs: dict, supported_args: list):
- Removes unsupported arguments
"""
return {k: v for k, v in kwargs.items() if v is not None and k in supported_args}


def _apply_env_var(kwargs, arg_name, env_name, arg_type=str):
"""Injects an environment variable into the arg dict if present"""
value = os.environ.get(env_name, kwargs.get(arg_name))
if value:
kwargs[arg_name] = arg_type(value)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

setup(
name='glcontext',
version='2.0.0',
version='2.1.0',
description='Portable OpenGL Context',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 4793341

Please sign in to comment.