1 #----------------------------------------------------------------------------
2 # Name: cachedloader.py
9 # Copyright: (c) 2004-2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
20 # TODO: Instantiate the database and create a pool
23 class CachedLoader(object):
26 self
.baseLoadDir
= None
28 def fullPath(self
, fileName
):
29 if os
.path
.isabs(fileName
):
31 elif self
.baseLoadDir
:
32 absPath
= os
.path
.join(self
.baseLoadDir
, fileName
)
34 absPath
= os
.path
.abspath(fileName
)
37 def setPrototype(self
, fileName
, loadedFile
):
38 absPath
= self
.fullPath(fileName
)
39 mtime
= time
.time() + 31536000.0 # Make sure prototypes aren't replaced by files on disk
40 self
.cache
[absPath
] = (mtime
, loadedFile
)
42 def update(self
, loader
):
43 self
.cache
.update(loader
.cache
)
48 def delete(self
, fileName
):
49 absPath
= self
.fullPath(fileName
)
50 del self
.cache
[absPath
]
52 def needsLoad(self
, fileName
):
53 absPath
= self
.fullPath(fileName
)
55 cached
= self
.cache
[absPath
]
56 cachedTime
= cached
[0]
57 if cachedTime
>= os
.path
.getmtime(absPath
):
63 def load(self
, fileName
, loader
):
64 absPath
= self
.fullPath(fileName
)
67 cached
= self
.cache
[absPath
]
72 cachedTime
= cached
[0]
73 # ToDO We might need smarter logic for checking if a file needs to be reloaded
74 # ToDo We need a way to disable checking if this is a production server
75 if cachedTime
>= os
.path
.getmtime(absPath
):
76 loadedFile
= cached
[1]
79 targetFile
= file(absPath
)
81 mtime
= os
.path
.getmtime(absPath
)
82 loadedFile
= loader(targetFile
)
83 self
.cache
[absPath
] = (mtime
, loadedFile
)