
    fQ                        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ed   Zedk(  Zej1                         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jB                         eejD                  _#        eej0                  _#        e ee       ee       ee       ee       ee       ee      efejH                  _%        e ee       ee       ee       ee       ee       ee       ee       ee       ee      efejL                  _%        e ee       ee       ee       ee       ee       ee      efejN                  _%        e ee       ee       ee       ee       ee       ee       ee       ee       ee      efejP                  _%        e ee      e ee      efejR                  _%        efejT                  _%        eefejV                  _%        eefejX                  _%        e ee       ee      eefejZ                  _%        e ee       ee      eefej\                  _%         edee      Z/dej`                  _#        dejb                  _#        efejb                  _%        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             yy# e$ r	 e
e	cZZY $w xY w)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j                  | D cg c]2  }t        |t              r|j                  t              n
t        |      4 c}      S c c}w )z*Creates a tables string for liblouis calls   ,)join
isinstancestrencodefileSystemEncodingbytes)
tablesListxs     0/usr/lib/python3/dist-packages/louis/__init__.py_createTablesStringr"   U   sL    99  	
 -7q#,>AHH'(E!HL	
 	
s   7ANc                 6    |rt        | z  | S t        | z         S )z,Creates a typeform buffer for liblouis calls)r   )lengthtypeforms     r!   _createTypeformbufr&   _   s!    -5Hv)PHv<M;PP    surrogatepassc                 8    t        |       j                  ||      S N)r   r   )r    encodingerrorss      r!   createEncodedByteStringr-   g   s     q6==6**r'   c                  H    t         j                         j                  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decode r'   r!   versionr4      s     !((11r'   c                  *    t         j                         S )zObtain charSize information for liblouis.
    @return: The size of the widechar with which liblouis was compiled.
    @rtype: int
    )r0   lou_charSizer3   r'   r!   charSizer7      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        j                  ||t        |      |t        |      |	d||
t        |      |      st        d| d|d|d|d|
      t        |t              rt        |	      |dd |j                  d|j
                  t        z   j!                  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 r,   )r"   r-   r   lenwideCharBytesvalueoutlenMultiplierr   r&   r0   lou_translater   RuntimeErrorr   listrawr2   conversionEncodingENCODING_ERROR_HANDLER	tableListinbufr%   	cursorPosmodetablesStringinlenoutlenoutbuftypeformbufinPosoutPoss               r!   	translaterT      sh   * 'y1L#E*E#e*-.E5;;!112F!&,,">?FK(x@V\\!$Eekk!$Fi I!!efi %9d<
 	
 (D!;'

1V\\M1299'= 	: 	
 	n} r'   c           
      @   t        |       }t        |      }t        t        |      t        z        }t        |j
                  t        z        }t        |j
                  t        z        }d}|rt        |j
                  |      }t        j                  ||t        |      |t        |      |d|      st        d| d|d|d|      t        |t              rt        |      |dd |j                  d|j
                  t        z   j!                  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
    Nr9   r:   r;   r<   r=   )r"   r-   r   r>   r?   r@   rA   r   r&   r0   lou_translateStringr   rC   r   rD   rE   r2   rF   rG   	rI   rJ   r%   rL   rM   rN   rO   rP   rQ   s	            r!   translateStringrX     s     'y1L#E*E#e*-.E5;;!112F!&,,">?FK(x@''ef	 %41
 	
 (D!;'::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        j                  ||t        |      |t        |      |	d||
t        |      |      st        d| ||||fz        t        |t              rt        |	      |dd |j                  d|j
                  t        z   j!                  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"   r-   r   r>   r?   r@   rA   r   r   rD   r&   r0   lou_backTranslater   rC   rE   r2   rF   rG   rH   s               r!   backTranslater[   F  ss   ( 'y1L#E*E#e*-.E5;;!112F!&,,">?FK(D!(6V\\!$Eekk!$Fi I%%efi [%9d;<
 	
 (D!;'

1V\\M1299'= 	: 	
 	n} r'   c           
      N   t        |       }t        |      }t        t        |      t        z        }t        |j
                  t        z        }t        |j
                  t        z        }d}t        |t              rt        |j
                        }t        j                  ||t        |      |t        |      |d|      st        d| ||fz        t        |t              rt        |      |dd |j                  d|j
                  t        z   j!                  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"   r-   r   r>   r?   r@   rA   r   r   rD   r&   r0   lou_backTranslateStringr   rC   rE   r2   rF   rG   rW   s	            r!   backTranslateStringr^     s     'y1L#E*E#e*-.E5;;!112F!&,,">?FK(D!(6++ef	 @%&'
 	
 (D!;'::4}45<<#9 =  r'   c                    t        |       }t        |      }t        t        |      t        z        }t        |j                  dz         }t        j                  |||||      st        d| ||fz        |j                  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 %dr/   )r"   r-   r   r>   r?   r   r@   r0   lou_hyphenaterC   r2   )rI   rJ   rL   rM   rN   hyphen_strings         r!   	hyphenaterc     s    $ 'y1L#E*E#e*-.E(q9M!!,umTR;y%QU>VV
 	
 %%g..r'   c                 `    t        |       }t        j                  |      st        d| z        y)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"   r0   lou_checkTablerC   )rI   rM   s     r!   
checkTablerf     s2     'y1L""<05	ABB 1r'   c                     t        |       }t        |t              r|j                  d      n
t	        |      }t
        j                  ||      st        d| d|      y)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
    r/   zCan't compile entry: tables z, inString N)r"   r   r   r   r   r0   lou_compileStringrC   )rI   inStringrM   inBytess       r!   compileStringrk     sU     'y1L*4Xs*Choog&xG%%lG<=FQ
 	
 =r'   c                 f    t        |       }|j                  d      }t        j                  ||      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
    r/   )r"   r   r0   lou_getTypeformForEmphClass)rI   	emphClassrM   s      r!   getTypeformForEmphClassro     s1     'y1L  )I//iHHr'   c                 b   t        |       }t        |      }t        t        |      t        z        }t        |j                  t        z        }t        j                  ||||d      st        d| d|      |j                  d|j                  t        z   j                  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"   r-   r   r>   r?   r   r@   r0   lou_dotsToCharrC   rE   r2   rF   rG   )rI   rJ   rM   r$   rP   s        r!   
dotsToCharrr     s     'y1L#E*E3u:./F!&,,">?F""<JAJER
 	
 ::4}45<<#9 =  r'   c                 b   t        |       }t        |      }t        t        |      t        z        }t        |j                  t        z        }t        j                  |||||      st        d| ||fz        |j                  d|j                  t        z   j                  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"   r-   r   r>   r?   r   r@   r0   lou_charToDotsrC   rE   r2   rF   rG   )rI   rJ   rL   rM   r$   rP   s         r!   
charToDotsru     s     'y1L#E*E3u:./F!&,,">?F""<MF%&'
 	
 ::4}45<<#9 =  r'   c                     | 8t        | t              s(t        dj                  t        j                              t
        j                  |       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}
    z,logCallback should be of type {} or NoneType)r   LogCallback	TypeErrorformat__name__r0   lou_registerLogCallback)logCallbacks    r!   registerLogCallbackr}   (  sG    & z+{'K:AA+BVBVW
 	
 ++K88r'   c                 X    | t         vrt        d| z        t        j                  |       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
ValueErrorr0   lou_setLogLevel)levels    r!   setLogLevelr   B  s.     I;eCDD##E**r'   r`          @         i   i   i   i   i'  i N  i0u  i@  iP  i`  __main__s   ../tables/en-us-g2.ctbzHello world!   )rK   r*   )Nr   r   )Nr   )r   )f__doc__sysr   r   r   atexitr   ctypesr   r   r	   r
   r   r   r   r   r   r   r   _loader	_functypeImportErrorr0   _is_windowsr6   r?   rA   r   rF   r"   r&   rG   r-   lou_freer1   restyperV   argtypesrB   r]   rZ   ra   re   rh   rm   rq   rt   rw   r{   r   r4   r7   rT   rX   r[   r^   rc   rf   rk   ro   rr   ru   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   rz   printr3   r'   r!   <module>r      sg  ** > = 
 
 
)*GY #$'! %%' }q((   +V0E0G  !MA$56 Q
 )  #+A+ 			 '    %    FOENFOENHFO		)   % FOENFOENHFOENENEN	#    FOENFOENHFO		-     ) FOENFOENHFOENENEN	'   # FO	FO	#    %-;    '/&:   #1980D $ $ - FOFO		$     FOFO		$     eX.+/     (#'    %*H   !2#:z*Z9x*Z/:C
"
I,294+ 
 & & &v#) ) )y$ $ $t						
  	
 
 			
 i8Y	7S	z	')	)./1
MN u  )yGY)s   M 
MM