Back to index

plone3  3.1.7
build_transforms.py
Go to the documentation of this file.
00001 """try to build some usefull transformations with the command and xml
00002 transforms and the available binaries
00003 """
00004 from Products.PortalTransforms.libtransforms.utils import bin_search, MissingBinary
00005 
00006 COMMAND_CONFIGS = (
00007     ('lynx_dump', '.html',
00008      {'binary_path'  : 'lynx',
00009       'command_line' : '-dump %s',
00010       'inputs'       : ('text/html',),
00011       'output'       : 'text/plain',
00012       }),
00013     ('tidy_html', '.html',
00014      {'binary_path'  : 'tidy',
00015       'command_line' : '%s',
00016       'inputs'       : ('text/html',),
00017       'output'       : 'text/html',
00018       }),
00019     ('rtf_to_html', None,
00020      {'binary_path'  : 'unrtf',
00021       'command_line' : '%s',
00022       'inputs'       : ('application/rtf',),
00023       'output'       : 'text/html',
00024       }),
00025     ('ppt_to_html', None,
00026      {'binary_path'  : 'ppthtml',
00027       'command_line' : '%s',
00028       'inputs'       : ('application/vnd.ms-powerpoint',),
00029       'output'       : 'text/html',
00030       }),
00031     ('excel_to_html', None,
00032      {'binary_path'  : 'xlhtml',
00033       'command_line' : '-nh -a %s',
00034       'inputs'       : ('application/vnd.ms-excel',),
00035       'output'       : 'text/html',
00036       }),
00037     ('ps_to_text', None,
00038      {'binary_path'  : 'ps2ascii',
00039       'command_line' : '%s',
00040       'inputs'       : ('application/postscript',),
00041       'output'       : 'text/plain',
00042       }),
00043     )
00044 
00045 TRANSFORMS = {}
00046 
00047 from command import ExternalCommandTransform
00048 for tr_name, extension, config in COMMAND_CONFIGS:
00049     try:
00050         bin = bin_search(config['binary_path'])
00051     except MissingBinary:
00052         print 'no such binary', config['binary_path']
00053     else:
00054         tr = ExternalCommandTransform(tr_name, extension)
00055         tr.config['binary_path'] = bin
00056         tr.__name__ = tr_name
00057         tr.config = config
00058         TRANSFORMS[tr_name] = tr
00059 
00060 XMLPROCS_CONF = {
00061     'xsltproc' : '--catalogs --xinclude -o %(output)s %(transform)s %(input)s',
00062     '4xslt' : ' -o %(output)s %(input)s %(transform)s'
00063     }
00064 
00065 bin = None
00066 for proc in XMLPROCS_CONF.keys():
00067     try:
00068         bin = bin_search(proc)
00069         break
00070     except MissingBinary:
00071         print 'no such binary', proc
00072 
00073 if bin is not None:
00074     print 'Using %s as xslt processor' % bin
00075     from xml import XsltTransform
00076     for output in ('html', 'plain'):
00077         name = "xml_to_" + output
00078         command_line = XMLPROCS_CONF[proc]
00079         tr = XsltTransform(name=name, inputs=('text/xml',), output='text/'+output,
00080                            binary_path=bin, command_line=command_line)
00081         TRANSFORMS[name] = tr
00082 
00083 def initialize(engine):
00084     for transform in TRANSFORMS.values():
00085         engine.registerTransform(transform)