From 6bce9fbeb22a1ddfc06ba8b221b616c73262f1cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 17 Sep 2019 12:03:41 +0200 Subject: [PATCH] python: remove the black magic from magnum.platform. Implicitly importing whole toolkits was not a good idea. --- doc/python/magnum.platform.rst | 25 ++++++++++++++----------- src/python/magnum/__init__.py | 15 --------------- src/python/magnum/platform/__init__.py | 22 ---------------------- src/python/magnum/test/__init__.py | 16 ++++++++++++++-- 4 files changed, 28 insertions(+), 50 deletions(-) diff --git a/doc/python/magnum.platform.rst b/doc/python/magnum.platform.rst index 5c7e09f..90d83a0 100644 --- a/doc/python/magnum.platform.rst +++ b/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): diff --git a/src/python/magnum/__init__.py b/src/python/magnum/__init__.py index 860c40f..a97095c 100644 --- a/src/python/magnum/__init__.py +++ b/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']: diff --git a/src/python/magnum/platform/__init__.py b/src/python/magnum/platform/__init__.py index 64991c3..ac91cde 100644 --- a/src/python/magnum/platform/__init__.py +++ b/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 diff --git a/src/python/magnum/test/__init__.py b/src/python/magnum/test/__init__.py index 76000eb..779ca64 100644 --- a/src/python/magnum/test/__init__.py +++ b/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)