Python filters beginning to work, still issues.
--HG-- branch : WINDOWSPORT
This commit is contained in:
+17
-7
@@ -117,23 +117,33 @@ class WordFilter:
|
||||
identification. Do 2 tries at most'''
|
||||
if self.ntry == 0:
|
||||
self.ntry = 1
|
||||
return (["antiword", "-t", "-i", "1", "-m", "UTF-8"],
|
||||
WordProcessData(self.em))
|
||||
cmd = rclexecm.which("antiword")
|
||||
if cmd:
|
||||
return ([cmd, "-t", "-i", "1", "-m", "UTF-8"],
|
||||
WordProcessData(self.em))
|
||||
else:
|
||||
return ([],None)
|
||||
elif self.ntry == 1:
|
||||
self.ntry = 2
|
||||
# antiword failed. Check for an rtf file, or text and
|
||||
# process accordingly. It the doc is actually msword, try
|
||||
# wvWare.
|
||||
mt = self.mimetype(fn)
|
||||
self.em.rclog("rcldoc.py: actual MIME type %s" % mt)
|
||||
if mt == "text/plain":
|
||||
return ([os.path.join(self.execdir,"rcltext")],
|
||||
return ([python, os.path.join(self.execdir, "rcltext")],
|
||||
WordPassData(self.em))
|
||||
elif mt == "text/rtf":
|
||||
return ([os.path.join(self.execdir, "rclrtf")],
|
||||
WordPassData(self.em))
|
||||
cmd = [python, os.path.join(self.execdir, "rclrtf.py")]
|
||||
self.em.rclog("rcldoc.py: returning cmd %s" % cmd)
|
||||
return (cmd, WordPassData(self.em))
|
||||
elif mt == "application/msword":
|
||||
return (["wvWare", "--nographics", "--charset=utf-8"],
|
||||
WordPassData(self.em))
|
||||
cmd = rclexecm.which("wvWare")
|
||||
if cmd:
|
||||
return ([cmd, "--nographics", "--charset=utf-8"],
|
||||
WordPassData(self.em))
|
||||
else:
|
||||
return ([],None)
|
||||
else:
|
||||
return ([],None)
|
||||
else:
|
||||
|
||||
+14
-4
@@ -49,6 +49,9 @@ class RclExecM:
|
||||
else:
|
||||
self.maxmembersize = 50 * 1024
|
||||
self.maxmembersize = self.maxmembersize * 1024
|
||||
if sys.platform == "win32":
|
||||
import msvcrt
|
||||
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
|
||||
|
||||
def rclog(self, s, doexit = 0, exitvalue = 1):
|
||||
print >> sys.stderr, "RCLMFILT:", self.myname, ":", s
|
||||
@@ -216,14 +219,15 @@ class Executor:
|
||||
We expect cmd as a list of command name + arguments'''
|
||||
|
||||
try:
|
||||
proc = subprocess.Popen(cmd + [filename],
|
||||
fullcmd = cmd + [filename]
|
||||
proc = subprocess.Popen(fullcmd,
|
||||
stdout = subprocess.PIPE)
|
||||
stdout = proc.stdout
|
||||
except subprocess.CalledProcessError as err:
|
||||
self.em.rclog("extractone: Popen() error: %s" % err)
|
||||
self.em.rclog("extractone: Popen(%s) error: %s" % (fullcmd, err))
|
||||
return (False, "")
|
||||
except OSError as err:
|
||||
self.em.rclog("extractone: Popen OS error: %s" % err)
|
||||
self.em.rclog("extractone: Popen(%s) OS error: %s" % (fullcmd, err))
|
||||
return (False, "")
|
||||
|
||||
for line in stdout:
|
||||
@@ -231,6 +235,7 @@ class Executor:
|
||||
|
||||
proc.wait()
|
||||
if proc.returncode:
|
||||
self.em.rclog("extractone: [%s] returncode %d" % (returncode))
|
||||
return False, postproc.wrapData()
|
||||
else:
|
||||
return True, postproc.wrapData()
|
||||
@@ -283,12 +288,17 @@ def which(program):
|
||||
for ext in os.environ.get("PATHEXT", "").split(os.pathsep):
|
||||
yield fpath + ext
|
||||
|
||||
def path_candidates():
|
||||
yield os.path.dirname(sys.argv[0])
|
||||
for path in os.environ["PATH"].split(os.pathsep):
|
||||
yield path
|
||||
|
||||
fpath, fname = os.path.split(program)
|
||||
if fpath:
|
||||
if is_exe(program):
|
||||
return program
|
||||
else:
|
||||
for path in os.environ["PATH"].split(os.pathsep):
|
||||
for path in path_candidates():
|
||||
exe_file = os.path.join(path, program)
|
||||
for candidate in ext_candidates(exe_file):
|
||||
if is_exe(candidate):
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import rclexecm
|
||||
import re
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Processing the output from unrtf
|
||||
class RTFProcessData:
|
||||
def __init__(self, em):
|
||||
self.em = em
|
||||
self.out = ""
|
||||
self.gothead = 0
|
||||
self.patendhead = re.compile('''</head>''')
|
||||
self.patcharset = re.compile('''^<meta http-equiv=''')
|
||||
|
||||
# Some versions of unrtf put out a garbled charset line.
|
||||
# Apart from this, we pass the data untouched.
|
||||
def takeLine(self, line):
|
||||
if not self.gothead:
|
||||
if self.patendhead.search(line):
|
||||
self.out += '<meta http-equiv="Content-Type"' + \
|
||||
'content="text/html;charset=UTF-8">' + "\n"
|
||||
self.out += line + "\n"
|
||||
self.gothead = 1
|
||||
elif not self.patcharset.search(line):
|
||||
self.out += line + "\n"
|
||||
else:
|
||||
self.out += line + "\n"
|
||||
|
||||
def wrapData(self):
|
||||
return self.out
|
||||
|
||||
class RTFFilter:
|
||||
def __init__(self, em):
|
||||
self.em = em
|
||||
|
||||
def reset(self):
|
||||
pass
|
||||
|
||||
def getCmd(self, fn):
|
||||
cmd = rclexecm.which("unrtf")
|
||||
if cmd:
|
||||
return ([cmd, "--nopict", "--html"], RTFProcessData(self.em))
|
||||
else:
|
||||
return ([],None)
|
||||
|
||||
if __name__ == '__main__':
|
||||
proto = rclexecm.RclExecM()
|
||||
filter = RTFFilter(proto)
|
||||
extract = rclexecm.Executor(proto, filter)
|
||||
rclexecm.main(proto, extract)
|
||||
Reference in New Issue
Block a user