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