Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cygwin.com/git/cygwin-apps/calm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--calm/utils.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/calm/utils.py b/calm/utils.py
index fb95cc6..9f75813 100644
--- a/calm/utils.py
+++ b/calm/utils.py
@@ -125,3 +125,36 @@ def system(args):
else:
for l in output.decode().splitlines():
logging.info(l)
+
+
+#
+# This provides a simple wrapper around a function which takes a pathname as
+# it's only parameter. The result is cached as long as the mtime of the
+# pathname is unchanged.
+#
+def mtime_cache(user_function):
+ sentinel = object() # unique object used to signal cache misses
+ cache = {}
+
+ def wrapper(key):
+ # make sure path is absolute
+ key = os.path.abspath(key)
+
+ (result, mtime) = cache.get(key, (sentinel, 0))
+
+ new_mtime = os.path.getmtime(key)
+
+ # cache hit
+ if result is not sentinel:
+ # cache valid
+ if new_mtime == mtime:
+ return result
+ else:
+ logging.debug('%s cache invalidated by mtime change' % key)
+
+ # cache miss
+ result = user_function(key)
+ cache[key] = (result, new_mtime)
+ return result
+
+ return wrapper