1
1
"""
2
2
Base finders
3
3
"""
4
- import os
5
4
from collections import namedtuple
5
+ from pathlib import Path
6
+
6
7
from demosys .conf import settings
7
8
from demosys .core .exceptions import ImproperlyConfigured
8
9
@@ -20,60 +21,24 @@ def __init__(self):
20
21
"This is required when using a FileSystemFinder."
21
22
)
22
23
self .paths = getattr (settings , self .settings_attr )
24
+ self ._cached_paths = {}
23
25
24
- self ._cache = {}
25
-
26
- def find (self , path ):
26
+ def find (self , path : Path ):
27
27
"""
28
- Find a file in the path.
29
- When creating a custom finder, this is the method you override .
28
+ Find a file in the path. The file may exist in multiple
29
+ paths. The last found file will be returned .
30
30
31
31
:param path: The path to find
32
32
:return: The absolute path to the file or None if not found
33
33
"""
34
- return self ._find (path )
35
-
36
- def _find (self , path ):
37
- """
38
- Similar to ``find()``, but it caches each result to speed things.
34
+ path_found = None
39
35
40
- :param path: The path to find
41
- :return: The absolute path to the file or None if not found
42
- """
43
36
for entry in self .paths :
44
- abspath = os .path .join (entry , path )
45
- if os .path .exists (abspath ):
46
- self .cache (abspath , abspath )
47
- return abspath
48
- else :
49
- self .cache (abspath , abspath , exists = False )
50
-
51
- return None
37
+ abspath = entry / path
38
+ if abspath .exists ():
39
+ path_found = abspath
52
40
53
- def find_cached (self , path ):
54
- """
55
- Check if the path is already cached.
56
- This method should normally not be overridden.
57
-
58
- :param path: The path to the file
59
- :return: The absolute path to the file or None
60
- """
61
- entry = self ._cache .get (path )
62
- if entry .exists :
63
- return entry .abspath
64
-
65
- return None
66
-
67
- def cache (self , path , abspath , exists = True ):
68
- """
69
- Caches an entry.
70
- Should ideally not be overridden.
71
-
72
- :param path: The path
73
- :param abspath: The absolute path
74
- :param exists: Did the file exist? (bool)
75
- """
76
- self ._cache [path ] = FinderEntry (path = path , abspath = abspath , exists = exists )
41
+ return path_found
77
42
78
43
79
44
class BaseEffectDirectoriesFinder (BaseFileSystemFinder ):
@@ -83,7 +48,7 @@ class BaseEffectDirectoriesFinder(BaseFileSystemFinder):
83
48
def __init__ (self ):
84
49
from demosys .effects .registry import effects
85
50
self .paths = list (effects .get_dirs ())
86
- self ._cache = {}
87
51
88
- def find (self , path ):
89
- return self ._find (os .path .join (self .directory , path ))
52
+ def find (self , path : Path ):
53
+ path = Path (self .directory ) / Path (path )
54
+ return super ().find (path )
0 commit comments