Commit d9c83625 authored by Nicolas Delaby's avatar Nicolas Delaby

Add new abstract commandtransform based on subprocess.



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@39351 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 79c7e3ac
# -*- coding: utf-8 -*-
import os
import sys
import tempfile
......@@ -107,3 +108,60 @@ class popentransform:
cache.setData(out)
return cache
from subprocess import Popen, PIPE
import shlex
class subprocesstransform:
"""abstract class for subprocess command based transform
Command must read from stdin and write to stdout
"""
implements(itransform)
binaryName = ""
binaryArgs = ""
useStdin = True
def __init__(self, name=None, binary=None, binaryArgs=None, useStdin=None,
**kwargs):
if name is not None:
self.__name__ = name
if binary is not None:
self.binary = bin_search(binary)
else:
self.binary = bin_search(self.binaryName)
if binaryArgs is not None:
self.binaryArgs = binaryArgs
if useStdin is not None:
self.useStdin = useStdin
def name(self):
return self.__name__
def getData(self, couterr):
return couterr.read()
def convert(self, data, cache, **kwargs):
command = "%s %s" % (self.binary, self.binaryArgs)
if self.useStdin:
tempfile_object = tempfile.NamedTemporaryFile()
tmpname = tempfile_object.name
tempfile_object.write( data)
tempfile_object.seek(0)
command = command % {'infile': tmpname} # apply tmp name to command
argument_list = shlex.split(command)
if self.useStdin:
process = Popen(argument_list, stdin=tempfile_object, stdout=PIPE,
stderr=PIPE, close_fds=True)
data_out, data_err = process.communicate()
tempfile_object.close()
else:
process = Popen(argument_list, stdin=PIPE, stdout=PIPE,
stderr=PIPE, close_fds=True)
data_out, data_err = process.communicate(input=data)
if data_err:
raise OSError, data_err
cache.setData(data_out)
return cache
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