7322N/ABackport of Python 3 changes from upstream git:
7322N/A
7322N/Ahttps://github.com/liblouis/liblouis/blob/61e98afda6bab9c921d1955b99235a945357889a/python/louis/__init__.py.in
7322N/A
7322N/A--- liblouis-2.1.1/python/louis/__init__.py.in
7322N/A+++ liblouis-2.1.1/python/louis/__init__.py.in
7322N/A@@ -34,6 +34,18 @@
7322N/A from ctypes import *
7322N/A import struct
7322N/A import atexit
7322N/A+import sys
7322N/A+
7322N/A+# Some general utility functions
7322N/A+def _createTablesString(tablesList):
7322N/A+ """Creates a tables string for liblouis calls"""
7322N/A+ return b",".join([x.encode("ASCII") if isinstance(x, str) else bytes(x) for x in tablesList])
7322N/A+
7322N/A+createStr = None
7322N/A+if sys.version_info[0] == 2:
7322N/A+ createStr = lambda x: unicode(x)
7322N/A+else:
7322N/A+ createStr = lambda x: str(x)
7322N/A
7322N/A #{ Module Configuration
7322N/A #: Specifies the number by which the input length should be multiplied
7322N/A@@ -85,14 +97,14 @@
7322N/A the release date and perhaps notable changes.
7322N/A @rtype: str
7322N/A """
7322N/A- return liblouis.lou_version()
7322N/A+ return liblouis.lou_version().decode("ASCII")
7322N/A
7322N/A def translate(tableList, inbuf, typeform=None,cursorPos=0, mode=0):
7322N/A """Translate a string of characters, providing position information.
7322N/A @param tableList: A list of translation tables.
7322N/A @type tableList: list of str
7322N/A @param inbuf: The string to translate.
7322N/A- @type inbuf: unicode
7322N/A+ @type inbuf: str
7322N/A @param typeform: A list of typeform constants indicating the typeform for each position in inbuf,
7322N/A C{None} for no typeform information.
7322N/A @type typeform: list of int
7322N/A@@ -104,12 +116,12 @@
7322N/A a list of input positions for each position in the output,
7322N/A a list of output positions for each position in the input, and
7322N/A the position of the cursor in the output.
7322N/A- @rtype: (unicode, list of int, list of int, int)
7322N/A+ @rtype: (str, list of int, list of int, int)
7322N/A @raise RuntimeError: If a complete translation could not be done.
7322N/A @see: lou_translate in the liblouis documentation
7322N/A """
7322N/A- tablesString = ",".join([str(x) for x in tableList])
7322N/A- inbuf = unicode(inbuf)
7322N/A+ tablesString = _createTablesString(tableList)
7322N/A+ inbuf = createStr(inbuf)
7322N/A inlen = c_int(len(inbuf))
7322N/A outlen = c_int(inlen.value*outlenMultiplier)
7322N/A outbuf = create_unicode_buffer(outlen.value)
7322N/A@@ -132,19 +144,19 @@
7322N/A @param tableList: A list of translation tables.
7322N/A @type tableList: list of str
7322N/A @param inbuf: The string to translate.
7322N/A- @type inbuf: unicode
7322N/A+ @type inbuf: str
7322N/A @param typeform: A list of typeform constants indicating the typeform for each position in inbuf,
7322N/A C{None} for no typeform information.
7322N/A @type typeform: list of int
7322N/A @param mode: The translation mode; add multiple values for a combined mode.
7322N/A @type mode: int
7322N/A @return: The translated string.
7322N/A- @rtype: unicode
7322N/A+ @rtype: str
7322N/A @raise RuntimeError: If a complete translation could not be done.
7322N/A @see: lou_translateString in the liblouis documentation
7322N/A """
7322N/A- tablesString = ",".join([str(x) for x in tableList])
7322N/A- inbuf = unicode(inbuf)
7322N/A+ tablesString = _createTablesString(tableList)
7322N/A+ inbuf = createStr(inbuf)
7322N/A inlen = c_int(len(inbuf))
7322N/A outlen = c_int(inlen.value*outlenMultiplier)
7322N/A outbuf = create_unicode_buffer(outlen.value)
7322N/A@@ -164,7 +176,7 @@
7322N/A @param tableList: A list of translation tables.
7322N/A @type tableList: list of str
7322N/A @param inbuf: Braille to back translate.
7322N/A- @type inbuf: unicode
7322N/A+ @type inbuf: str
7322N/A @param typeform: List where typeform constants will be placed.
7322N/A @type typeform: list
7322N/A @param cursorPos: Position of cursor.
7322N/A@@ -175,12 +187,12 @@
7322N/A a list of input positions for each position in the output,
7322N/A a list of the output positions for each position in the input and
7322N/A the position of the cursor in the output.
7322N/A- @rtype: (unicode, list of int, list of int, int)
7322N/A+ @rtype: (str, list of int, list of int, int)
7322N/A @raises RuntimeError: If back translation could not be completed.
7322N/A @see: lou_backTranslate in the liblouis documentation.
7322N/A """
7322N/A- tablestring = ','.join([str(x) for x in tableList])
7322N/A- inbuf = unicode(inbuf)
7322N/A+ tablestring = _createTablesString(tableList)
7322N/A+ inbuf = createStr(inbuf)
7322N/A inlen = c_int(len(inbuf))
7322N/A outlen = c_int(inlen.value * outlenMultiplier)
7322N/A outbuf = create_unicode_buffer(outlen.value)
7322N/A@@ -203,19 +215,19 @@
7322N/A @param tableList: A list of translation tables.
7322N/A @type tableList: list of str
7322N/A @param inbuf: The Braille to back translate.
7322N/A- @type inbuf: unicode
7322N/A+ @type inbuf: str
7322N/A @param typeform: List for typeform constants to be put in.
7322N/A If you don't want typeform data then give None
7322N/A @type typeform: list
7322N/A @param mode: The translation mode
7322N/A @type mode: int
7322N/A @return: The back translation of inbuf.
7322N/A- @rtype: unicode
7322N/A+ @rtype: str
7322N/A @raises RuntimeError: If a full back translation could not be done.
7322N/A @see: lou_backTranslateString in the liblouis documentation.
7322N/A """
7322N/A- tablestring = ','.join([str(x) for x in tableList])
7322N/A- inbuf = unicode(inbuf)
7322N/A+ tablestring = _createTablesString(tableList)
7322N/A+ inbuf = createStr(inbuf)
7322N/A inlen = c_int(len(inbuf))
7322N/A outlen = c_int(inlen.value * outlenMultiplier)
7322N/A outbuf = create_unicode_buffer(outlen.value)
7322N/A@@ -237,7 +249,7 @@
7322N/A @param inbuf: The text to get hyphenation information about.
7322N/A This should be a single word and leading/trailing whitespace
7322N/A and punctuation is ignored.
7322N/A- @type inbuf: unicode
7322N/A+ @type inbuf: str
7322N/A @param mode: Lets liblouis know if inbuf is plain text or Braille.
7322N/A Set to 0 for text and anyother value for Braille.
7322N/A @type mode: int
7322N/A@@ -247,13 +259,13 @@
7322N/A @raises RuntimeError: If hyphenation data could not be produced.
7322N/A @see: lou_hyphenate in the liblouis documentation.
7322N/A """
7322N/A- tablestring = ','.join([str(x) for x in tableList])
7322N/A- inbuf = unicode(inbuf)
7322N/A+ tablesString = _createTablesString(tableList)
7322N/A+ inbuf = createStr(inbuf)
7322N/A inlen = c_int(len(inbuf))
7322N/A hyphen_string = create_string_buffer(inlen.value)
7322N/A- if not liblouis.lou_hyphenate(tablestring, inbuf, inlen, hyphen_string, mode):
7322N/A- raise RuntimeError("Can't hyphenate tables %s, inbuf %s, mode %d" %(tablestring, inbuf, mode))
7322N/A- return hyphen_string.value
7322N/A+ if not liblouis.lou_hyphenate(tablesString, inbuf, inlen, hyphen_string, mode):
7322N/A+ raise RuntimeError("Can't hyphenate tables %s, inbuf %s, mode %d" %(tablesString, inbuf, mode))
7322N/A+ return hyphen_string.value.decode("ASCII")
7322N/A
7322N/A def compileString(tableList, inString):
7322N/A """Compile a table entry on the fly at run-time.
7322N/A@@ -264,7 +276,8 @@
7322N/A @raise RuntimeError: If compilation of the entry failed.
7322N/A @see: lou_compileString in the liblouis documentation
7322N/A """
7322N/A- tablesString = ",".join([str(x) for x in tableList])
7322N/A+ tablesString = _createTablesString(tableList)
7322N/A+ inBytes = inString.encode("ASCII") if isinstance(inString, str) else bytes(inString)
7322N/A if not liblouis.lou_compileString(tablesString, inString):
7322N/A raise RuntimeError("Can't compile entry: tables %s, inString %s" % (tableList, inString))
7322N/A
7322N/A@@ -282,9 +295,13 @@
7322N/A comp8Dots = 8
7322N/A pass1Only = 16
7322N/A compbrlLeftCursor = 32
7322N/A+otherTrans = 64
7322N/A+ucBrl = 128
7322N/A #}
7322N/A
7322N/A if __name__ == '__main__':
7322N/A # Just some common tests.
7322N/A- print version()
7322N/A- print translate(['../tables/en-us-g2.ctb'], u'Hello world!', cursorPos=5)
7322N/A+ print(version())
7322N/A+ print(translate([b'../tables/en-us-g2.ctb'], 'Hello world!', cursorPos=5))
7322N/A+
7322N/A+