From 9e994c4f62a2f9e8d614197c235283b74991c2ee Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Tue, 16 Nov 2010 15:51:31 +0100 Subject: [PATCH] python filters: create common main program inside rclexecm.py to help with testing --- src/filters/rclaudio | 7 ++++--- src/filters/rclchm | 6 ++++-- src/filters/rclexecm.py | 43 +++++++++++++++++++++++++++++++++++++++++ src/filters/rclics | 31 ++++------------------------- src/filters/rclinfo | 43 ++++------------------------------------- src/filters/rclzip | 6 ++++-- 6 files changed, 63 insertions(+), 73 deletions(-) diff --git a/src/filters/rclaudio b/src/filters/rclaudio index f8794246..13a04077 100755 --- a/src/filters/rclaudio +++ b/src/filters/rclaudio @@ -37,7 +37,6 @@ class AudioTagExtractor: def __init__(self, em): self.em = em self.currentindex = 0 - self.em.setmimetype("text/html") def extractone(self, params): #self.em.rclog("extractone %s %s" % (params["filename:"], params["mimetype:"])) @@ -76,6 +75,7 @@ class AudioTagExtractor: title = self.em.htmlescape(tags["title"][0].encode("utf-8")) except: pass + self.em.setmimetype("text/html") alldata = self.em.htmlescape(tags.pprint().encode("utf-8")) alldata = alldata.replace("\n", "
") docdata = htmltemplate % (album, artist, title, alldata) @@ -98,5 +98,6 @@ class AudioTagExtractor: self.currentindex += 1 return ret -e = rclexecm.RclExecM() -e.mainloop(AudioTagExtractor(e)) +proto = rclexecm.RclExecM() +extract = AudioTagExtractor(proto) +rclexecm.main(proto, extract) diff --git a/src/filters/rclchm b/src/filters/rclchm index 4301293e..84b9bff8 100755 --- a/src/filters/rclchm +++ b/src/filters/rclchm @@ -112,6 +112,7 @@ class rclCHM: res, doc = self.chm.RetrieveObject(ui) #self.em.rclog("extract: RetrieveObject: %d [%s]" % (res, doc)) if res > 0: + self.em.setmimetype("text/html") return (True, doc, path, iseof) return (False, "", path, iseof) @@ -146,5 +147,6 @@ class rclCHM: self.currentindex += 1 return ret -e = rclexecm.RclExecM() -e.mainloop(rclCHM(e)) +proto = rclexecm.RclExecM() +extract = rclCHM(proto) +rclexecm.main(proto, extract) diff --git a/src/filters/rclexecm.py b/src/filters/rclexecm.py index 41ddd9f1..afd936f9 100644 --- a/src/filters/rclexecm.py +++ b/src/filters/rclexecm.py @@ -28,6 +28,9 @@ class RclExecM: # Our worker sometimes knows the mime types of the data it sends def setmimetype(self, mt): self.mimetype = mt + + # Read single parameter from process input: line with param name and size + # followed by data. def readparam(self): s = sys.stdin.readline() if s == '': @@ -95,6 +98,7 @@ class RclExecM: # If we have an ipath, that's what we look for, else ask for next entry ipath = "" + self.mimetype = "" if params.has_key("ipath:") and len(params["ipath:"]): ok, data, ipath, eof = processor.getipath(params) else: @@ -123,3 +127,42 @@ class RclExecM: # Got message, act on it self.processmessage(processor, params) + +# Common main routine for all python execm filters: either run the +# normal protocol engine or a local loop to test without recollindex +def main(proto, extract): + if len(sys.argv) == 1: + proto.mainloop(extract) + else: + # Got a file name parameter: TESTING without an execm parent + # Loop on all entries or get specific ipath + if not extract.openfile({'filename:':sys.argv[1]}): + print "Open error" + sys.exit(1) + ipath = "" + if len(sys.argv) == 3: + ipath = sys.argv[2] + + if ipath != "": + ok, data, ipath, eof = extract.getipath({'ipath:':ipath}) + if ok: + print "== Found entry for ipath %s (mimetype [%s]):" % \ + (ipath, proto.mimetype) + print data + print + else: + print "Got error, eof %d"%eof + sys.exit(0) + + ecnt = 0 + while 1: + ok, data, ipath, eof = extract.getnext("") + if ok: + ecnt = ecnt + 1 + print "== Entry %d ipath %s (mimetype [%s]:" % \ + (ecnt, ipath, proto.mimetype) +# print data + print + else: + print "Not ok, eof %d" % eof + break diff --git a/src/filters/rclics b/src/filters/rclics index f4985758..866b8b33 100755 --- a/src/filters/rclics +++ b/src/filters/rclics @@ -37,7 +37,6 @@ class IcalExtractor: self.file = "" self.contents = [] self.em = em - self.em.setmimetype("text/plain") def extractone(self, index): if index >= len(self.contents): @@ -48,6 +47,7 @@ class IcalExtractor: iseof = rclexecm.RclExecM.noteof if self.currentindex >= len(self.contents) -1: iseof = rclexecm.RclExecM.eofnext + self.em.setmimetype("text/plain") return (True, docdata, str(index), iseof) ###### File type handler api, used by rclexecm ----------> @@ -152,29 +152,6 @@ class ICalSimpleSplitter: return lo -##### Main program: either talk to the parent or execute test loop - -e = rclexecm.RclExecM() -ical = IcalExtractor(e) - -if len(sys.argv) == 1: - e.mainloop(ical) -else: - # Got a file name parameter: testing without an execm parent - # Loop on all entries - if not ical.openfile({'filename:':sys.argv[1]}): - print "Open error" - sys.exit(1) - - ecnt = 0 - while 1: - ok, data, ipath, eof = ical.getnext("") - if ok: - ecnt = ecnt + 1 - print "=========== ENTRY %d =================" % ecnt - print data - print - else: - print "Got error, eof %d"%eof - break - +proto = rclexecm.RclExecM() +extract = IcalExtractor(proto) +rclexecm.main(proto, extract) diff --git a/src/filters/rclinfo b/src/filters/rclinfo index 8d47997d..05d99c21 100755 --- a/src/filters/rclinfo +++ b/src/filters/rclinfo @@ -29,7 +29,6 @@ class InfoExtractor: self.file = "" self.contents = [] self.em = em - self.em.setmimetype("text/html") def extractone(self, index): if index >= len(self.contents): @@ -44,6 +43,7 @@ class InfoExtractor: iseof = rclexecm.RclExecM.noteof if self.currentindex >= len(self.contents) -1: iseof = rclexecm.RclExecM.eofnext + self.em.setmimetype("text/html") return (True, docdata, str(index), iseof) ###### File type handler api, used by rclexecm ----------> @@ -195,41 +195,6 @@ class InfoSimpleSplitter: ##### Main program: either talk to the parent or execute test loop - -e = rclexecm.RclExecM() -info = InfoExtractor(e) - -if len(sys.argv) == 1: - e.mainloop(info) -else: - # Got a file name parameter: TESTING without an execm parent - # Loop on all entries or get specific ipath - if not info.openfile({'filename:':sys.argv[1]}): - print "Open error" - sys.exit(1) - ipath = "" - if len(sys.argv) == 3: - ipath = sys.argv[2] - - if ipath != "": - ok, data, ipath, eof = info.getipath({'ipath:':ipath}) - if ok: - print "=========== ENTRY for IPATH %s =============" % (ipath) - print data - print - else: - print "Got error, eof %d"%eof - sys.exit(0) - - ecnt = 0 - while 1: - ok, data, ipath, eof = info.getnext("") - if ok: - ecnt = ecnt + 1 - print "=========== ENTRY %d IPATH %s =============" % (ecnt,ipath) -# print data - print - else: - print "Got error, eof %d"%eof - break - +proto = rclexecm.RclExecM() +extract = InfoExtractor(proto) +rclexecm.main(proto, extract) diff --git a/src/filters/rclzip b/src/filters/rclzip index ce1c196e..a35e0519 100755 --- a/src/filters/rclzip +++ b/src/filters/rclzip @@ -48,5 +48,7 @@ class ZipExtractor: self.currentindex += 1 return ret -e = rclexecm.RclExecM() -e.mainloop(ZipExtractor(e)) +# Main program: create protocol handler and extractor and run them +proto = rclexecm.RclExecM() +extract = ZipExtractor(proto) +rclexecm.main(proto, extract)