Attachment 'recode.py'

Download

   1 #!/usr/bin/env python
   2 # -*- coding: iso-8859-1 -*-
   3 """
   4     MoinMoin - file encoding conversion
   5 
   6     @copyright: 2006 by Thomas Waldmann, Oliver Siemoneit
   7     @license: GNU GPL, see COPYING for details.
   8 
   9     Convert data of src file in encoding src_enc to encoding
  10     dst_enc in dst_file.
  11 
  12     Usage:
  13 
  14     recode.py src_enc dst_enc src_file dst_file
  15 
  16     Example:
  17 
  18     # Using non utf-8 editor to edit utf-8 file:
  19 
  20     # Make a working copy using iso-8859-1 encoding
  21     recode.py utf-8 iso-8859-1 de.po de-iso1.po
  22 
  23     # Use non-utf8 editor
  24     $EDITOR de-iso1.po
  25 
  26     # Recode back to utf-8
  27     recode.py iso-8859-1 utf-8 de-iso1.po de-utf8.po
  28 
  29     # Review changes and replace original if everything is ok
  30     diff de.po de-utf8.po | less
  31     mv de-utf8.po de.po
  32 
  33 """
  34 
  35 import sys
  36 
  37 def error(msg):
  38     sys.stderr.write(msg + '\n')
  39     
  40 def run():
  41     try:
  42         cmd, src_enc, dst_enc, src_file, dst_file = sys.argv
  43 
  44         lines = []
  45         for line in open(src_file).readlines():
  46             lines += unicode(line, src_enc).encode(dst_enc)
  47 
  48         open(dst_file,"w").writelines(lines)
  49 
  50     except UnicodeError, err:
  51         error("Can't recode: %s" % str(err))
  52     except LookupError, err:
  53         error(str(err))
  54     except ValueError:
  55         error("Wrong number of arguments")
  56         error(__doc__)
  57 
  58 
  59 if __name__ == "__main__":
  60     run()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-12-25 14:03:16, 1.3 KB) [[attachment:recode.py]]
  • [get | view] (2006-12-25 14:02:39, 29.1 KB) [[attachment:traceback.jpg]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.