Skip to content

Commit bda0b6a

Browse files
authored
Merge pull request #30 from Contraz/textwriter
Basic textwriter + improved resource features
2 parents 7fe6c25 + 85f8a8b commit bda0b6a

File tree

27 files changed

+606
-83
lines changed

27 files changed

+606
-83
lines changed

demosys/conf/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@
99

1010
ENVIRONMENT_VARIABLE = "DEMOSYS_SETTINGS_MODULE"
1111

12+
# pylint: disable=C0103
13+
1214

1315
class Settings:
16+
SHADER_DIRS = []
17+
TEXTURE_DIRS = []
18+
DATA_DIRS = []
19+
SETTINGS_MODULE = None
20+
1421
def __init__(self):
1522
settings_module = os.environ.get(ENVIRONMENT_VARIABLE)
1623
if not settings_module:
@@ -58,5 +65,17 @@ def add_shader_dir(self, dir):
5865
dirs.append(dir)
5966
self.SHADER_DIRS = dirs
6067

68+
def add_texture_dir(self, directory):
69+
"""Hack in texture directory"""
70+
dirs = list(self.TEXTURE_DIRS)
71+
dirs.append(directory)
72+
self.TEXTURE_DIRS = dirs
73+
74+
def add_data_dir(self, directory):
75+
"""Hack in a data directory"""
76+
dirs = list(self.DATA_DIRS)
77+
dirs.append(directory)
78+
self.DATA_DIRS = dirs
79+
6180

6281
settings = Settings()

demosys/conf/default_settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
'SCENE_DIRS',
2121
'SCENE_FINDERS',
2222
'SCENE_LOADERS',
23+
'DATA_DIRS',
24+
'DATA_FINDERS',
2325
)
2426

2527
DEBUG = False
@@ -98,3 +100,9 @@
98100
'demosys.scene.loaders.gltf.GLTF2',
99101
'demosys.scene.loaders.wavefront.ObjLoader',
100102
)
103+
104+
DATA_DIRS = ()
105+
DATA_FINDERS = (
106+
"demosys.core.datafiles.finders.FileSystemFinder",
107+
"demosys.core.datafiles.finders.EffectDirectoriesFinder",
108+
)

demosys/core/__init__.py

Whitespace-only changes.

demosys/core/datafiles/finders.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import functools
2+
3+
from demosys.core import finders
4+
from demosys.conf import settings
5+
from demosys.core.exceptions import ImproperlyConfigured
6+
from demosys.utils.module_loading import import_string
7+
8+
9+
class FileSystemFinder(finders.BaseFileSystemFinder):
10+
"""Find data in ``DATA_DIRS``"""
11+
settings_attr = 'DATA_DIRS'
12+
13+
14+
class EffectDirectoriesFinder(finders.BaseEffectDirectoriesFinder):
15+
"""Finds data in the registered effects"""
16+
directory = 'data'
17+
18+
19+
def get_finders():
20+
for finder in settings.DATA_FINDERS:
21+
yield get_finder(finder)
22+
23+
24+
@functools.lru_cache(maxsize=None)
25+
def get_finder(import_path):
26+
"""
27+
Get a finder class from an import path.
28+
Raises ``demosys.core.exceptions.ImproperlyConfigured`` if the finder is not found.
29+
This function uses an lru cache.
30+
31+
:param import_path: string representing an import path
32+
:return: An instance of the finder
33+
"""
34+
Finder = import_string(import_path)
35+
if not issubclass(Finder, finders.BaseFileSystemFinder):
36+
raise ImproperlyConfigured('Finder {} is not a subclass of core.finders.FileSystemFinder'.format(import_path))
37+
return Finder()

demosys/core/finders.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@
33
"""
44
import os
55
from collections import namedtuple
6+
from demosys.conf import settings
7+
from demosys.core.exceptions import ImproperlyConfigured
68

79
FinderEntry = namedtuple('FinderEntry', ['path', 'abspath', 'exists'])
810

911

10-
class FileSystemFinder:
11-
"""Find files in the local file system"""
12-
def __init__(self, paths):
13-
self.paths = paths
12+
class BaseFileSystemFinder:
13+
"""Base class for searching directory lists"""
14+
settings_attr = None
15+
16+
def __init__(self):
17+
if not hasattr(settings, self.settings_attr):
18+
raise ImproperlyConfigured(
19+
"Settings module don't define TEXTURE_DIRS."
20+
"This is required when using a FileSystemFinder."
21+
)
22+
self.paths = getattr(settings, self.settings_attr)
23+
1424
self._cache = {}
1525

1626
def find(self, path):
@@ -30,8 +40,8 @@ def _find(self, path):
3040
:param path: The path to find
3141
:return: The absolute path to the file or None if not found
3242
"""
33-
for p in self.paths:
34-
abspath = os.path.join(p, path)
43+
for entry in self.paths:
44+
abspath = os.path.join(entry, path)
3545
if os.path.exists(abspath):
3646
self.cache(abspath, abspath)
3747
return abspath
@@ -48,9 +58,11 @@ def find_cached(self, path):
4858
:param path: The path to the file
4959
:return: The absolute path to the file or None
5060
"""
51-
e = self._cache.get(path)
52-
if e.exists:
53-
return e.abspath
61+
entry = self._cache.get(path)
62+
63+
if entry.exists:
64+
return entry.abspath
65+
5466
return None
5567

5668
def cache(self, path, abspath, exists=True):
@@ -63,3 +75,16 @@ def cache(self, path, abspath, exists=True):
6375
:param exists: Did the file exist? (bool)
6476
"""
6577
self._cache[path] = FinderEntry(path=path, abspath=abspath, exists=exists)
78+
79+
80+
class BaseEffectDirectoriesFinder(BaseFileSystemFinder):
81+
"""Base class for searching effect directories"""
82+
directory = None
83+
84+
def __init__(self):
85+
from demosys.effects.registry import effects
86+
self.paths = list(effects.get_dirs())
87+
self._cache = {}
88+
89+
def find(self, path):
90+
return self._find(os.path.join(self.directory, path))

demosys/core/scenefiles/finders.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,19 @@
11
import functools
2-
import os
2+
33
from demosys.core import finders
44
from demosys.conf import settings
55
from demosys.core.exceptions import ImproperlyConfigured
66
from demosys.utils.module_loading import import_string
77

8-
SCENE_DIR = 'scenes'
9-
108

11-
class FileSystemFinder(finders.FileSystemFinder):
9+
class FileSystemFinder(finders.BaseFileSystemFinder):
1210
"""Find textures in ``SCENE_DIRS``"""
13-
def __init__(self):
14-
if not hasattr(settings, 'SCENE_DIRS'):
15-
raise ImproperlyConfigured(
16-
"Settings module don't define SCENE_DIRS."
17-
"This is required when using a FileSystemFinder."
18-
)
19-
super().__init__(settings.TEXTURE_DIRS)
11+
settings_attr = 'SCENE_DIRS'
2012

21-
# TODO: Use values from settings to filter files
22-
# def find(self, path):
23-
# pass
2413

25-
26-
class EffectDirectoriesFinder(finders.FileSystemFinder):
14+
class EffectDirectoriesFinder(finders.BaseEffectDirectoriesFinder):
2715
"""Finds textures in the registered effects"""
28-
def __init__(self):
29-
from demosys.effects.registry import effects
30-
dirs = list(effects.get_dirs())
31-
super().__init__(dirs)
32-
33-
# TODO: Use values from settings to filter texture files
34-
def find(self, path):
35-
return self._find(os.path.join(SCENE_DIR, path))
16+
directory = 'scenes'
3617

3718

3819
def get_finders():
@@ -51,6 +32,6 @@ def get_finder(import_path):
5132
:return: An instance of the finder
5233
"""
5334
Finder = import_string(import_path)
54-
if not issubclass(Finder, finders.FileSystemFinder):
35+
if not issubclass(Finder, finders.BaseFileSystemFinder):
5536
raise ImproperlyConfigured('Finder {} is not a subclass of core.finders.FileSystemFinder'.format(import_path))
5637
return Finder()

demosys/core/shaderfiles/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
11
import functools
2-
import os
2+
33
from demosys.core import finders
44
from demosys.conf import settings
55
from demosys.core.exceptions import ImproperlyConfigured
66
from demosys.utils.module_loading import import_string
77

8-
SHADER_DIR = 'shaders'
9-
108

11-
class FileSystemFinder(finders.FileSystemFinder):
9+
class FileSystemFinder(finders.BaseFileSystemFinder):
1210
"""Find shaders in ``SHADER_DIRS``"""
13-
def __init__(self):
14-
if not hasattr(settings, 'SHADER_DIRS'):
15-
raise ImproperlyConfigured(
16-
"Settings module don't define SHADER_DIRS."
17-
"This is required when using a FileSystemFinder."
18-
)
19-
super().__init__(settings.SHADER_DIRS)
11+
settings_attr = 'SHADER_DIRS'
2012

2113

22-
class EffectDirectoriesFinder(finders.FileSystemFinder):
14+
class EffectDirectoriesFinder(finders.BaseEffectDirectoriesFinder):
2315
"""Finds shaders in the registered effects"""
24-
def __init__(self):
25-
from demosys.effects.registry import effects
26-
dirs = list(effects.get_dirs())
27-
super().__init__(dirs)
28-
29-
# TODO: Use values from settings to filter shader files
30-
def find(self, path):
31-
return self._find(os.path.join(SHADER_DIR, path))
16+
directory = 'shaders'
3217

3318

3419
def get_finders():
@@ -47,6 +32,6 @@ def get_finder(import_path):
4732
:return: An instance of the finder
4833
"""
4934
Finder = import_string(import_path)
50-
if not issubclass(Finder, finders.FileSystemFinder):
35+
if not issubclass(Finder, finders.BaseFileSystemFinder):
5136
raise ImproperlyConfigured('Finder {} is not a subclass of core.finders.FileSystemFinder'.format(import_path))
5237
return Finder()

demosys/core/texturefiles/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,19 @@
11
import functools
2-
import os
2+
33
from demosys.core import finders
44
from demosys.conf import settings
55
from demosys.core.exceptions import ImproperlyConfigured
66
from demosys.utils.module_loading import import_string
77

8-
TEXTURE_DIR = 'textures'
9-
108

11-
class FileSystemFinder(finders.FileSystemFinder):
9+
class FileSystemFinder(finders.BaseFileSystemFinder):
1210
"""Find textures in ``TEXTURE_DIRS``"""
13-
def __init__(self):
14-
if not hasattr(settings, 'TEXTURE_DIRS'):
15-
raise ImproperlyConfigured(
16-
"Settings module don't define TEXTURE_DIRS."
17-
"This is required when using a FileSystemFinder."
18-
)
19-
super().__init__(settings.TEXTURE_DIRS)
11+
settings_attr = 'TEXTURE_DIRS'
2012

21-
# TODO: Use values from settings to filter texture files
22-
# def find(self, path):
23-
# pass
2413

25-
26-
class EffectDirectoriesFinder(finders.FileSystemFinder):
14+
class EffectDirectoriesFinder(finders.BaseEffectDirectoriesFinder):
2715
"""Finds textures in the registered effects"""
28-
def __init__(self):
29-
from demosys.effects.registry import effects
30-
dirs = list(effects.get_dirs())
31-
super().__init__(dirs)
32-
33-
# TODO: Use values from settings to filter texture files
34-
def find(self, path):
35-
return self._find(os.path.join(TEXTURE_DIR, path))
16+
directory = 'textures'
3617

3718

3819
def get_finders():
@@ -51,6 +32,6 @@ def get_finder(import_path):
5132
:return: An instance of the finder
5233
"""
5334
Finder = import_string(import_path)
54-
if not issubclass(Finder, finders.FileSystemFinder):
35+
if not issubclass(Finder, finders.BaseFileSystemFinder):
5536
raise ImproperlyConfigured('Finder {} is not a subclass of core.finders.FileSystemFinder'.format(import_path))
5637
return Finder()

0 commit comments

Comments
 (0)