view MoinMoin/i18n/tools/recode.py @ 4133:251827af7ddc

Fixed: cannot check for File.Error in same line like checking import of File
author Florian Krupicka <florian.krupicka@googlemail.com>
date Tue, 27 May 2008 18:13:17 +0200
parents 01f05e74aa9c
children
line wrap: on
line source
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - file encoding conversion

    @copyright: 2005 by Thomas Waldmann (MoinMoin:ThomasWaldmann)
    @license: GNU GPL, see COPYING for details.

    Convert data in encoding src_enc to encoding dst_enc, both specified
    on command line. Data is read from standard input and written to
    standard output.

    Usage:

    ./recode.py src_enc dst_enc < src  > dst

    Example:

    # Using non utf-8 editor to edit utf-8 file:

    # Make a working copy using iso-8859-1 encoding
    ./recode.py utf-8 iso-8859-1 < de.po > de-iso1.po

    # Use non-utf8 editor
    $EDITOR de-iso1.po

    # Recode back to utf-8
    ./recode.py iso-8859-1 utf-8 < de-iso1.po > de-utf8.po

    # Review changes and replace original if everything is ok
    diff de.po de-utf8.po | less
    mv de-utf8.po de.po

"""

import sys

def error(msg):
    sys.stderr.write(msg + '\n')

def run():
    try:
        cmd, src_enc, dst_enc = sys.argv

        for line in sys.stdin:
            line = unicode(line, src_enc).encode(dst_enc)
            sys.stdout.write(line)

    except UnicodeError, err:
        error("Can't recode: %s" % str(err))
    except LookupError, err:
        error(str(err))
    except ValueError:
        error("Wrong number of arguments")
        error(__doc__)


if __name__ == "__main__":
    run()