Back to index

plone3  3.1.7
command.py
Go to the documentation of this file.
00001 """
00002 A custom transform using external command
00003 """
00004 
00005 __revision__ = '$Id: command.py 4439 2005-06-15 16:32:36Z panjunyong $'
00006 
00007 import os.path
00008 from os import popen3
00009 from Products.PortalTransforms.interfaces import itransform
00010 from Products.PortalTransforms.libtransforms.utils import bin_search, sansext
00011 from Products.PortalTransforms.libtransforms.commandtransform import commandtransform
00012 from Products.PortalTransforms.utils import log
00013 
00014 class ExternalCommandTransform(commandtransform):
00015     """ Custom external command
00016 
00017     transform content by launching an external command
00018 
00019     the command should take the content in an input file (designed by '%s' in
00020     the command line parameters) and return output on stdout.
00021     Input and output mime types must be set correctly !
00022     """
00023     __implements__ = (itransform,)
00024 
00025     __name__ = "command_transform"
00026 
00027     def __init__(self, name=None, input_extension=None, **kwargs):
00028         self.config = {
00029             'binary_path'  : '',
00030             'command_line' : '',
00031             'inputs'       : ('text/plain',),
00032             'output'       : 'text/plain',
00033             }
00034         self.config_metadata = {
00035             'binary_path'  : ('string', 'Binary path',
00036                               'Path of the executable on the server.'),
00037             'command_line' : ('string', 'Command line',
00038                               '''Additional command line option.
00039 There should be at least the input file (designed by "%(input)s").
00040 The transformation\'s result must be printed on stdout.
00041 '''),
00042             'inputs'       : ('list', 'Inputs', 'Input(s) MIME type. Change with care.'),
00043             'output'       : ('string', 'Output', 'Output MIME type. Change with care.'),
00044             }
00045         self.config.update(kwargs)
00046         commandtransform.__init__(self, name=name, binary=self.config['binary_path'],  **kwargs)
00047 
00048         # use the full binary path
00049         self.config.update({'binary_path':self.binary})
00050         self.input_extension = input_extension
00051 
00052     def __getattr__(self, attr):
00053         if attr == 'inputs':
00054             return self.config['inputs']
00055         if attr == 'output':
00056             return self.config['output']
00057         raise AttributeError(attr)
00058 
00059 
00060     def convert(self, data, cache, **kwargs):
00061         filename = kwargs.get('filename') or 'unknown'
00062         if self.input_extension is not None:
00063             kwargs['filename'] = 'unknown' + self.input_extension
00064         else:
00065             kwargs['filename'] = 'unknown' + os.path.splitext(filename)[-1]
00066         tmpdir, fullname = self.initialize_tmpdir(data, **kwargs)
00067 
00068         data = self.invokeCommand(fullname)
00069         cache.setData(data)
00070         path, images = self.subObjects(tmpdir)
00071         objects = {}
00072         if images:
00073             self.fixImages(path, images, objects)
00074             cache.setSubObjects(objects)
00075         self.cleanDir(tmpdir)
00076         return cache
00077 
00078 
00079     def invokeCommand(self, input_name):
00080         command = '%(binary_path)s %(command_line)s' % self.config
00081         input, output, error = popen3(command % input_name)
00082         input.close()
00083         # first read stderr, else we may hang on stout
00084         # but, still hang my windows, so commented it :-(
00085         # error_data = error.read()
00086         error_data = 'error while running "%s"' % (command % input_name)
00087         error.close()
00088         data = output.read()
00089         output.close()
00090         if error_data and not data:
00091             data = error_data
00092         else:
00093             log('Error while running "%s":\n %s' % (command % input_name,
00094                                                     error_data))
00095         return data
00096 
00097 def register():
00098     return ExternalCommandTransform()