Skip to content

Commit

Permalink
Added an "info()" method to the server, and a retrying that uses it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Regebro committed Jul 22, 2024
1 parent 2510393 commit f75e5a6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
21 changes: 21 additions & 0 deletions src/unoserver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import sys
import time

from importlib import metadata
from xmlrpc.client import ServerProxy
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 26 additions & 5 deletions src/unoserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -53,6 +55,8 @@ def __init__(
self.xmlrcp_server = None

def start(self, executable="libreoffice"):
import time

logger.info(f"Starting unoserver {__version__}.")

connection = (
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f75e5a6

Please sign in to comment.