home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / diveintopython / examples / unicode2koi8r.py < prev    next >
Encoding:
Python Source  |  2004-05-05  |  3.1 KB  |  131 lines

  1. """Convert Cyrillic from iso-8859-1 Unicode-encoded to KOI8-R-encoded
  2.  
  3. This script is used during the build process of the Russian translation
  4. of "Dive Into Python" (http://diveintopython.org/).
  5.  
  6. It takes one argument, which can be either an HTML file or a directory.
  7. If a file, it converts the file in place; if a directory, it converts
  8. every HTML file in the immediate directory (but not recursively).
  9.  
  10. Safe but pointless to run more than once on the same file or directory.
  11. """
  12.  
  13. __author__ = "Mark Pilgrim (mark@diveintopython.org)"
  14. __version__ = "$Revision: 1.2 $"
  15. __date__ = "$Date: 2004/05/05 21:57:19 $"
  16. __copyright__ = "Copyright (c) 2001 Mark Pilgrim"
  17. __license__ = "Python"
  18.  
  19. import os
  20. import sys
  21. import re
  22.  
  23. unicodeToKOI8R = { \
  24.     'Ё': '\xb3',
  25.     'А': '\xe1',
  26.     'Б': '\xe2',
  27.     'В': '\xf7',
  28.     'Г': '\xe7',
  29.     'Д': '\xe4',
  30.     'Е': '\xe5',
  31.     'Ж': '\xf6',
  32.     'З': '\xfa',
  33.     'И': '\xe9',
  34.     'Й': '\xea',
  35.     'К': '\xeb',
  36.     'Л': '\xec',
  37.     'М': '\xed',
  38.     'Н': '\xee',
  39.     'О': '\xef',
  40.     'П': '\xf0',
  41.     'Р': '\xf2',
  42.     'С': '\xf3',
  43.     'Т': '\xf4',
  44.     'У': '\xf5',
  45.     'Ф': '\xe6',
  46.     'Х': '\xe8',
  47.     'Ц': '\xe3',
  48.     'Ч': '\xfe',
  49.     'Ш': '\xfb',
  50.     'Щ': '\xfd',
  51.     'Ъ': '\xff',
  52.     'Ы': '\xf9',
  53.     'Ь': '\xf8',
  54.     'Э': '\xfc',
  55.     'Ю': '\xe0',
  56.     'Я': '\xf1',
  57.     'а': '\xc1',
  58.     'б': '\xc2',
  59.     'в': '\xd7',
  60.     'г': '\xc7',
  61.     'д': '\xc4',
  62.     'е': '\xc5',
  63.     'ж': '\xd6',
  64.     'з': '\xda',
  65.     'и': '\xc9',
  66.     'й': '\xca',
  67.     'к': '\xcb',
  68.     'л': '\xcc',
  69.     'м': '\xcd',
  70.     'н': '\xce',
  71.     'о': '\xcf',
  72.     'п': '\xd0',
  73.     'р': '\xd2',
  74.     'с': '\xd3',
  75.     'т': '\xd4',
  76.     'у': '\xd5',
  77.     'ф': '\xc6',
  78.     'х': '\xc8',
  79.     'ц': '\xc3',
  80.     'ч': '\xde',
  81.     'ш': '\xdb',
  82.     'щ': '\xdd',
  83.     'ъ': '\xdf',
  84.     'ы': '\xd9',
  85.     'ь': '\xd8',
  86.     'э': '\xdc',
  87.     'ю': '\xc0',
  88.     'я': '\xd1',
  89.     'ё': '\xa3' }
  90.  
  91. unicodePattern = re.compile(r'&#[0-9]{4,4};')
  92. charsetPattern = re.compile(r'ISO-8859-1', re.IGNORECASE)
  93.  
  94. def translateMatch(match):
  95.     unicode = match.group(0)
  96.     if unicodeToKOI8R.has_key(unicode):
  97.         return unicodeToKOI8R[unicode]
  98.     else:
  99.         return unicode
  100.  
  101. def translateBuffer(buffer):
  102.     buffer = unicodePattern.sub(translateMatch, buffer)
  103.     buffer = charsetPattern.sub('KOI8-R', buffer)
  104.     return buffer
  105.  
  106. def translateFile(filename, outfilename=None):
  107.     if not outfilename:
  108.         outfilename = filename
  109.     fsock = open(filename)
  110.     buffer = fsock.read()
  111.     fsock.close()
  112.     buffer = translateBuffer(buffer)
  113.     fsock = open(outfilename, 'wb')
  114.     fsock.write(buffer)
  115.     fsock.close()
  116.  
  117. def htmlFilter(filename):
  118.     return os.path.splitext(filename)[1] == '.html'
  119.  
  120. def translateDirectory(directoryname, filterFunc=htmlFilter):
  121.     fileList = [os.path.join(directoryname, f) for f in os.listdir(directoryname)]
  122.     fileList = filter(filterFunc, fileList)
  123.     map(translateFile, fileList)
  124.  
  125. if __name__ == "__main__":
  126.     name = sys.argv[1]
  127.     if os.path.isdir(name):
  128.         translateDirectory(name)
  129.     else:
  130.         translateFile(name)
  131.