[liblouis-liblouisxml] Re: A question with future extending the hungarian grade1 table related testing tasks

  • From: Hammer Attila <hammera@xxxxxxxxx>
  • To: liblouis-liblouisxml@xxxxxxxxxxxxx
  • Date: Wed, 09 May 2012 07:41:17 +0200

Hy Mesar,

I little modifyed the script, but not importanter places with you changed the content generation function. I moved some top of lines the header generation part after error handling related code when a parameter is not gived, so the script doesn't produce a mistifical list index out of range traceback error message the user if not gived the second translation table parameter. I defined a try and an except block with handles a possible user error if an user give a not existing translation table in the braille translation generation related code.

I looked your hello file example. The generated test harness file containing a sintax error line in the 22th line.
With your hello file containing following content:
"""foo bar 'bas"""
The test harness file have following generated line:
        'txt': u""""""foo bar 'bas"""""",
This line producing the sintax error. If need handling this situation, following line producing right result I think:
        'txt': u'"""foo bar \'bas"""',
Only need put the harness file this content, but not need changing I think the translated output text to user see the right result.

I wrote my yesterday letter wrong information the second parameter related. Not need give the full path with the translation table parameter, so harness_generator.py hello hu1.ctb hu_harness.py command works right too.

I sending the new modified script with attachment.

Attila
#!/usr/bin/env python
#!coding: utf-8
import os, sys, louis

def generate_content(lines):
    content = ''
    limit = len(lines)
    firstLine = True
    for i in range(limit):
        lines[i] = lines[i].replace('\n', '')
        lines[i] = lines[i].decode('utf-8')
        if lines[i] == '': continue
        if firstLine:
            content = '    {\n'
            firstLine = False
        else:
            content += ' {\n'
        txtQuoting = "'"; brlQuoting = "'"
        if "'" in lines[i]  and '"' in lines[i]:
            txtQuoting = '"""'
        elif "'" in lines[i]:
            txtQuoting = '"'

        content += '        \'txt\': u%s%s%s,\n' %(txtQuoting, lines[i], 
txtQuoting)
        try:
            brl = louis.translateString([sys.argv[2]], lines[i], None, 0)
        except RuntimeError:
            print 'The '+sys.argv[2]+' translation table doesn\'t exists, 
aborting the harness generation.'
            sys.exit()

        if "'" in brl and '"' in brl:
            brlQuoting = '"""'
        elif "'" in brl:
            brlQuoting = '"'
        content += '        \'brl\': u%s%s%s,\n' %(brlQuoting, brl, brlQuoting)
        content += '    },'
    content += '\n]'
    return content

#Main program
#print len(sys.argv)
if len(sys.argv)!=4:
    print 'Usage: '+sys.argv[0]+' <textFile> <liblouisTable> <testHarnessFile>'
    sys.exit()
if not os.path.exists(sys.argv[1]):
    print 'Error: '+sys.argv[1]+' file doesn\'t exist.'
    sys.exit()
tableName = os.path.basename(sys.argv[2])
header = [ '# -*- coding: utf-8 -*-', '', '"""', 
           'Liblouis test harness for the %s table.' % tableName,
           'Please see the liblouis documentationfor more information.',
           '"""' '', 
           'import sys, louis', '', 
           'table = \'%s\'' % tableName, '', 'tests = ['
]
f=open(sys.argv[1], 'r')
lines=f.readlines()
f.close()
g=open(sys.argv[3], 'w')
g.write("\n".join(header)+"\n")
content=generate_content(lines)
g.write(content.encode('utf-8'))
g.close()
print 'Test harness generation is complete.\nPlease inspect %s, and do any 
needed corrections' %sys.argv[3]

Other related posts: