diff --git a/src/unoserver/client.py b/src/unoserver/client.py index f20f3c3..a5dce81 100644 --- a/src/unoserver/client.py +++ b/src/unoserver/client.py @@ -2,6 +2,7 @@ import logging import os import sys +import time from importlib import metadata from xmlrpc.client import ServerProxy @@ -41,6 +42,22 @@ def __init__(self, server="127.0.0.1", port="2003", host_location="auto"): else: raise RuntimeError("host_location can be 'auto', 'remote', or 'local'") + def _connect(self, proxy, retries=5, sleep=10): + """Check the connection to the proxy multiple times + + Returns the info() data (unoserver version + filters)""" + + while retries > 0: + try: + info = proxy.info() + return info + except ConnectionError: + retries -= 1 + if retries > 0: + time.sleep(sleep) + else: + raise + def convert( self, inpath=None, @@ -95,6 +112,8 @@ def convert( raise ValueError("The outpath can not be a directory") with ServerProxy(f"http://{self.server}:{self.port}", allow_none=True) as proxy: + self._connect(proxy) + result = proxy.convert( inpath, indata, @@ -175,6 +194,8 @@ def compare( newpath = os.path.abspath(newpath) with ServerProxy(f"http://{self.server}:{self.port}", allow_none=True) as proxy: + self._connect(proxy) + result = proxy.compare( oldpath, olddata, diff --git a/src/unoserver/server.py b/src/unoserver/server.py index 23e29b7..34a2468 100644 --- a/src/unoserver/server.py +++ b/src/unoserver/server.py @@ -27,11 +27,13 @@ def __init__( addr_info = socket.getaddrinfo(addr[0], addr[1], proto=socket.IPPROTO_TCP) if len(addr_info) == 0: - raise RuntimeError(f"Could not get interface information for {addr[0]}:{addr[1]}") + raise RuntimeError( + f"Could not get interface information for {addr[0]}:{addr[1]}" + ) self.address_family = addr_info[0][0] self.socket_type = addr_info[0][1] - super().__init__(addr=addr_info[0][4], allow_none=allow_none) + super().__init__(addr=addr_info[0][4], allow_none=allow_none) class UnoServer: @@ -53,6 +55,8 @@ def __init__( self.xmlrcp_server = None def start(self, executable="libreoffice"): + import time + logger.info(f"Starting unoserver {__version__}.") connection = ( @@ -98,18 +102,35 @@ def signal_handler(signum, frame): if platform.system() != "Windows": signal.signal(signal.SIGHUP, signal_handler) + time.sleep(10) + self.xmlrcp_thread.start() return self.libreoffice_process def serve(self): # Create server - with XMLRPCServer( - (self.interface, int(self.port)), allow_none=True - ) as server: + with XMLRPCServer((self.interface, int(self.port)), allow_none=True) as server: self.xmlrcp_server = server server.register_introspection_functions() + @server.register_function + def info(): + conv = converter.UnoConverter( + interface=self.uno_interface, port=self.uno_port + ) + import_filters = conv.get_filter_names( + conv.get_available_import_filters() + ) + export_filters = conv.get_filter_names( + conv.get_available_export_filters() + ) + return { + "unoserver": __version__, + "import_filters": import_filters, + "export_filters": export_filters, + } + @server.register_function def convert( inpath=None,