Back to index

moin  1.9.0~rc2
__init__.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Filter Package
00004 
00005     @copyright: 2006-2009 MoinMoin:ThomasWaldmann
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 import sys, os
00010 import time
00011 
00012 from MoinMoin import log
00013 logging = log.getLogger(__name__)
00014 
00015 from MoinMoin.util import pysupport
00016 
00017 modules = pysupport.getPackageModules(__file__)
00018 
00019 standard_codings = ['utf-8', 'iso-8859-15', 'iso-8859-1', ]
00020 
00021 from MoinMoin.util.SubProcess import exec_cmd
00022 
00023 
00024 def quote_filename(filename):
00025     """ quote a filename (could contain blanks or other special chars) in a
00026         way suitable for the platform we run on.
00027     """
00028     # XXX Use os.name AND/OR sys.platform?
00029     if os.name == 'posix':
00030         filename = "'%s'" % filename
00031     elif sys.platform == 'win32':
00032         filename = '"%s"' % filename
00033     else:
00034         raise ValueError("MoinMoin.filter.quote_filename: os/platform not supported")
00035     return filename
00036 
00037 
00038 def execfilter(cmd, filename, codings=standard_codings):
00039     """ use cmd to get plaintext content of filename
00040         to decode to unicode, we use the first coding of codings list that
00041         does not throw an exception or force ascii
00042     """
00043     filter_cmd = cmd % quote_filename(filename)
00044     data, errors, rc = exec_cmd(filter_cmd, timeout=300)
00045     logging.debug("Command '%s', rc: %d, stdout: %d bytes, stderr: %s" % (filter_cmd, rc, len(data), errors))
00046     for c in codings:
00047         try:
00048             return data.decode(c)
00049         except UnicodeError:
00050             pass
00051     return data.decode('ascii', 'replace')
00052