[Erp5-report] r35728 nicolas - /erp5/trunk/products/PortalTransforms/transforms/png_to_text.py
nobody at svn.erp5.org
nobody at svn.erp5.org
Fri May 28 14:57:03 CEST 2010
Author: nicolas
Date: Fri May 28 14:57:00 2010
New Revision: 35728
URL: http://svn.erp5.org?rev=35728&view=rev
Log:
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.
Modified:
erp5/trunk/products/PortalTransforms/transforms/png_to_text.py
Modified: erp5/trunk/products/PortalTransforms/transforms/png_to_text.py
URL: http://svn.erp5.org/erp5/trunk/products/PortalTransforms/transforms/png_to_text.py?rev=35728&r1=35727&r2=35728&view=diff
==============================================================================
--- erp5/trunk/products/PortalTransforms/transforms/png_to_text.py [utf8] (original)
+++ erp5/trunk/products/PortalTransforms/transforms/png_to_text.py [utf8] Fri May 28 14:57:00 2010
@@ -1,7 +1,8 @@
+# -*- coding: utf-8 -*-
from Products.PortalTransforms.interfaces import itransform
from Products.PortalTransforms.libtransforms.commandtransform \
import popentransform
-
+from subprocess import Popen, PIPE
import os
import tempfile
from zope.interface import implements
@@ -13,7 +14,7 @@
inputs = ('image/png',)
output = 'text/plain'
output_encoding = 'utf-8'
-
+
__version__ = '2008-10-07.01'
binaryName = "ocrocmd"
@@ -24,29 +25,28 @@
# XXX Surcharge from commandtransform, as ocrocmd do not accept
# parameters but environnement variable.
# 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:
tmpfile, tmpname = tempfile.mkstemp(text=False) # create tmp
os.write(tmpfile, data) # write data to tmp using a file descriptor
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:
- cin.write(str(data))
-
- status = cin.close()
-
- out = self.getData(couterr)
- couterr.close()
+ else:
+ popen = Popen([command, tmpname], env=environment, stdin=PIPE,
+ stdout=PIPE)
+ out = popen.communicate(str(data))[0]
if not self.useStdin:
# remove tmp file
os.unlink(tmpname)
-
- cache.setData(out)
- return cache
+
+ cache.setData(out)
+ return cache
def register():
return png_to_text()
More information about the Erp5-report
mailing list