~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/filter/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
"""
3
3
    MoinMoin - Filter Package
4
4
 
5
 
    @copyright: 2006-2009 MoinMoin:ThomasWaldmann
 
5
    @copyright: 2006-2008 MoinMoin:ThomasWaldmann
6
6
    @license: GNU GPL, see COPYING for details.
7
7
"""
8
8
 
9
 
import sys, os
10
 
import time
 
9
import os
11
10
 
12
11
from MoinMoin import log
13
12
logging = log.getLogger(__name__)
18
17
 
19
18
standard_codings = ['utf-8', 'iso-8859-15', 'iso-8859-1', ]
20
19
 
21
 
from MoinMoin.util.SubProcess import exec_cmd
22
 
 
23
 
 
24
 
def quote_filename(filename):
25
 
    """ quote a filename (could contain blanks or other special chars) in a
26
 
        way suitable for the platform we run on.
27
 
    """
28
 
    # XXX Use os.name AND/OR sys.platform?
29
 
    if os.name == 'posix':
30
 
        filename = "'%s'" % filename
31
 
    elif sys.platform == 'win32':
32
 
        filename = '"%s"' % filename
33
 
    else:
34
 
        raise ValueError("MoinMoin.filter.quote_filename: os/platform not supported")
35
 
    return filename
36
 
 
37
 
 
38
20
def execfilter(cmd, filename, codings=standard_codings):
39
21
    """ use cmd to get plaintext content of filename
40
22
        to decode to unicode, we use the first coding of codings list that
41
23
        does not throw an exception or force ascii
42
24
    """
43
 
    filter_cmd = cmd % quote_filename(filename)
44
 
    data, errors, rc = exec_cmd(filter_cmd, timeout=300)
45
 
    logging.debug("Command '%s', rc: %d, stdout: %d bytes, stderr: %s" % (filter_cmd, rc, len(data), errors))
 
25
    filter_cmd = cmd % filename
 
26
    child_stdin, child_stdout, child_stderr = os.popen3(filter_cmd)
 
27
    data = child_stdout.read()
 
28
    errors = child_stderr.read()
 
29
    child_stdout.close()
 
30
    child_stderr.close()
 
31
    logging.debug("cmd: %s stderr: %s" % (filter_cmd, errors))
46
32
    for c in codings:
47
33
        try:
48
34
            return data.decode(c)