U
    N³}_Ó  ã                   @   sd   d dl Z d dlZd dlmZ d dlmZ d dlZd dlmZ d dl	m
Z G dd„ deƒZdd	„ ZdS )
é    N)Úplatform)Úsleep)ÚNoConnectException)Ú	Exceptionc                   @   s   e Zd ZdS )ÚBootstrapExceptionN)Ú__name__Ú
__module__Ú__qualname__© r
   r
   ú,/usr/lib/libreoffice/program/officehelper.pyr   "   s   r   c            	   
   C   sN  züdt jkrt jd } nd} t j | d¡} t d¡r<| d7 } t ¡  dtt ¡ ƒdd…  }| d	d
d d|dg¡f}t  	t j
| |¡ t ¡ }|j d|¡}d d|dg¡}d}z| |¡}W qúW q® tk
rö   |d8 }|dkrêtddƒ‚tdƒ Y q®X q®W nL tk
r   ‚ Y n6 tk
rH } ztdt|ƒ dƒ‚W 5 d}~X Y nX |S )a?  Bootstrap OOo and PyUNO Runtime.
    The soffice process is started opening a named pipe of random name, then the local context is used
	to access the pipe. This function directly returns the remote component context, from whereon you can
	get the ServiceManager by calling getServiceManager() on the returned object.
	ZUNO_PATHÚ ZsofficeÚwinz.exeÚunoé   Nz--nologoz--nodefaultz--accept=pipe,name=z;urp;z"com.sun.star.bridge.UnoUrlResolverzuno:pipe,name=z ;urp;StarOffice.ComponentContexté   é   r   z!Cannot connect to soffice server.g      à?zCaught exception )ÚosÚenvironÚpathÚjoinr   Ú
startswithÚrandomZseedÚstrÚspawnvÚP_NOWAITr   ZgetComponentContextZServiceManagerZcreateInstanceWithContextZresolver   r   r   r   )	ZsOfficeZ	sPipeNameZcmdArrayZxLocalContextZresolverZsConnectZnLoopZxContextÚer
   r
   r   Ú	bootstrap%   s>    

 ÿ

$r   )r   r   Úsysr   Útimer   r   Zcom.sun.star.connectionr   Zcom.sun.star.unor   ZUnoExceptionr   r   r
   r
   r
   r   Ú<module>   s   