[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/TextIndexNG - Converter.py:1.1.2.1
Andreas Jung
andreas@digicool.com
Tue, 26 Feb 2002 18:46:24 -0500
Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/TextIndexNG
In directory cvs.zope.org:/tmp/cvs-serv20992
Added Files:
Tag: ajung-textindexng-branch
Converter.py
Log Message:
added registry for converters
=== Added File Zope/lib/python/Products/PluginIndexes/TextIndexNG/Converter.py ===
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
__doc__ = """ converter registry """
__version__ = '$Id: Converter.py,v 1.1.2.1 2002/02/26 23:46:23 andreasjung Exp $'
import re, os
import converters
_default_cfg = 'converters.ini'
class ConverterRegistry:
""" registry for converters """
converters = {}
def __init__(self):
self.readConfig(_default_cfg)
def addConverter(self, extension=None, mimetype=None, callback=None):
""" map either an extension or a mimetype to a converter function """
if extension:
self.converters[ extension ] = callback
if mimetype:
self.converters[ mimetype ] = callback
def readConfig(self, fname):
lines = open(fname).readlines()
for line in lines:
exts,mt,func = line.split(';')
exts = exts.split(',')
mt = mt.strip()
func = func.strip()
callback = self.getCallback( func )
for ext in exts: self.addConverter(extension=ext,
callback=callback)
self.addConverter(mimetype=mt, callback=callback)
def getCallback(self, module):
return converters.getConverter(module)
def __call__(self, data, filename=None, mimetype=None):
cb = None
if mimetype:
cb = self.converters.get(mimetype, None)
elif not cb and filename:
base,ext = os.apth.split( filename )
cb = self.converters.get(ext, None)
if not cb: return data
return cb( data )
Registry = ConverterRegistry()