-
Notifications
You must be signed in to change notification settings - Fork 58
/
__init__.py
110 lines (80 loc) · 3.02 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from contextlib import contextmanager
from pathlib import Path
from typing import Union
from moderngl_window.conf import settings
from moderngl_window.exceptions import ImproperlyConfigured
from moderngl_window.resources.programs import programs # noqa
from moderngl_window.resources.textures import textures # noqa
# from moderngl_window.resources.tracks import tracks # noqa
from moderngl_window.resources.scenes import scenes # noqa
from moderngl_window.resources.data import data # noqa
def register_dir(path: Union[Path, str]) -> None:
"""Adds a resource directory for all resource types
Args:
path (Union[Path, str]): Directory path
"""
register_data_dir(path)
register_program_dir(path)
register_scene_dir(path)
register_texture_dir(path)
def register_program_dir(path: Union[Path, str]) -> None:
"""Adds a resource directory specifically for programs
Args:
path (Union[Path, str]): Directory path
"""
_append_unique_path(path, settings.PROGRAM_DIRS)
def register_texture_dir(path: Union[Path, str]) -> None:
"""Adds a resource directory specifically for textures
Args:
path (Union[Path, str]): Directory path
"""
_append_unique_path(path, settings.TEXTURE_DIRS)
def register_scene_dir(path: Union[Path, str]) -> None:
"""Adds a resource directory specifically for scenes
Args:
path (Union[Path, str]): Directory path
"""
_append_unique_path(path, settings.SCENE_DIRS)
def register_data_dir(path: Union[Path, str]) -> None:
"""Adds a resource directory specifically for data files
Args:
path (Union[Path, str]): Directory path
"""
_append_unique_path(path, settings.DATA_DIRS)
def _append_unique_path(path: Union[Path, str], dest: list):
path = Path(path)
if not path.is_absolute():
raise ImproperlyConfigured("Search path must be absolute: {}".format(path))
if not path.is_dir():
raise ImproperlyConfigured("Search path is not a directory: {}".format(path))
if not path.exists():
raise ImproperlyConfigured("Search path do not exist: {}".format(path))
for resource_path in dest:
if Path(resource_path).samefile(path):
break
else:
dest.append(Path(path).absolute())
@contextmanager
def temporary_dirs(dirs: Union[Path, str]):
"""Temporarily changes all resource directories
Example::
with temporary_dirs([path1, path2, path3]):
# Load some resource here
Args:
dirs (Union[Path,str]) list of paths to use
"""
data_dirs = settings.DATA_DIRS
program_dirs = settings.PROGRAM_DIRS
scene_dirs = settings.SCENE_DIRS
textures_dirs = settings.TEXTURE_DIRS
settings.DATA_DIRS = dirs
settings.PROGRAM_DIRS = dirs
settings.SCENE_DIRS = dirs
settings.TEXTURE_DIRS = dirs
try:
yield dirs
finally:
settings.DATA_DIRS = data_dirs
settings.PROGRAM_DIRS = program_dirs
settings.SCENE_DIRS = scene_dirs
settings.TEXTURE_DIRS = textures_dirs