Input handlers: more closing to help with windows temp files

This commit is contained in:
Jean-Francois Dockes 2019-07-21 10:03:03 +02:00
parent a1daa8de55
commit af664e7768
4 changed files with 42 additions and 6 deletions

View File

@ -35,6 +35,7 @@ if not hasrclconfig:
class SevenZipExtractor:
def __init__(self, em):
self.currentindex = 0
self.fp = None
self.em = em
def extractone(self, ipath):
@ -51,6 +52,10 @@ class SevenZipExtractor:
iseof = rclexecm.RclExecM.eofnext
return (ok, docdata, rclexecm.makebytes(ipath), iseof)
def closefile(self):
if self.fp:
self.fp.close()
###### File type handler api, used by rclexecm ---------->
def openfile(self, params):
filename = params["filename:"]
@ -65,8 +70,8 @@ class SevenZipExtractor:
self.skiplist = skipped.split(" ")
try:
fp = open(filename, 'rb')
self.sevenzip = Archive7z(fp)
self.fp = open(filename, 'rb')
self.sevenzip = Archive7z(self.fp)
return True
except Exception as err:
self.em.rclog("openfile: failed: [%s]" % err)
@ -90,6 +95,7 @@ class SevenZipExtractor:
self.currentindex = 0
self.em.setmimetype('text/plain')
if len(self.sevenzip.getnames()) == 0:
self.closefile()
eof = rclexecm.RclExecM.eofnext
else:
eof = rclexecm.RclExecM.noteof
@ -97,6 +103,7 @@ class SevenZipExtractor:
if self.currentindex >= len(self.sevenzip.getnames()):
#self.em.rclog("getnext: EOF hit")
self.closefile()
return (False, "", "", rclexecm.RclExecM.eofnow)
else:
entryname = self.sevenzip.getnames()[self.currentindex]
@ -112,9 +119,13 @@ class SevenZipExtractor:
break
self.currentindex += 1
if entryname is None:
self.closefile()
return (False, "", "", rclexecm.RclExecM.eofnow)
ret= self.extractone(entryname)
ret = self.extractone(entryname)
if ret[3] == rclexecm.RclExecM.eofnext or \
ret[3] == rclexecm.RclExecM.eofnow:
self.closefile()
self.currentindex += 1
return ret

View File

@ -97,6 +97,9 @@ class RarExtractor:
iseof = rclexecm.RclExecM.eofnext
return (ok, docdata, rclexecm.makebytes(ipath), iseof)
def closefile(self):
self.rar = None
###### File type handler api, used by rclexecm ---------->
def openfile(self, params):
self.currentindex = -1
@ -139,6 +142,7 @@ class RarExtractor:
self.currentindex = 0
self.em.setmimetype('text/plain')
if len(self.rar.namelist()) == 0:
self.closefile()
eof = rclexecm.RclExecM.eofnext
else:
eof = rclexecm.RclExecM.noteof
@ -146,10 +150,14 @@ class RarExtractor:
if self.currentindex >= len(self.rar.namelist()):
#self.em.rclog("getnext: EOF hit")
self.closefile()
return (False, "", "", rclexecm.RclExecM.eofnow)
else:
ret= self.extractone(self.rar.namelist()[self.currentindex])
ret = self.extractone(self.rar.namelist()[self.currentindex])
self.currentindex += 1
if ret[3] == rclexecm.RclExecM.eofnext or \
ret[3] == rclexecm.RclExecM.eofnow:
self.closefile()
return ret
# Main program: create protocol handler and extractor and run them

View File

@ -42,6 +42,9 @@ class TarExtractor:
iseof = rclexecm.RclExecM.eofnext
return (ok, docdata, rclexecm.makebytes(ipath), iseof)
def closefile(self):
self.tar = None
def openfile(self, params):
self.currentindex = -1
try:
@ -71,6 +74,7 @@ class TarExtractor:
self.currentindex = 0
self.em.setmimetype('text/plain')
if len(self.namen) == 0:
self.closefile()
eof = rclexecm.RclExecM.eofnext
else:
eof = rclexecm.RclExecM.noteof
@ -78,10 +82,14 @@ class TarExtractor:
if self.currentindex >= len(self.namen):
self.namen=[]
self.closefile()
return (False, "", "", rclexecm.RclExecM.eofnow)
else:
ret= self.extractone(self.namen[self.currentindex])
ret = self.extractone(self.namen[self.currentindex])
self.currentindex += 1
if ret[3] == rclexecm.RclExecM.eofnext or \
ret[3] == rclexecm.RclExecM.eofnow:
self.closefile()
return ret

View File

@ -13,15 +13,20 @@ class WarExtractor:
def extractone(self, tarinfo):
docdata = ""
iseof = rclexecm.RclExecM.noteof
try:
member = self.tar.extractfile(tarinfo)
docdata = member.read()
ok = True
except Exception as err:
self.em.rclog("extractone: failed: [%s]" % err)
iseof = rclexecm.RclExecM.eofnow
ok = False
return (ok, docdata, tarinfo.name, rclexecm.RclExecM.noteof)
return (ok, docdata, tarinfo.name, iseof)
def closefile():
self.tar = None
###### File type handler api, used by rclexecm ---------->
def openfile(self, params):
self.currentindex = -1
@ -50,9 +55,13 @@ class WarExtractor:
tarinfo = self.tar.next()
if tarinfo is None:
#self.em.rclog("getnext: EOF hit")
self.closefile()
return (False, "", "", rclexecm.RclExecM.eofnow)
else:
ret = self.extractone(tarinfo)
if ret[3] == rclexecm.RclExecM.eofnext or \
ret[3] == rclexecm.RclExecM.eofnow:
self.closefile()
return ret
# Main program: create protocol handler and extractor and run them