def __init__(self, window_settings={}, renderer_settings={}, context_enables=DEFAULT_CONTEXT_ENABLES): Renderer.__init__(self) window_settings = {**DEFAULT_WINDOW_SETTINGS, **window_settings} renderer_settings = {**DEFAULT_RENDERER_SETTINGS, **renderer_settings} self.size = window_settings['size'] for key, value in renderer_settings.items(): setattr(self, key, value) self.context = moderngl.create_standalone_context(require=430) window_cls = import_string(window_settings["class"]) self.wnd = window_cls(**window_settings) self.context.enable(context_enables) moderngl_window.activate_context(self.wnd, self.context) self.context = self.wnd.ctx # register event methods self.wnd.resize_func = self.resize self.wnd.iconify_func = self.iconify self.wnd.key_event_func = self.key_event self.wnd.mouse_position_event_func = self.mouse_position_event self.wnd.mouse_drag_event_func = self.mouse_drag_event self.wnd.mouse_scroll_event_func = self.mouse_scroll_event self.wnd.mouse_press_event_func = self.mouse_press_event self.wnd.mouse_release_event_func = self.mouse_release_event self.wnd.unicode_char_entered_func = self.unicode_char_entered self.wnd.close_func = self.close self.set_advance_time_function(self.advance_time)
def create_window_from_settings() -> BaseWindow: """ Creates a window using configured values in :py:attr:`moderngl_window.conf.Settings.WINDOW`. This will also activate the window/context. Returns: The Window instance """ window_cls = import_string(settings.WINDOW["class"]) window = window_cls(**settings.WINDOW) activate_context(window=window) return window
def get_window_cls(window: str = None) -> Type[BaseWindow]: """ Attempt to obtain a window class using the full dotted python path. This can be used to import custom or modified window classes. Args: window (str): Name of the window Returns: A reference to the requested window class. Raises exception if not found. """ logger.info("Attempting to load window class: %s", window) return import_string(window)
def get_finder(import_path: str): """ Get a finder class from an import path. This function uses an lru cache. Args: import_path: string representing an import path Return: An instance of the finder Raises: ImproperlyConfigured is the finder is not found """ Finder = import_string(import_path) if not issubclass(Finder, BaseFilesystemFinder): raise ImproperlyConfigured('Finder {} is not a subclass of .finders.FileSystemFinder'.format(import_path)) return Finder()
def test_import_string(self): """Ensure importing a class returns a type""" cls = import_string('moderngl_window.context.headless.Window') self.assertEqual(type(cls), type)
def test_import_error_missing_cls(self): """ImportError when missin class in module""" with self.assertRaises(ImportError): import_string('moderngl_window.context.headless.NadaWindow')
def test_no_dotted_path(self): with self.assertRaises(ImportError): import_string('nonexistingmodule')
def test_import_error(self): """ImportError should be raised""" with self.assertRaises(ImportError): import_string('this.is.bs')
def _loader_cls(self, python_path: str): return import_string(python_path)