[Assorted-commits] SF.net SVN: assorted: [232] python-commons/trunk/src/commons/files.py
Brought to you by:
yangzhang
|
From: <yan...@us...> - 2008-01-17 23:14:09
|
Revision: 232
http://assorted.svn.sourceforge.net/assorted/?rev=232&view=rev
Author: yangzhang
Date: 2008-01-17 15:13:45 -0800 (Thu, 17 Jan 2008)
Log Message:
-----------
added persistence to disk double buffers
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-01-17 23:11:45 UTC (rev 231)
+++ python-commons/trunk/src/commons/files.py 2008-01-17 23:13:45 UTC (rev 232)
@@ -15,6 +15,8 @@
import os, re, tempfile
+from path import path
+
def soft_makedirs( path ):
"""
Emulate C{mkdir -p} (doesn't complain if it already exists).
@@ -83,9 +85,14 @@
A simple disk double-buffer. One file is for reading, the other is for
writing, and a facility for swapping the two roles is provided.
"""
- def __init__( self, path ):
- self.paths = [ path + '.0', path + '.1' ]
- self.w, self.r = 0, 1
+ def __init__( self, path_base, do_persist = True ):
+ self.paths = [ path_base + '.0', path_base + '.1' ]
+ self.do_persist = do_persist
+ self.switch_status = path( path_base + '.switched' )
+ if not do_persist or not self.switch_status.exists():
+ self.w, self.r = 0, 1 # default
+ else:
+ self.w, self.r = 1, 0
# initialize empty reader file
with file( self.paths[ self.r ], 'w' ): pass
self.reload_files()
@@ -93,8 +100,11 @@
self.writer = file( self.paths[ self.w ], 'w' )
self.reader = file( self.paths[ self.r ] )
def switch( self ):
+ self.close()
+ if self.do_persist:
+ if self.w == 0: self.switch_status.touch()
+ else: self.switch_status.remove()
self.r, self.w = self.w, self.r
- self.close()
self.reload_files()
def write( self, x ):
self.writer.write( x )
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|