first pass at converting the filters for python 2/3 compat

This commit is contained in:
Jean-Francois Dockes
2015-11-06 16:49:03 +01:00
parent cc68331f3d
commit f344e8fedd
21 changed files with 298 additions and 255 deletions
+37 -33
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env python
from __future__ import print_function
# Read an ICS file, break it into "documents" which are events, todos,
# or journal entries, and interface with recoll execm
@@ -13,36 +14,36 @@ import rclexecm
import sys
# Decide how we'll process the file.
modules = ('internal', 'icalendar', 'vobject')
usemodule = 'internal'
modules = ("internal", "icalendar", "vobject")
usemodule = "internal"
forcevobject = 0
if usemodule != 'internal':
if usemodule != "internal":
try:
if forcevobject:
raise Exception
from icalendar import Calendar, Event
usemodule = 'icalendar'
usemodule = "icalendar"
except:
try:
import vobject
usemodule = 'vobject'
usemodule = "vobject"
except:
print "RECFILTERROR HELPERNOTFOUND python:icalendar"
print "RECFILTERROR HELPERNOTFOUND python:vobject"
print("RECFILTERROR HELPERNOTFOUND python:icalendar")
print("RECFILTERROR HELPERNOTFOUND python:vobject")
sys.exit(1);
class IcalExtractor:
def __init__(self, em):
self.file = ""
self.contents = []
self.contents = []
self.em = em
def extractone(self, index):
if index >= len(self.contents):
return(False, "", "", True)
docdata = self.contents[index]
#self.em.rclog(docdata)
#self.em.rclog(docdata)
iseof = rclexecm.RclExecM.noteof
if self.currentindex >= len(self.contents) -1:
@@ -55,32 +56,32 @@ class IcalExtractor:
self.file = params["filename:"]
try:
calstr = open(self.file, 'rb')
except Exception, e:
calstr = open(self.file, "rb")
except Exception as e:
self.em.rclog("Openfile: open: %s" % str(e))
return False
self.currentindex = -1
if usemodule == 'internal':
if usemodule == "internal":
self.contents = ICalSimpleSplitter().splitcalendar(calstr)
elif usemodule == 'icalendar':
elif usemodule == "icalendar":
try:
cal = Calendar.from_string(calstr.read())
except Exception, e:
except Exception as e:
self.em.rclog("Openfile: read or parse error: %s" % str(e))
return False
self.contents = cal.walk()
self.contents = [item.as_string() for item in self.contents
if (item.name == 'VEVENT' or item.name == 'VTODO'
or item.name == 'VJOURNAL')]
if (item.name == "VEVENT" or item.name == "VTODO"
or item.name == "VJOURNAL")]
else:
try:
cal = vobject.readOne(calstr)
except Exception, e:
except Exception as e:
self.em.rclog("Openfile: cant parse object: %s" % str(e))
return False
for lstnm in ('vevent_list', 'vtodo_list', 'vjournal_list'):
for lstnm in ("vevent_list", "vtodo_list", "vjournal_list"):
lst = getattr(cal, lstnm, [])
for ev in lst:
self.contents.append(ev.serialize())
@@ -90,7 +91,10 @@ class IcalExtractor:
def getipath(self, params):
try:
index = int(params["ipath:"])
if params["ipath:"] == b'':
index = 0
else:
index = int(params["ipath:"])
except:
return (False, "", "", True)
return self.extractone(index)
@@ -100,7 +104,7 @@ class IcalExtractor:
if self.currentindex == -1:
# Return "self" doc
self.currentindex = 0
self.em.setmimetype('text/plain')
self.em.setmimetype(b'text/plain')
if len(self.contents) == 0:
eof = rclexecm.RclExecM.eofnext
else:
@@ -121,44 +125,44 @@ class ICalSimpleSplitter:
# Note that if an 'interesting' element is nested inside another one,
# it will not be extracted (stay as text in external event). This is
# not an issue and I don't think it can happen with the current list
interesting = ('VTODO', 'VEVENT', 'VJOURNAL')
interesting = (b'VTODO', b'VEVENT', b'VJOURNAL')
def splitcalendar(self, fin):
curblkname = ''
curblk = ''
curblkname = b''
curblk = b''
lo = []
for line in fin:
line = line.rstrip()
if line == '':
if line == b'':
continue
if curblkname:
curblk = curblk + line + "\n"
curblk = curblk + line + b'\n'
l = line.split(":")
l = line.split(b':')
if len(l) < 2:
continue
# If not currently inside a block and we see an
# 'interesting' BEGIN, start block
if curblkname == '' and l[0].upper() == "BEGIN" :
if curblkname == b'' and l[0].upper() == b'BEGIN':
name = l[1].upper()
if name in ICalSimpleSplitter.interesting:
curblkname = name
curblk = curblk + line + "\n"
curblk = curblk + line + b'\n'
# If currently accumulating block lines, check for end
if curblkname and l[0].upper() == "END" and \
if curblkname and l[0].upper() == b'END' and \
l[1].upper() == curblkname:
lo.append(curblk)
curblkname = ''
curblk = ''
curblkname = b''
curblk = b''
if curblk:
lo.append(curblk)
curblkname = ''
curblk = ''
curblkname = b''
curblk = b''
return lo