This was indeed very broken and had never been used nor tested.
I made a number of fixes and changes today that got it to work:
- added cloning support to PythonInputStream
- added isClone argument to PythonInputStream.close()
- because InputStream expects subclasses to set its length variable upon
construction, a lengthInternal() method was added to the python protocol
to return the length of the input file or source thus opened
- fixed NUL char bug in PythonInputStream.readInternal() that caused reads
to be short when the data read contained NUL chars
- added missing seekInternal() method to PythonOutputStream
- made PythonOutputStream close() call OutputStream close() first
It seems that I also got it to work with the Lucene index compound file format
(the default) with the exception that PythonDirectory.deleteFile() sometimes
is asked to delete non-existent files. I was not able, today, to solve that
problem though.
The new version of fdir.py is attached. I'd like to integrate it into a unit
test at some point.
Fixes are checked in.
Andi..
import os, sys
DEBUG = False #True
class DebugWrapper( object ):
def __init__(self, obj ):
self.obj = obj
def __getattr__(self, name):
print self.obj.__class__.__name__, self.obj.name, name
sys.stdout.flush()
return getattr(self.obj, name )
class DebugFactory( object ):
def __init__(self, klass):
self.klass = klass
def __call__(self, *args, **kw):
instance = self.klass(*args, **kw)
return DebugWrapper( instance )
class DirLock( object ):
# feeling lucky lock impl.
# basically each lock instance ignore others,
# safe for single context usage only.
def __init__(self, name, path ):
self.name = name
self.lock_file = path
self.locked = False
def isLocked(self):
return self.locked
def obtainTimeout( self, timeout ):
self.locked = True
return True
def obtain( self ):
self.locked = True
return True
def release( self ):
self.locked = False
if DEBUG:
DirLock = DebugFactory( DirLock )
class FileStream( object ):
def __init__(self, name, fh, size=0L ):
self.name = name
self.fh = fh
self.size = size # when used as input file
self.length = 0L # when used as output file
def close(self, isClone=False):
if not isClone:
self.fh.close()
def readInternal( self, length, pos ):
self.fh.seek(pos)
return self.fh.read( length )
def seekInternal( self, pos ):
self.fh.seek( pos )
def flushBuffer( self, buffer ):
self.fh.write( buffer )
self.fh.flush()
self.length += len(buffer)
def lengthInternal( self ):
return self.size
def length( self ):
return self.length
if DEBUG:
FileStream = DebugFactory( FileStream )
class FileDirectory( object ):
def __init__(self, path ):
self.name = path
assert os.path.isdir( path )
self.path = path
def close(self):
pass
def createFile(self, name ):
file_path = os.path.join( self.path, name )
fh = open( file_path, "w" )
return FileStream( name, fh )
def deleteFile( self, name ):
if self.fileExists(name):
os.unlink( os.path.join( self.path, name ) )
def fileExists( self, name ):
return os.path.exists( os.path.join( self.path, name ) )
def fileLength( self, name ):
file_path = os.path.join( self.path, name )
return os.path.getsize( file_path )
def fileModified( self, name ):
file_path = os.path.join( self.path, name )
return os.path.getmtime( file_path )
def list(self):
return os.listdir( self.path )
def makeLock( self, name ):
return DirLock( name, os.path.join( self.path, name ) )
def openFile( self, name ):
file_path = os.path.join( self.path, name )
fh = open( file_path, 'r')
return FileStream( name, fh, os.path.getsize(file_path) )
def renameFile(self, fname, tname):
return os.rename( os.path.join( self.path, fname ),
os.path.join( self.path, tname ) )
def touchFile( self, name):
file_path = os.path.join( self.path, name )
fh = open( file_path, 'rw')
c = fh.read(1)
fh.seek(0)
fh.write(c)
fh.close()
if DEBUG:
FileDirectory = DebugFactory(FileDirectory)
_______________________________________________
pylucene-dev mailing list
[email protected]
http://lists.osafoundation.org/mailman/listinfo/pylucene-dev