Back to index

plone3  3.1.7
commandtransform.py
Go to the documentation of this file.
00001 import os
00002 import sys
00003 import tempfile
00004 import re
00005 import shutil
00006 from os.path import join, basename
00007 
00008 from Products.PortalTransforms.interfaces import itransform
00009 from Products.PortalTransforms.libtransforms.utils import bin_search, sansext, getShortPathName
00010 
00011 class commandtransform:
00012     """abstract class for external command based transform
00013     """
00014     __implements__ = itransform
00015 
00016     def __init__(self, name=None, binary=None, **kwargs):
00017         if name is not None:
00018             self.__name__ = name
00019         if binary is not None:
00020             self.binary = bin_search(binary)
00021             self.binary = getShortPathName(self.binary)
00022 
00023     def name(self):
00024         return self.__name__
00025 
00026     def initialize_tmpdir(self, data, **kwargs):
00027         """create a temporary directory, copy input in a file there
00028         return the path of the tmp dir and of the input file
00029         """
00030         tmpdir = tempfile.mktemp()
00031         os.mkdir(tmpdir)
00032         filename = kwargs.get("filename", '')
00033         fullname = join(tmpdir, basename(filename))
00034         filedest = open(fullname , "wb").write(data)
00035         return tmpdir, fullname
00036 
00037     def subObjects(self, tmpdir):
00038         imgs = []
00039         for f in os.listdir(tmpdir):
00040             result = re.match("^.+\.(?P<ext>.+)$", f)
00041             if result is not None:
00042                 ext = result.group('ext')
00043                 if ext in ('png', 'jpg', 'gif'):
00044                     imgs.append(f)
00045         path = join(tmpdir, '')
00046         return path, imgs
00047 
00048     def fixImages(self, path, images, objects):
00049         for image in images:
00050             objects[image] = open(join(path, image), 'rb').read()
00051 
00052     def cleanDir(self, tmpdir):
00053         shutil.rmtree(tmpdir)
00054 
00055 class popentransform:
00056     """abstract class for external command based transform
00057 
00058     Command must read from stdin and write to stdout
00059     """
00060     __implements__ = itransform
00061 
00062     binaryName = ""
00063     binaryArgs = ""
00064     useStdin = True
00065 
00066     def __init__(self, name=None, binary=None, binaryArgs=None, useStdin=None,
00067                  **kwargs):
00068         if name is not None:
00069             self.__name__ = name
00070         if binary is not None:
00071             self.binary = bin_search(binary)
00072         else:
00073             self.binary = bin_search(self.binaryName)
00074         if binaryArgs is not None:
00075             self.binaryArgs = binaryArgs
00076         if useStdin is not None:
00077             self.useStdin = useStdin
00078 
00079     def name(self):
00080         return self.__name__
00081 
00082     def getData(self, couterr):
00083         return couterr.read()
00084 
00085     def convert(self, data, cache, **kwargs):
00086         command = "%s %s" % (self.binary, self.binaryArgs)
00087         if not self.useStdin:
00088             tmpfile, tmpname = tempfile.mkstemp(text=False) # create tmp
00089             os.write(tmpfile, data) # write data to tmp using a file descriptor
00090             os.close(tmpfile)       # close it so the other process can read it
00091             command = command % { 'infile' : tmpname } # apply tmp name to command
00092 
00093         cin, couterr = os.popen4(command, 'b')
00094 
00095         if self.useStdin:
00096             cin.write(data)
00097 
00098         status = cin.close()
00099 
00100         out = self.getData(couterr)
00101         couterr.close()
00102 
00103         if not self.useStdin:
00104             # remove tmp file
00105             os.unlink(tmpname)
00106 
00107         cache.setData(out)
00108         return cache