Script ConvertIndex_descLatin1ToUTF8_py |
[hide private]
[frames] | no frames]

# Source Code for Script script-ConvertIndex_descLatin1ToUTF8_py

     1  #!/usr/bin/python
     2  #Small utility to find all index.desc files and convert them from latin1 to UTF8
     3  #written by Jerome 23/03/2009
     4  # $Id: script-ConvertIndex_descLatin1ToUTF8_py-pysrc.html,v 1.1 2010-04-28 19:44:10 jerome Exp $
     5  import  os, sys
     6
     7 -def convertToUTF8(unknownStr):
     8      enc = None
     9      unicStr = u""
    10      for line in unknownStr.split("\n"):
    11          if line.find("coding:") == 0:
    12              enc = line.split(":", 1)[1].strip()
    13      if enc == None:
    14          enc = "Latin-1"
    15          unicStr = u"coding: UTF-8\n\n"
    16      for line in unknownStr.split("\n"):
    17          if line.find("coding:") == 0:
    18              unicStr += u"coding: UTF-8\n"
    19          else:
    20              unicStr += line.strip().decode(enc) + u"\n"
    21      return unicStr.encode("UTF-8")
    22
    23  for root, dirs, files in os.walk(os.path.abspath(sys.argv[1])):
    24      if "index.desc" in files >= 0:
    25          filein = os.path.join(os.path.abspath(sys.argv[1]), root, "index.desc")
    26          f = open(filein).read()
    27          g = convertToUTF8(f)
    28          if f != g[:-1]:
    29              open(filein, "w").write(g)
    30