Back to index

moin  1.9.0~rc2
recode.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: iso-8859-1 -*-
00003 """
00004     MoinMoin - file encoding conversion
00005 
00006     @copyright: 2005 by Thomas Waldmann (MoinMoin:ThomasWaldmann)
00007     @license: GNU GPL, see COPYING for details.
00008 
00009     Convert data in encoding src_enc to encoding dst_enc, both specified
00010     on command line. Data is read from standard input and written to
00011     standard output.
00012 
00013     Usage:
00014 
00015     ./recode.py src_enc dst_enc < src  > dst
00016 
00017     Example:
00018 
00019     # Using non utf-8 editor to edit utf-8 file:
00020 
00021     # Make a working copy using iso-8859-1 encoding
00022     ./recode.py utf-8 iso-8859-1 < de.po > de-iso1.po
00023 
00024     # Use non-utf8 editor
00025     $EDITOR de-iso1.po
00026 
00027     # Recode back to utf-8
00028     ./recode.py iso-8859-1 utf-8 < de-iso1.po > de-utf8.po
00029 
00030     # Review changes and replace original if everything is ok
00031     diff de.po de-utf8.po | less
00032     mv de-utf8.po de.po
00033 
00034 """
00035 
00036 import sys
00037 
00038 def error(msg):
00039     sys.stderr.write(msg + '\n')
00040 
00041 def run():
00042     try:
00043         cmd, src_enc, dst_enc = sys.argv
00044 
00045         for line in sys.stdin:
00046             line = unicode(line, src_enc).encode(dst_enc)
00047             sys.stdout.write(line)
00048 
00049     except UnicodeError, err:
00050         error("Can't recode: %s" % str(err))
00051     except LookupError, err:
00052         error(str(err))
00053     except ValueError:
00054         error("Wrong number of arguments")
00055         error(__doc__)
00056 
00057 
00058 if __name__ == "__main__":
00059     run()
00060