Browse Source

python: remove the black magic from magnum.platform.

Implicitly importing whole toolkits was not a good idea.
pull/8/head
Vladimír Vondruš 7 years ago
parent
commit
6bce9fbeb2
  1. 25
      doc/python/magnum.platform.rst
  2. 15
      src/python/magnum/__init__.py
  3. 22
      src/python/magnum/platform/__init__.py
  4. 16
      src/python/magnum/test/__init__.py

25
doc/python/magnum.platform.rst

@ -25,18 +25,16 @@
.. py:module:: magnum.platform
While concrete implementations of :py:`Application` and
:py:`WindowlessApplication` classes are available in the submodules,
there's a convenience logic importing the most fitting implementation
that's available directly into the `platform` module, meaning you can do
just this without having to think about a particular implementation, for
example:
Concrete implementations of :py:`Application` and :py:`WindowlessApplication`
classes are available in submodules, and it's up to you which one you
choose. For example:
.. code:: py
from magnum import platform
import magnum.platform.sdl2
class MyApp(platform.Application): # platform.sdl2.Application
class MyApp(platform.sdl2.Application):
...
The same goes for :py:`WindowlessApplication` implementations, for example:
@ -44,11 +42,16 @@
.. code:: py
from magnum import platform
import magnum.platform.egl
class MyApp(platform.WindowlessApplication): # platform.egl.WindowlessApp
class MyApp(platform.egl.WindowlessApplication):
...
.. block-warning:: Subject to change
Alternatively, if you want to narrow down the mention of a particular
toolkit to just one line, you can also do:
At the moment, there's no way to specify a different priority order or
disable the auto-import altogether.
.. code:: py
from magnum.platform.sdl2 import Application
class MyApp(Application):

15
src/python/magnum/__init__.py

@ -43,21 +43,6 @@ if 'platform' in globals():
for i in ['glfw', 'sdl2', 'glx', 'wgl', 'egl']:
if hasattr(platform, i): sys.modules['magnum.platform.' + i] = getattr(platform, i)
# Bring one application implementation directly into the platform module.
# The same logic is duplicated in platform/__init__.py.
if hasattr(platform, 'sdl2'):
platform.Application = platform.sdl2.Application
elif hasattr(platform, 'glfw'):
platform.Application = platform.glfw.Application
# Same for windowless apps
if hasattr(platform, 'glx'):
platform.WindowlessApplication = platform.glx.WindowlessApplication
elif hasattr(platform, 'wgl'):
platform.WindowlessApplication = platform.wgl.WindowlessApplication
elif hasattr(platform, 'egl'):
platform.WindowlessApplication = platform.egl.WindowlessApplication
# Scenegraph has subpackages
if 'scenegraph' in globals():
for i in ['matrix', 'trs']:

22
src/python/magnum/platform/__init__.py

@ -24,25 +24,3 @@
#
"""Platform-specific application and context creation"""
# This logic is repeated in magnum's main __init__.py for static builds as
# there magnum.platform is a submodule inside the native _magnum module.
try:
from .glx import WindowlessApplication
except ImportError: # pragma: no cover
try:
from .wgl import WindowlessApplication
except ImportError:
try:
from .egl import WindowlessApplication
except ImportError:
pass
try:
from .sdl2 import Application
except ImportError: # pragma: no cover
try:
from .glfw import Application
except ImportError:
pass

16
src/python/magnum/test/__init__.py

@ -33,10 +33,22 @@ sys.path = [os.path.join(os.path.dirname(__file__), os.environ.get('CMAKE_BINARY
from magnum import *
from magnum import gl, platform
try:
from magnum.platform.glx import WindowlessApplication
except ImportError:
try:
from magnum.platform.wgl import WindowlessApplication
except ImportError:
try:
from magnum.platform.egl import WindowlessApplication
except ImportError:
WindowlessApplication = None
def setUpModule():
if os.environ.get('MAGNUM_SKIP_GL_TESTS') == 'ON':
raise unittest.SkipTest('GL tests skipped')
if not hasattr(platform, 'WindowlessApplication'): # pragma: no cover
if not WindowlessApplication:
raise unittest.SkipTest('no WindowlessApplication found')
class GLTestCase(unittest.TestCase):
@ -45,7 +57,7 @@ class GLTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
if not GLTestCase.app:
GLTestCase.app = platform.WindowlessApplication()
GLTestCase.app = WindowlessApplication()
def assertNoGLError(self):
self.assertEqual(gl.Renderer.error, gl.Renderer.Error.NO_ERROR)

Loading…
Cancel
Save