[Assorted-commits] SF.net SVN: assorted: [688] python-commons/trunk/src/commons/files.py
Brought to you by:
yangzhang
|
From: <yan...@us...> - 2008-04-29 05:18:53
|
Revision: 688
http://assorted.svn.sourceforge.net/assorted/?rev=688&view=rev
Author: yangzhang
Date: 2008-04-28 22:18:57 -0700 (Mon, 28 Apr 2008)
Log Message:
-----------
added versioned_guard, versioned_cache
Modified Paths:
--------------
python-commons/trunk/src/commons/files.py
Modified: python-commons/trunk/src/commons/files.py
===================================================================
--- python-commons/trunk/src/commons/files.py 2008-04-29 00:32:42 UTC (rev 687)
+++ python-commons/trunk/src/commons/files.py 2008-04-29 05:18:57 UTC (rev 688)
@@ -14,7 +14,7 @@
from __future__ import with_statement
import os, re, tempfile
-
+from cPickle import *
from path import path
def soft_makedirs( path ):
@@ -113,3 +113,59 @@
def close( self ):
self.reader.close()
self.writer.close()
+
+def versioned_guard(path, fresh_version):
+ """
+ Maintain a version object. This is useful for working with versioned
+ caches.
+
+ @param path: The path to the file containing the cached version object.
+ @type path: str
+
+ @param fresh_version: The actual latest version that the cached version
+ should be compared against.
+ @type fresh_version: object (any type that can be compared)
+
+ @return: True iff the cached version is obsolete (less than the fresh
+ version or doesn't exist).
+ @rtype: bool
+ """
+ try:
+ with file( path ) as f: cache_version = load(f)
+ except IOError, (errno, errstr):
+ if errno != 2: raise
+ return True
+ else:
+ if fresh_version > cache_version:
+ with file( path, 'w' ) as f: dump(fresh_version, f)
+ return True
+ else:
+ return False
+
+def versioned_cache(version_path, fresh_version, cache_path, cache_func):
+ """
+ If fresh_version is newer than the version in version_path, then invoke
+ cache_func and cache the result in cache_path (using pickle).
+
+ @param version_path: The path to the file version.
+ @type version_path: str
+
+ @param fresh_version: The actual, up-to-date version value.
+ @type fresh_version: object (any type that can be compared)
+
+ @param cache_path: The path to the cached data.
+ @type cache_path: str
+
+ @param cache_func: The function that produces the fresh data to be cached.
+ @type cache_func: function (no arguments)
+ """
+ if versioned_guard( version_path, fresh_version ):
+ # cache obsolete, force-fetch new data
+ print 'here'
+ result = cache_func()
+ with file(cache_path, 'w') as f: dump(result, f)
+ return result
+ else:
+ # cache up-to-date (should be available since dlcs-timestamp exists!)
+ print 'there'
+ with file(cache_path) as f: return load(f)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|