11"""
22Base finders
33"""
4- import os
54from collections import namedtuple
5+ from pathlib import Path
6+
67from demosys .conf import settings
78from demosys .core .exceptions import ImproperlyConfigured
89
@@ -20,60 +21,24 @@ def __init__(self):
2021 "This is required when using a FileSystemFinder."
2122 )
2223 self .paths = getattr (settings , self .settings_attr )
24+ self ._cached_paths = {}
2325
24- self ._cache = {}
25-
26- def find (self , path ):
26+ def find (self , path : Path ):
2727 """
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 .
3030
3131 :param path: The path to find
3232 :return: The absolute path to the file or None if not found
3333 """
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
3935
40- :param path: The path to find
41- :return: The absolute path to the file or None if not found
42- """
4336 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
5240
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
7742
7843
7944class BaseEffectDirectoriesFinder (BaseFileSystemFinder ):
@@ -83,7 +48,7 @@ class BaseEffectDirectoriesFinder(BaseFileSystemFinder):
8348 def __init__ (self ):
8449 from demosys .effects .registry import effects
8550 self .paths = list (effects .get_dirs ())
86- self ._cache = {}
8751
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