Commit 6009673e authored by Nicolas Delaby's avatar Nicolas Delaby

Replace os.popen4 by subprocess to not mix error messages

and stdout.
This prevent to return "No character boxes found! ..."
returns by ocrupus from stderr instead of '' from stdout.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@35728 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent b4b77167
# -*- coding: utf-8 -*-
from Products.PortalTransforms.interfaces import itransform from Products.PortalTransforms.interfaces import itransform
from Products.PortalTransforms.libtransforms.commandtransform \ from Products.PortalTransforms.libtransforms.commandtransform \
import popentransform import popentransform
from subprocess import Popen, PIPE
import os import os
import tempfile import tempfile
from zope.interface import implements from zope.interface import implements
...@@ -13,7 +14,7 @@ class png_to_text(popentransform): ...@@ -13,7 +14,7 @@ class png_to_text(popentransform):
inputs = ('image/png',) inputs = ('image/png',)
output = 'text/plain' output = 'text/plain'
output_encoding = 'utf-8' output_encoding = 'utf-8'
__version__ = '2008-10-07.01' __version__ = '2008-10-07.01'
binaryName = "ocrocmd" binaryName = "ocrocmd"
...@@ -24,29 +25,28 @@ class png_to_text(popentransform): ...@@ -24,29 +25,28 @@ class png_to_text(popentransform):
# XXX Surcharge from commandtransform, as ocrocmd do not accept # XXX Surcharge from commandtransform, as ocrocmd do not accept
# parameters but environnement variable. # parameters but environnement variable.
# Surcharging prevent to put the variable in the zope.conf file # Surcharging prevent to put the variable in the zope.conf file
command = "%s %s" % (self.binary, self.binaryArgs) command = self.binary
environment = {'quiet': '1',
'hocr': '0',
'blockwise': '0'}
if not self.useStdin: if not self.useStdin:
tmpfile, tmpname = tempfile.mkstemp(text=False) # create tmp tmpfile, tmpname = tempfile.mkstemp(text=False) # create tmp
os.write(tmpfile, data) # write data to tmp using a file descriptor os.write(tmpfile, data) # write data to tmp using a file descriptor
os.close(tmpfile) # close it so the other process can read it os.close(tmpfile) # close it so the other process can read it
command = command % { 'infile' : tmpname } # apply tmp name to command popen = Popen([command, tmpname], env=environment, stdout=PIPE)
out = popen.communicate()[0]
cin, couterr = os.popen4('quiet=1 hocr=0 blockwise=0 %s' % command, 'b')
if self.useStdin: else:
cin.write(str(data)) popen = Popen([command, tmpname], env=environment, stdin=PIPE,
stdout=PIPE)
status = cin.close() out = popen.communicate(str(data))[0]
out = self.getData(couterr)
couterr.close()
if not self.useStdin: if not self.useStdin:
# remove tmp file # remove tmp file
os.unlink(tmpname) os.unlink(tmpname)
cache.setData(out) cache.setData(out)
return cache return cache
def register(): def register():
return png_to_text() return png_to_text()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment