/usr/share/khmerconverter/modules/legacyConvertText.py is in khmerconverter 1.4-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #!/usr/bin/python
# -*- coding: utf8 -*-
# Khmer Unicode to Khmer Legacy fonts Conversion
# (c) 2006 The WordForge Foundation, all rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# See the LICENSE file for more details.
#
# Developed by:
# Hok Kakada (hokkakada@khmeros.info)
# Keo Sophon (keosophon@khmeros.info)
# San Titvirak (titvirak@khmeros.info)
# Seth Chanratha (sethchanratha@khmeros.info)
#
# This module creates a Text file in Khmer unicode format from legacy
# input file.
from FontDataXML import FontData
import legacyReorder
import legacyConverter
import unittest
import tempfile
import os
def convertTxtFile(inputFile, outputFile, outputFont):
"""
This function creates plain text file from the khmer unicode to legacy.
"""
if (inputFile == outputFile):
raise TypeError('Input file and output file must be different!')
fd = FontData()
if (not fd.isConvertable(outputFont)):
raise TypeError('Unknown output font ' + outputFont + ' !')
try:
fileIn = open(inputFile, 'r')
except IOError:
raise IOError('Cannot open file "' + inputFile + '" for reading!')
try:
fileOut = open(outputFile, 'w')
except IOError:
raise IOError('Cannot open file "' + outputFile + '" for writing!')
data = fd.unicodeData(outputFont)
# reading line by line from the input file, until end of file.
for line in fileIn:
result = line.decode('utf-8')
result = legacyReorder.reorder(result)
result = legacyConverter.converter(result, data)
fileOut.write(result)
fileIn.close()
fileOut.close()
class TestConvertTxt(unittest.TestCase):
def testSameFile(self):
# same file raise error
self.assertRaises(TypeError, convertTxtFile, 'file1', 'file1', 'fontname')
def testNotFound(self):
# raise error when file is unreadable
self.assertRaises(TypeError, convertTxtFile, 'file', 'file1', 'fontname')
def testConversion(self):
handle, filename = tempfile.mkstemp()
tmpFile = open(filename, 'w')
tmpFile.write(u'កខគ'.encode('utf-8'))
tmpFile.close()
# create a usable filename for output
tmpFile = tempfile.TemporaryFile()
outputFilename = tmpFile.name
tmpFile.close()
convertTxtFile(filename, outputFilename, 'abc')
tmpFile = open(outputFilename, 'r')
result = tmpFile.readline()
tmpFile.close()
os.remove(filename)
os.remove(outputFilename)
self.assertEqual(result.decode('utf-8'), 'kxK')
if __name__ == '__main__':
unittest.main()
|