#!/usr/bin/env python

# Zip file filter for Recoll

import rclexecm
from zipfile import ZipFile, error

class ZipExtractor:
    def __init__(self, em):
        self.currentindex = 0
        self.em = em

    def extractzipentry(self, name):
        return (ret, data)

    def extractone(self, ipath):
        #self.em.rclog("extractone: [%s]" % ipath)
        docdata = ""
        try:
            docdata = self.zip.read(ipath)
            ok = True
        except error, err:
            self.em.rclog("extractone: failed: [%s]" % err)
            ok = False
        iseof = rclexecm.RclExecM.noteof
        if self.currentindex >= len(self.zip.namelist()) -1:
            iseof = rclexecm.RclExecM.eofnext
        return (ok, docdata, ipath, iseof)

    ###### File type handler api, used by rclexecm ---------->
    def openfile(self, params):
        self.currentindex = 0
        try:
            self.zip = ZipFile(params["filename:"])
            return True
        except:
            return False

    def getipath(self, params):
        return self.extractone(params["ipath:"])
        
    def getnext(self, params):
        if self.currentindex >= len(self.zip.namelist()):
            #self.em.rclog("getnext: EOF hit")
            return (False, "", "", rclexecm.RclExecM.eofnow)
        else:
            ret= self.extractone(self.zip.namelist()[self.currentindex])
            self.currentindex += 1
            return ret

e = rclexecm.RclExecM()
e.mainloop(ZipExtractor(e))
