Commit 7b7e82d0 authored by Nicolas Delaby's avatar Nicolas Delaby

Add New chain for PortalTransform. These Chains use oood to convert Documents

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@21150 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 5ae06143
### Register Transforms
### This is interesting because we don't expect all transforms to be
### available on all platforms. To do this we allow things to fail at
### two levels
### 1) Imports
### If the import fails the module is removed from the list and
### will not be processed/registered
### 2) Registration
### A second phase happens when the loaded modules register method
### is called and this produces an instance that will used to
### implement the transform, if register needs to fail for now it
### should raise an ImportError as well (dumb, I know)
from logging import DEBUG, ERROR
from Products.PortalTransforms.utils import log
from Products.PortalTransforms.libtransforms.utils import MissingBinary
modules = (
'html_to_odt',
'odt_to_doc',
'odt_to_pdf',
)
g = globals()
transforms = []
for m in modules:
try:
ns = __import__(m, g, g, None)
transforms.append(ns.register())
except ImportError, e:
msg = "Problem importing module %s : %s" % (m, e)
log(msg, severity=ERROR)
except MissingBinary, e:
log(str(e), severity=DEBUG)
except Exception, e:
import traceback
traceback.print_exc()
log("Raised error %s for %s" % (e, m), severity=ERROR)
def initialize(engine):
for transform in transforms:
engine.registerTransform(transform)
from Products.PortalTransforms.interfaces import itransform
from oood_commandtransform import OOOdCommandTransform, OOoDocumentDataStream
from zLOG import LOG
class HTMLToOdt:
"""Transforms HTML to odt by using oood"""
__implements__ = itransform
__name__ = 'html_to_odt'
inputs = ('text/html',)
output = 'application/vnd.oasis.opendocument.text'
tranform_engine = OOOdCommandTransform.__module__
def name(self):
return self.__name__
def __getattr__(self, attr):
if attr == 'inputs':
return self.config['inputs']
if attr == 'output':
return self.config['output']
raise AttributeError(attr)
def convert(self, orig, data, cache=None, filename=None, context=None, **kwargs):
doc = OOOdCommandTransform(context, filename, orig, self.inputs[0])
doc.convert()
odt = doc.convertTo('odt')
if cache is not None:
cache.setData(odt)
return cache
else:
stream = OOoDocumentDataStream()
stream.setData(odt)
return stream
def register():
return HTMLToOdt()
from Products.PortalTransforms.interfaces import itransform
from oood_commandtransform import OOOdCommandTransform, OOoDocumentDataStream
from Products.ERP5.Document.File import _unpackData
from zLOG import LOG
class OdtToDoc:
"""Transforms ODT to Doc by using oood"""
__implements__ = itransform
__name__ = 'odt_to_doc'
inputs = ('application/vnd.oasis.opendocument.text',)
output = 'application/msword'
tranform_engine = OOOdCommandTransform.__module__
def name(self):
return self.__name__
def __getattr__(self, attr):
if attr == 'inputs':
return self.config['inputs']
if attr == 'output':
return self.config['output']
raise AttributeError(attr)
def convert(self, orig, data, cache=None, filename=None, context=None, **kwargs):
data = _unpackData(orig)
doc = OOOdCommandTransform(context, filename, data, self.inputs[0])
doc.convert()
msword = doc.convertTo('doc')
if cache is not None:
cache.setData(msword)
return cache
else:
stream = OOoDocumentDataStream()
stream.setData(msword)
return stream
def register():
return OdtToDoc()
from Products.PortalTransforms.interfaces import itransform
from oood_commandtransform import OOOdCommandTransform, OOoDocumentDataStream
from Products.ERP5.Document.File import _unpackData
from zLOG import LOG
class OdtToPdf:
"""Transforms ODT to PDF by using oood"""
__implements__ = itransform
__name__ = 'odt_to_pdf'
inputs = ('application/vnd.oasis.opendocument.text',)
output = 'application/pdf'
tranform_engine = OOOdCommandTransform.__module__
def name(self):
return self.__name__
def __getattr__(self, attr):
if attr == 'inputs':
return self.config['inputs']
if attr == 'output':
return self.config['output']
raise AttributeError(attr)
def convert(self, orig, data, cache=None, filename=None, context=None, **kwargs):
data = _unpackData(orig)
doc = OOOdCommandTransform(context, filename, data, self.inputs[0])
doc.convert()
pdf = doc.convertTo('pdf')
if cache is not None:
cache.setData(msword)
return cache
else:
stream = OOoDocumentDataStream()
stream.setData(pdf)
return stream
def register():
return OdtToPdf()
from Products.PortalTransforms.libtransforms.commandtransform import commandtransform
from Products.PortalTransforms.interfaces import idatastream
from Products.ERP5Type.Document import newTempOOoDocument
from zLOG import LOG
class TransformError(Exception):
pass
class OOoDocumentDataStream:
"""Handle OOoDocument in Portal Transforms"""
__implements__ = idatastream
def setData(self, value):
"""set the main"""
self.value = value
def getData(self):
return self.value
def setSubObjects(self, objects):
pass
def getSubObjects(self):
return {}
def getMetadata(self):
"""return a dict-like object with any optional metadata from
the transform
You can modify the returned dictionnary to add/change metadata
"""
return {}
def isCacheable(self):
"""
True by Default
"""
return getattr(self, '_is_cacheable', True)
def setCachable(self, value):
self._is_cacheable = value
class OOOdCommandTransform(commandtransform):
"""Transformer using oood"""
def __init__(self, context, name, data, mimetype):
commandtransform.__init__(self, name)
if name:
self.__name__ = name
self.data = data
self.context = context
self.mimetype = mimetype
def name(self):
return self.__name__
def convert(self):
tmp_ooo = newTempOOoDocument(self.context, self.name)
tmp_ooo.edit( base_data=self.data,
fname=self.name,
source_reference=self.name,
base_content_type=self.mimetype,)
tmp_ooo.oo_data = self.data
self.ooo = tmp_ooo
def convertTo(self, format):
if self.ooo.isTargetFormatAllowed(format):
mime, data = self.ooo.convert(format)
return data
else:
raise TransformError
\ No newline at end of file
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