Back to index

plone3  3.1.7
rtf_to_xml.py
Go to the documentation of this file.
00001 """
00002 Uses the http://sf.net/projects/rtf2xml bin to do its handy work
00003 
00004 """
00005 from Products.PortalTransforms.interfaces import itransform
00006 from Products.PortalTransforms.libtransforms.utils import bin_search, sansext
00007 from Products.PortalTransforms.libtransforms.commandtransform import commandtransform
00008 import os
00009 
00010 class rtf_to_xml(commandtransform):
00011     __implements__ = itransform
00012 
00013     __name__ = "rtf_to_xml"
00014     inputs   = ('application/rtf',)
00015     output  = 'text/xml'
00016 
00017     binaryName = "rtf2xml"
00018 
00019     def __init__(self):
00020         commandtransform.__init__(self, binary=self.binaryName)
00021 
00022     def convert(self, data, cache, **kwargs):
00023         kwargs['filename'] = 'unknown.rtf'
00024 
00025         tmpdir, fullname = self.initialize_tmpdir(data, **kwargs)
00026         xml = self.invokeCommand(tmpdir, fullname)
00027         path, images = self.subObjects(tmpdir)
00028         objects = {}
00029         if images:
00030             self.fixImages(path, images, objects)
00031         self.cleanDir(tmpdir)
00032         cache.setData(xml)
00033         cache.setSubObjects(objects)
00034         return cache
00035 
00036     def invokeCommand(self, tmpdir, fullname):
00037         # FIXME: windows users...
00038         xmlfile = "%s/%s.xml" % (tmpdir, sansext(fullname))
00039         cmd = 'cd "%s" && %s -o %s "%s" 2>error_log 1>/dev/null' % (
00040             tmpdir, self.binary, xmlfile, fullname)
00041         os.system(cmd)
00042         try:
00043             xml = open(xmlfile).read()
00044         except:
00045             try:
00046                 return open("%s/error_log" % tmpdir, 'r').read()
00047             except:
00048                 return ''
00049         return xml
00050 
00051 def register():
00052     return rtf_to_xml()