Skip to content

Commit 25e3806

Browse files
committed
Finders: Remove duplcate finder code
1 parent b7e5a53 commit 25e3806

File tree

5 files changed

+22
-84
lines changed

5 files changed

+22
-84
lines changed

demosys/core/datafiles/finders.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import functools
2-
31
from demosys.core import finders
42
from demosys.conf import settings
5-
from demosys.core.exceptions import ImproperlyConfigured
6-
from demosys.utils.module_loading import import_string
73

84

95
class FileSystemFinder(finders.BaseFileSystemFinder):
@@ -18,20 +14,4 @@ class EffectDirectoriesFinder(finders.BaseEffectDirectoriesFinder):
1814

1915
def get_finders():
2016
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()
17+
yield finders.get_finder(finder)

demosys/core/finders.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""
22
Base finders
33
"""
4+
import functools
45
from collections import namedtuple
56
from pathlib import Path
67

78
from demosys.conf import settings
89
from demosys.core.exceptions import ImproperlyConfigured
10+
from demosys.utils.module_loading import import_string
911

1012
FinderEntry = namedtuple('FinderEntry', ['path', 'abspath', 'exists'])
1113

@@ -57,3 +59,19 @@ def __init__(self):
5759
def find(self, path: Path):
5860
path = Path(self.directory) / Path(path)
5961
return super().find(path)
62+
63+
64+
@functools.lru_cache(maxsize=None)
65+
def get_finder(import_path):
66+
"""
67+
Get a finder class from an import path.
68+
Raises ``demosys.core.exceptions.ImproperlyConfigured`` if the finder is not found.
69+
This function uses an lru cache.
70+
71+
:param import_path: string representing an import path
72+
:return: An instance of the finder
73+
"""
74+
Finder = import_string(import_path)
75+
if not issubclass(Finder, BaseFileSystemFinder):
76+
raise ImproperlyConfigured('Finder {} is not a subclass of core.finders.FileSystemFinder'.format(import_path))
77+
return Finder()

demosys/core/scenefiles/finders.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import functools
2-
31
from demosys.core import finders
42
from demosys.conf import settings
5-
from demosys.core.exceptions import ImproperlyConfigured
6-
from demosys.utils.module_loading import import_string
73

84

95
class FileSystemFinder(finders.BaseFileSystemFinder):
@@ -18,20 +14,4 @@ class EffectDirectoriesFinder(finders.BaseEffectDirectoriesFinder):
1814

1915
def get_finders():
2016
for finder in settings.SCENE_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()
17+
yield finders.get_finder(finder)
Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import functools
2-
31
from demosys.core import finders
42
from demosys.conf import settings
5-
from demosys.core.exceptions import ImproperlyConfigured
6-
from demosys.utils.module_loading import import_string
73

84

95
class FileSystemFinder(finders.BaseFileSystemFinder):
@@ -18,20 +14,4 @@ class EffectDirectoriesFinder(finders.BaseEffectDirectoriesFinder):
1814

1915
def get_finders():
2016
for finder in settings.SHADER_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()
17+
yield finders.get_finder(finder)
Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import functools
2-
31
from demosys.core import finders
42
from demosys.conf import settings
5-
from demosys.core.exceptions import ImproperlyConfigured
6-
from demosys.utils.module_loading import import_string
73

84

95
class FileSystemFinder(finders.BaseFileSystemFinder):
@@ -18,20 +14,4 @@ class EffectDirectoriesFinder(finders.BaseEffectDirectoriesFinder):
1814

1915
def get_finders():
2016
for finder in settings.TEXTURE_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()
17+
yield finders.get_finder(finder)

0 commit comments

Comments
 (0)