def __init__(self): settings_module = os.environ.get(ENVIRONMENT_VARIABLE) if not settings_module: raise ImproperlyConfigured( "Settings module environment variable not found. " "You must defined the environment variable {}".format( ENVIRONMENT_VARIABLE)) # Update this dict from global settings for setting in dir(default_settings): if setting.isupper(): setattr(self, setting, getattr(default_settings, setting)) # Read the supplied settings module self.SETTINGS_MODULE = settings_module module = importlib.import_module(self.SETTINGS_MODULE) if not module: raise ImproperlyConfigured( "Settings module '{}' not found. ".format( self.SETTINGS_MODULE)) for setting in dir(module): if setting.isupper(): value = getattr(module, setting) # TODO: Add more validation here setattr(self, setting, value)
def handle(self, *args, **options): demosys.setup() manager_path = getattr(settings, 'EFFECT_MANAGER') if not manager_path: raise ImproperlyConfigured( "EFFECT_MANAGER not properly configured in settings") print(manager_path) try: manager_cls = import_string(manager_path) except ImportError as e: raise ImproperlyConfigured( "EFFECT_MANAGER '{}' failed to initialize: {}".format( manager_path, e)) manager = manager_cls() demosys.run(manager=manager)
def get(self, path, create=False, **kwargs) -> Scene: """ Get or create a scene object. This may return an empty object that will be filled during load based on the ``create`` parameter. :param path: Path to the scene file(s) :param create: (bool) Create an empty scene object if it doesn't exist :return: Scene object """ # Figure out what scene loader class should be used for loader_name in settings.SCENE_LOADERS: loader_cls = import_string(loader_name) if loader_cls.supports_file(path): if path not in self.scenes: loader_cls = import_string(loader_name) self.scenes[path] = Scene(path, loader=loader_cls(path), **kwargs) break else: raise ImproperlyConfigured( "Scene {} has no loader class registered. Check settings.SCENE_LOADERS" ) return self.scenes[path]
def load_shader(self, shader, name=None, reload=False): """ Loads a single shader adding it to the shader registry :param shader: The shader to load :param name: Unique name in the registry. Usually the path :param reload: (bool) Are we reloading the shader? """ if name is None: name = shader.path finders = list(get_finders()) for finder in finders: path = finder.find(name) if path: print(" - {}".format(path)) with open(path, 'r') as fd: shader.set_source(fd.read()) try: shader.prepare(reload=reload) except (ShaderError, moderngl.Error) as err: print("ShaderError: ", err) if not reload: raise except Exception as err: print(err) raise break else: raise ImproperlyConfigured("Cannot find shader {}".format(name))
def load(self, reload=False): """ Loads all the shaders using the configured finders. """ finders = list(get_finders()) print("Loading shaders:") for name, shader in self.shaders.items(): for finder in finders: path = finder.find(name) if path: print(" - {}".format(path)) with open(path, 'r') as fd: shader.set_source(fd.read()) try: shader.prepare() except ShaderError as err: print("ShaderError: ", err) if not reload: raise except Exception as err: print(err) raise break else: raise ImproperlyConfigured( "Cannot find shader {}".format(name))
def get_finder(import_path): """ Get a finder class from an import path. Raises ``demosys.core.exceptions.ImproperlyConfigured`` if the finder is not found. This function uses an lru cache. :param import_path: string representing an import path :return: An instance of the finder """ Finder = import_string(import_path) if not issubclass(Finder, finders.BaseFileSystemFinder): raise ImproperlyConfigured('Finder {} is not a subclass of core.finders.FileSystemFinder'.format(import_path)) return Finder()
def load(self): finders = list(get_finders()) print("Loading scenes:") for name, scene in self.scenes.items(): for finder in finders: path = finder.find(name) if path: print(" - {}".format(path)) scene.load(path) break else: raise ImproperlyConfigured("Cannot find scene {}".format(name)) self._on_loaded()
def load(self): """ Loads all the textures using the configured finders. """ finders = list(get_finders()) print("Loading textures:") for name, texture in self.textures.items(): for finder in finders: path = finder.find(name) if path: print(" - {}".format(path)) image = Image.open(path) texture.set_image(image) break else: raise ImproperlyConfigured("Cannot find texture {}".format(name))
def __init__(self, path, mode='binary'): """ :param path: file with relative path :param mode: What mode the file should be read ('b': binary, 't': text) """ self.path = path self.mode = mode self.data = None load_funcs = self._get_load_funcs() try: self.func = load_funcs[mode] except KeyError: raise ImproperlyConfigured( "{} doesn't support mode '{}'. Options are {}".format(self.__class__, mode, load_funcs.keys()) )
def load(self): """ Loads all the data files using the configured finders. """ finders = list(get_finders()) print("Loading data files:") for name, data_file in self.file_map.items(): for finder in finders: path = finder.find(name) if path: print(" - {}".format(path)) data_file.load(path) break else: raise ImproperlyConfigured("Cannot find data file {}".format(name)) self._on_loaded()
def get(self, name, create=False, cls=Data) -> Data: """ Get or create a Data object. :param name: data file with relative path :param crate: (bool) register a new resource (or fetch existing) :param cls: (Data class) custom data class :return: Data object """ if not hasattr(cls, 'load'): raise ImproperlyConfigured("{} must have a load(path) method".format(cls.__class__)) key = name.lower() data_file = self.file_map.get(key) if not data_file: data_file = cls(name) self.files.append(data_file) self.file_map[key] = data_file return data_file
def __init__(self): self.width = settings.WINDOW['size'][0] self.height = settings.WINDOW['size'][1] self.resizable = settings.WINDOW.get('resizable') or False self.title = settings.WINDOW.get('title') or "demosys-py" self.aspect_ratio = settings.WINDOW.get('aspect_ratio', 16 / 9) if not glfw.init(): raise ValueError("Failed to initialize glfw") self.check_glfw_version() glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, settings.OPENGL['version'][0]) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, settings.OPENGL['version'][1]) profile = PROFILES.get(settings.OPENGL['profile']) if not profile: raise ImproperlyConfigured( "OPENGL profile {} not supported".format(profile)) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) if settings.OPENGL.get('forward_compat'): glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL.GL_TRUE) if not settings.WINDOW.get('resizable'): glfw.window_hint(glfw.RESIZABLE, GL.GL_FALSE) glfw.window_hint(glfw.DOUBLEBUFFER, GL.GL_TRUE) glfw.window_hint(glfw.RED_BITS, 8) glfw.window_hint(glfw.GREEN_BITS, 8) glfw.window_hint(glfw.BLUE_BITS, 8) glfw.window_hint(glfw.ALPHA_BITS, 8) glfw.window_hint(glfw.DEPTH_BITS, 24) glfw.window_hint(glfw.STENCIL_BITS, 8) monitor = None if settings.WINDOW.get('fullscreen'): monitor = glfw.get_primary_monitor() modes = glfw.get_video_modes(monitor) print(modes) # Pick a mode close to the configured one for mode in modes: w, h = mode[0] if self.width <= w: break self.width, self.height = mode[0] print("Window size:", self.width, self.height) self.window = glfw.create_window(self.width, self.height, self.title, monitor, None) if not self.window: glfw.terminate() raise ValueError("Failed to create window") if not settings.WINDOW.get('cursor'): glfw.set_input_mode(self.window, glfw.CURSOR, glfw.CURSOR_DISABLED) # Get the actual buffer size of the window # This is important for some displays like Apple's Retina as reported window sizes are virtual self.buffer_width, self.buffer_height = glfw.get_framebuffer_size( self.window) print("Frame buffer size:", self.buffer_width, self.buffer_height) w, h = glfw.get_window_size(self.window) print("Actual window size:", w, h) glfw.make_context_current(self.window) print("Context Version:", GL.glGetString(GL.GL_VERSION).decode()) # The number of screen updates to wait from the time glfwSwapBuffers # was called before swapping the buffers and returning if settings.WINDOW.get('vsync'): glfw.swap_interval(1)
def __init__(self): if not hasattr(settings, 'TEXTURE_DIRS'): raise ImproperlyConfigured( "Settings module don't define SHADER_DIRS." "This is required when using a FileSystemFinder.") super().__init__(settings.TEXTURE_DIRS)