
    cQ                     6   d Z ddlmZmZmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZ 	 ddlmZmZ eecZZn# e$ r e
e	cZZY nw xY wed         Zedk    Ze                                Zded	z  z   Zerd
n	 e            Zdedz  z  Zd Zd5dZdZeefdZ  eej!                   eej"        _#        eej        _#        e ee           ee           ee           ee           ee           ee          efej$        _%        e ee           ee           ee           ee           ee           ee           ee           ee           ee          efej&        _%        e ee           ee           ee           ee           ee           ee          efej'        _%        e ee           ee           ee           ee           ee           ee           ee           ee           ee          efej(        _%        e ee          e ee          efej)        _%        efej*        _%        eefej+        _%        eefej,        _%        e ee           ee          eefej-        _%        e ee           ee          eefej.        _%         edee          Z/dej0        _#        dej1        _#        efej1        _%        d Z2d Z3d6dZ4d7dZ5d6dZ6d7dZ7d8dZ8d Z9d Z:d Z;d Z<d8dZ=d Z>d Z?dZ@d xZAxZBZCd	xZDxZEZFdxZGxZHZIdZJd!ZKd"ZLd#ZMd$ZNd%ZOd&ZPd'ZQd(ZRd)ZSd ZTd	ZUdZVd"ZWd#ZXd$ZYeYZZd%Z[dZ\d*Z]d+Z^d,Z_d-Z`d.Zad/Zbe\e]e^e_e`eaebfZcedd0k    r- ee e2                        ee e4d1gd2d34                     dS dS )9a  Liblouis Python ctypes bindings
These bindings allow you to use the liblouis braille translator and back-translator library
from within Python.
This documentation is only related to the Python helper.
Please see the liblouis documentation for more information.

Most of these functions take a C{tableList}      argument which specifies
a list of translation tables to use. Please see the liblouis documentation
concerning the C{tableList} parameter to the C{lou_translateString}
function for information about how liblouis searches for these tables.

@author: Michael Curran <mick@kulgan.net>
@author: James Teh <jamie@jantrid.net>
@author: Eitan Isaacson <eitan@ascender.com>
@author: Michael Whapples <mwhapples@aim.com>
@author: Davy Kager <mail@davykager.nl>
@author: Leonard de Ruijter <alderuijter@gmail.com>
@author: Babbage B.V. <info@babbage.com>
@author: Andre-Abush Clause <dev@andreabc.net>
    )getfilesystemencodingplatformversion_info)register)	c_ushort	CFUNCTYPEcdllc_char_pc_charc_intPOINTERbyrefcreate_string_buffer)WINFUNCTYPEwindllzliblouis.so.20win32      mbcsz	utf_%d_le   c                 @    d                     d | D                       S )z*Creates a tables string for liblouis calls   ,c                     g | ]@}t          |t                    r|                    t                    nt	          |          AS  )
isinstancestrencodefileSystemEncodingbytes).0xs     0/usr/lib/python3/dist-packages/louis/__init__.py
<listcomp>z'_createTablesString.<locals>.<listcomp>X   sO     	
 	
 	
 -7q#,>,>LAHH'(((E!HH	
 	
 	
    )join)
tablesLists    r"   _createTablesStringr'   U   s4    99	
 	
	
 	
 	
  r$   Nc                 @    |rt          | z  | nt          | z              S )z,Creates a typeform buffer for liblouis calls)r   )lengthtypeforms     r"   _createTypeformbufr+   _   s'    -5PHv))Hv<M;P;PPr$   surrogatepassc                 H    t          |                               ||          S N)r   r   )r!   encodingerrorss      r"   createEncodedByteStringr1   g   s     q66==6***r$   c                  Z    t                                                               d          S )zObtain version information for liblouis.
    @return: The version of liblouis, plus other information, such as
        the release date and perhaps notable changes.
    @rtype: str
    ASCII)liblouislou_versiondecoder   r$   r"   versionr7      s$     !!((111r$   c                  4    t                                           S )zObtain charSize information for liblouis.
    @return: The size of the widechar with which liblouis was compiled.
    @rtype: int
    )r4   lou_charSizer   r$   r"   charSizer:      s    
   """r$   c                 ~   t          |           }t          |          }t          t          |          t          z            }t          |j        t          z            }t          |j        t          z            }d}	|rt          |j        |          }	t          |j        z              }
t          |j        z              }t          |          }t          
                    ||t          |          |t          |          |	d||
t          |          |          st          d| d|d|d|d|
          t          |t                    rt          |	          |dd<   |j        d|j        t          z                               t"          t$                    |
d|j                 |d|j                 |j        fS )a  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: 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 cursorPos: The position of the cursor in inbuf.
    @type cursorPos: int
    @param mode: The translation mode; add multiple values for a combined mode.
    @type mode: int
    @return: A tuple of: the translated string,
        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: (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
    NCan't translate: tables , inbuf , typeform z, cursorPos , mode r0   )r'   r1   r   lenwideCharBytesvalueoutlenMultiplierr   r+   r4   lou_translater   RuntimeErrorr   listrawr6   conversionEncodingENCODING_ERROR_HANDLER	tableListinbufr*   	cursorPosmodetablesStringinlenoutlenoutbuftypeformbufinPosoutPoss               r"   	translaterW      s   * 'y11L#E**E#e**-..E5;!1122F!&,">??FK A(x@@V\!$$Eek!$$Fi  I!!efi  
 lyy%%%999dd<
 
 	
 (D!! (;''
1V\M11299'= 	: 	
 	
 	nn}} r$   c           
         t          |           }t          |          }t          t          |          t          z            }t          |j        t          z            }t          |j        t          z            }d}|rt          |j        |          }t          
                    ||t          |          |t          |          |d|          st          d| d|d|d|          t          |t                    rt          |          |dd<   |j        d|j        t          z                               t"          t$                    S )a  Translate a string of characters.
    @param tableList: A list of translation tables.
    @type tableList: list of str
    @param inbuf: The string to translate.
    @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: str
    @raise RuntimeError: If a complete translation could not be done.
    @see: lou_translateString in the liblouis documentation
    Nr<   r=   r>   r?   r@   )r'   r1   r   rA   rB   rC   rD   r   r+   r4   lou_translateStringr   rF   r   rG   rH   r6   rI   rJ   	rL   rM   r*   rO   rP   rQ   rR   rS   rT   s	            r"   translateStringr[     sL     'y11L#E**E#e**-..E5;!1122F!&,">??FK A(x@@''ef	 	 
 lyy%%%441
 
 	
 (D!! (;'':4}445<<#9 =   r$   c                    t          |           }t          |          }t          t          |          t          z            }t          |j        t          z            }t          |j        t          z            }d}	t          |t                    rt          |j                  }	t          |j        z              }
t          |j        z              }t          |          }t                              ||t          |          |t          |          |	d||
t          |          |          st          d| ||||fz            t          |t                    rt          |	          |dd<   |j        d|j        t          z                               t"          t$                    |
d|j                 |d|j                 |j        fS )ag  Back translates a string of characters, providing position information.
    @param tableList: A list of translation tables.
    @type tableList: list of str
    @param inbuf: Braille to back translate.
    @type inbuf: str
    @param typeform: List where typeform constants will be placed.
    @type typeform: list
    @param cursorPos: Position of cursor.
    @type cursorPos: int
    @param mode: Translation mode.
    @type mode: int
    @return: A tuple: A string of the back translation,
        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: (str, list of int, list of int, int)
    @raise RuntimeError: If a complete back translation could not be done.
    @see: lou_backTranslate in the liblouis documentation.
    NzMCan't back translate: tables %s, inbuf %s, typeform %s, cursorPos %d, mode %dr@   )r'   r1   r   rA   rB   rC   rD   r   r   rG   r+   r4   lou_backTranslater   rF   rH   r6   rI   rJ   rK   s               r"   backTranslater^   F  s   ( 'y11L#E**E#e**-..E5;!1122F!&,">??FK(D!! 7(66V\!$$Eek!$$Fi  I%%efi  
 [%9d;<
 
 	
 (D!! (;''
1V\M11299'= 	: 	
 	
 	nn}} r$   c           
         t          |           }t          |          }t          t          |          t          z            }t          |j        t          z            }t          |j        t          z            }d}t          |t                    rt          |j                  }t                              ||t          |          |t          |          |d|          st          d| ||fz            t          |t                    rt          |          |dd<   |j        d|j        t          z                               t"          t$                    S )aM  Back translate from Braille.
    @param tableList: A list of translation tables.
    @type tableList: list of str
    @param inbuf: The Braille to back translate.
    @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: str
    @raise RuntimeError: If a complete back translation could not be done.
    @see: lou_backTranslateString in the liblouis documentation.
    Nz2Can't back translate: tables %s, inbuf %s, mode %dr@   )r'   r1   r   rA   rB   rC   rD   r   r   rG   r+   r4   lou_backTranslateStringr   rF   rH   r6   rI   rJ   rZ   s	            r"   backTranslateStringra     sG     'y11L#E**E#e**-..E5;!1122F!&,">??FK(D!! 7(66++ef	 	 
 @%&'
 
 	
 (D!! (;'':4}445<<#9 =   r$   c                 N   t          |           }t          |          }t          t          |          t          z            }t          |j        dz             }t                              |||||          st          d| ||fz            |j        
                    d          S )a  Get information for hyphenation.
    @param tableList: A list of translation tables and hyphenation
        dictionaries.
    @type tableList: list of str
    @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: 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
    @return: A string with '1' at the beginning of every syllable
        and '0' elsewhere.
    @rtype: str
    @raise RuntimeError: If hyphenation data could not be produced.
    @see: lou_hyphenate in the liblouis documentation.
       z-Can't hyphenate: tables %s, inbuf %s, mode %dr3   )r'   r1   r   rA   rB   r   rC   r4   lou_hyphenaterF   r6   )rL   rM   rO   rP   rQ   hyphen_strings         r"   	hyphenaterf     s    $ 'y11L#E**E#e**-..E(q99M!!,umTRR 
;y%QU>VV
 
 	
 %%g...r$   c                 |    t          |           }t                              |          st          d| z            dS )a  Check if the specified tables can be found and compiled.
        This can be used to check if a list of tables contains errors
        before sending it to other liblouis functions
        that accept a list of tables.
    @param tableList: A list of translation tables.
    @type tableList: list of str
    @raise RuntimeError: If compilation failed.
    @see: lou_checkTable in the liblouis documentation
    zCan't compile: tables %sN)r'   r4   lou_checkTablerF   )rL   rP   s     r"   
checkTableri     sH     'y11L""<00 C5	ABBBC Cr$   c                     t          |           }t          |t                    r|                    d          nt	          |          }t
                              ||          st          d| d|          dS )aH  Compile a table entry on the fly at run-time.
    @param tableList: A list of translation tables.
    @type tableList: list of str
    @param inString: The table entry to be added.
    @type inString: str
    @raise RuntimeError: If compilation of the entry failed.
    @see: lou_compileString in the liblouis documentation
    r3   zCan't compile entry: tables z, inString N)r'   r   r   r   r   r4   lou_compileStringrF   )rL   inStringrP   inBytess       r"   compileStringrn     s     'y11L*4Xs*C*CXhoog&&&xG%%lG<< 
l=FYYQ
 
 	

 
r$   c                     t          |           }t          r|                    d          }t                              ||          S )a  Get the typeform bit for the named emphasis class.
    @param tableList: A list of translation tables.
    @type tableList: list of str
    @param emphClass: An emphasis class name.
    @type emphClass: str
    @see: lou_getTypeformForEmphClass in the liblouis documentation
    r3   )r'   _is_py3r   r4   lou_getTypeformForEmphClass)rL   	emphClassrP   s      r"   getTypeformForEmphClassrs     sB     'y11L .$$W--	//iHHHr$   c                    t          |           }t          |          }t          t          |          t          z            }t          |j        t          z            }t                              ||||d          st          d| d|          |j
        d|j        t          z                               t          t                    S )a  "Convert a string of dot patterns to a string of characters according to the specifications in tableList.
    @param tableList: A list of translation tables.
    @type tableList: list of str
    @param inbuf: a string of dot patterns, either in liblouis format or Unicode braille.
    @type inbuf: str
    @raise RuntimeError: If a complete conversion could not be done.
    @see: lou_dotsToChar in the liblouis documentation
    r   z#Can't convert dots to char: tables r=   Nr@   )r'   r1   r   rA   rB   r   rC   r4   lou_dotsToCharrF   rH   r6   rI   rJ   )rL   rM   rP   r)   rS   s        r"   
dotsToCharrv     s     'y11L#E**E3u::.//F!&,">??F""<JJ 
lAJEER
 
 	
 :4}445<<#9 =   r$   c                    t          |           }t          |          }t          t          |          t          z            }t          |j        t          z            }t                              |||||          st          d| ||fz            |j
        d|j        t          z                               t          t                    S )a  "Convert a string of characterss to a string of dot patterns according to the specifications in tableList.
    @param tableList: A list of translation tables.
    @type tableList: list of str
    @param inbuf: a string of characters.
    @type inbuf: str
    @param mode: The translation mode; add multiple values for a combined mode.
    @type mode: int
    @raise RuntimeError: If a complete conversion could not be done.
    @see: lou_charToDots in the liblouis documentation
    z8Can't convert char to dots: tables %s, inbuf %s, mode %dNr@   )r'   r1   r   rA   rB   r   rC   r4   lou_charToDotsrF   rH   r6   rI   rJ   )rL   rM   rO   rP   r)   rS   s         r"   
charToDotsry     s     'y11L#E**E3u::.//F!&,">??F""<MM 
F%&'
 
 	
 :4}445<<#9 =   r$   c                     | At          | t                    s,t          d                    t          j                            t
                              |           S )a|  Register logging callbacks.
    Set to C{None} for default callback.
    @param logCallback: The callback to use.
        The callback must take two arguments:
        @param level: The log level on which a message is logged.
        @type level: int
        @param message: The logged message.
            Note that the callback should provide its own ASCII decoding routine.
        @type message: bytes

        Example callback:

        @louis.LogCallback
        def incomingLouisLog(level, message):
            print("Message %s logged at level %d" % (message.decode("ASCII"), level))

    @type logCallback: L{LogCallback}
    Nz,logCallback should be of type {} or NoneType)r   LogCallback	TypeErrorformat__name__r4   lou_registerLogCallback)logCallbacks    r"   registerLogCallbackr   )  sU    & z+{'K'K:AA+BVWW
 
 	
 ++K888r$   c                 l    | t           vrt          d| z            t                              |           S )zSet the level for logging callback to be called at.
    @param level: one of the C{LOG_*} constants.
    @type level: int
    @raise ValueError: If an invalid log level is provided.
    z Level %d is an invalid log level)	logLevels
ValueErrorr4   lou_setLogLevel)levels    r"   setLogLevelr   C  s7     I;eCDDD##E***r$   rc          @         i   i   i   i   i'  i N  i0u  i@  iP  i`  __main__s   ../tables/en-us-g2.ctbzHello world!   )rN   r.   )Nr   r   )Nr   )r   )f__doc__sysr   r   r   atexitr   ctypesr   r   r	   r
   r   r   r   r   r   r   r   _loader	_functypeImportErrorr4   _is_windowsr9   rB   rD   r   rI   r'   r+   rJ   r1   lou_freer5   restyperY   argtypesrE   r`   r]   rd   rh   rk   rq   ru   rx   r{   r   r   r7   r:   rW   r[   r^   ra   rf   ri   rn   rs   rv   ry   r   r   
plain_textemph_1comp_emph_1italicemph_2comp_emph_2	underlineemph_3comp_emph_3boldemph_4emph_5emph_6emph_7emph_8emph_9emph_10computer_brailleno_translateno_contractnoContractionscompbrlAtCursordotsIOcompbrlLeftCursorucBrlnoUndefinednoUndefinedDotspartialTransLOG_ALL	LOG_DEBUGLOG_INFOLOG_WARN	LOG_ERROR	LOG_FATALLOG_OFFr   r~   printr   r$   r"   <module>r      s  * * > = = = = = = = = =      
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
)********GYY ) ) )yGYYY)#$'! %%'' }q((   +GVV0E0E0G0G  !MA$56   Q Q Q Q
 )  #+A+ + + + 		   '   %   GFOOGENNGFOOGENNGHGFOO		)  % GFOOGENNGFOOGENNGHGFOOGENNGENNGENN	#   GFOOGENNGFOOGENNGHGFOO		-   ) GFOOGENNGFOOGENNGHGFOOGENNGENNGENN	'  # GFOO	GFOO	#   %-;   '/&:  #1980D $ - GFOOGFOO		$    GFOOGFOO		$    ieX..+/   (#'   %*H  !2 2 2# # #: : : :z* * * *Z9 9 9 9x* * * *Z/ / / /:C C C
 
 
"I I I  ,   29 9 94+ + + 
 & & &v#) ) )y$ $ $t						
  	
 
 			
 i8Y	7S	z	E''))	E))./1
M
M
MNNNNN s   8 
AA