1212import os
1313from typing import Union
1414from sorcery import dict_of
15-
15+ import shlex , subprocess
1616import uno
1717import unohelper
1818from com .sun .star .beans import PropertyValue
2727from .utils import *
2828import pdb
2929
30+ def start_office (host :str = "localhost" ,port :str = "2000" ):
31+ """
32+ start one process LibreOffice
33+
34+ :param host: define host in the UNO connect-string --accept
35+ :param port: define port in the UNO connect-string --accept
36+ environnement had to be different for each environnement
37+ """
38+
39+ subprocess .Popen (
40+ shlex .split ('soffice \
41+ -env:UserInstallation="file:///tmp/LibO_Process'+ port + '" \
42+ -env:UserInstallation="file:///tmp/LibO_Process'+ port + '" \
43+ "--accept=socket,host="'+ host + ',port=' + port + ';urp;" \
44+ --headless --nologo --terminate_after_init \
45+ --norestore " ' ), shell = False , stdin = subprocess .PIPE ,
46+ stdout = subprocess .PIPE ,)
3047
48+ return host , port ,'file:///tmp/LibO_Process' + str (port )
3149
3250
3351class Connexion :
@@ -53,14 +71,15 @@ def __init__(self, host: str, port: str):
5371 self .host = host
5472 self .port = port
5573 self .local_ctx = uno .getComponentContext ()
56- for attempt in range (10 ):
74+ for attempt in range (3 ):
5775 try :
5876 self .ctx = self .local_ctx .ServiceManager .createInstanceWithContext (
5977 "com.sun.star.bridge.UnoUrlResolver" , self .local_ctx
6078 ).resolve (f"uno:socket,host={ host } ,port={ port } ;urp;StarOffice.ComponentContext" )
6179 except (NoConnectException , RuntimeException ) as e :
62- if attempt < 9 :
63- sleep (1 )
80+ if attempt < 2 :
81+ start_office (host ,port )
82+ sleep (2 )
6483 else :
6584 raise errors .UnoException (
6685 'connection_error' ,
0 commit comments