Skip to content

Commit

Permalink
Update readme, more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Regebro committed Aug 18, 2023
1 parent b6f189c commit ffa546a
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 53 deletions.
46 changes: 35 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,37 +89,61 @@ and `python3 -m unoserver.comparer` with the same arguments as the main scripts.
Unoserver
~~~~~~~~~

``unoserver [-h] [--interface INTERFACE] [--port PORT] [--daemon] [--executable EXECUTABLE]``
.. code::
* `--interface`: The interface used by the server, defaults to "localhost"
* `--port`: The port used by the server, defaults to "2002"
* `--daemon`: Deamonize the server
unoserver [-h] [--interface INTERFACE] [--uno-interface UNO_INTERFACE] [--port PORT] [--uno-port UNO_PORT]
[--daemon] [--executable EXECUTABLE] [--user-installation USER_INSTALLATION]
[--libreoffice-pid-file LIBREOFFICE_PID_FILE]
* `--interface`: The interface used by the XMLRPC server, defaults to "127.0.0.1"
* `--port`: The port used by the XMLRPC server, defaults to "2003"
* `--uno-interface`: The interface used by the LibreOffice server, defaults to "127.0.0.1"
* `--uno-port`: The port used by the LibreOffice server, defaults to "2002"
* `--daemon`: Deamonize the server
* `--executable`: The path to the LibreOffice executable
* `--user-installation`: The path to the LibreOffice user profile, defaults to a dynamically created temporary directory
* `--libreoffice-pid-file`: If set, unoserver will write the Libreoffice PID to this file.
If started in daemon mode, the file will not be deleted when unoserver exits.

Unoconvert
~~~~~~~~~~

``unoconvert [-h] [--convert-to CONVERT_TO] [--filter FILTER_NAME] [--interface INTERFACE] [--port PORT] infile outfile``
.. code::
unoconvert [-h] [--convert-to CONVERT_TO] [--filter FILTER] [--filter-options FILTER_OPTIONS]
[--update-index] [--dont-update-index] [--host HOST] [--port PORT]
[--host-location {auto,remote,local}] infile outfile
* `infile`: The path to the file to be converted (use - for stdin)
* `outfile`: The path to the converted file (use - for stdout)
* `--convert-to`: The file type/extension of the output file (ex pdf). Required when using stdout
* `--filter`: The export filter to use when converting. It is selected automatically if not specified.
* `--interface`: The interface used by the server, defaults to "localhost"
* `--filter-options`: Options for the export filter, in name=value format. Use true/false for boolean values.
* `--host`: The host used by the server, defaults to "127.0.0.1"
* `--port`: The port used by the server, defaults to "2002"
* `--host-location`: The host location determines the handling of files. If you run the client on the
same machine as the server, it can be set to local, and the files are sent as paths. If they are
different machines, it is remote and the files are sent as binary data. Default is auto, and it will
send the file as a path if the host is 127.0.0.1 or localhost, and binary data for other hosts.

Unocompare
~~~~~~~~~~

``unocompare [-h] [--convert-to CONVERT_TO] [--interface INTERFACE] [--port PORT] infile inorigfile outfile``
.. code::
* `infile`: The path to the modified file to be compared with the original one (use - for stdin)
* `inorigfile`: The path to the original file to be compared with the modified one (use - for stdin)
unocompare [-h] [--file-type FILE_TYPE] [--host HOST] [--port PORT] [--host-location {auto,remote,local}]
oldfile newfile outfile
* `oldfile`: The path to the older file to be compared with the original one (use - for stdin)
* `newfile`: The path to the newer file to be compared with the modified one (use - for stdin)
* `outfile`: The path to the result of the comparison and converted file (use - for stdout)
* `--convert-to`: The file type/extension of the output file (ex pdf). Required when using stdout
* `--interface`: The interface used by the server, defaults to "localhost"
* `--file-type`: The file type/extension of the result output file (ex pdf). Required when using stdout
* `--host`: The host used by the server, defaults to "127.0.0.1"
* `--port`: The port used by the server, defaults to "2002"
* `--host-location`: The host location determines the handling of files. If you run the client on the
same machine as the server, it can be set to local, and the files are sent as paths. If they are
different machines, it is remote and the files are sent as binary data. Default is auto, and it will
send the file as a path if the host is 127.0.0.1 or localhost, and binary data for other hosts.


Development and Testing
Expand Down
121 changes: 81 additions & 40 deletions src/unoserver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@
class UnoClient:
"""An RPC client for Unoserver"""

def __init__(self, server="127.0.0.1", port="2003"):
def __init__(self, server="127.0.0.1", port="2003", host_location="auto"):
self.server = server
self.port = port
if host_location == "auto":
if server in ("127.0.0.1", "localhost"):
self.remote = False
else:
self.remote = True
elif host_location == "remote":
self.remote = True
elif host_location == "local":
self.remote = False
else:
raise RuntimeError("host_location can be 'auto', 'remote', or 'local'")

def convert(
self,
Expand Down Expand Up @@ -67,7 +78,7 @@ def convert(
else:
convert_to = os.path.splitext(outpath)[-1].strip(os.path.extsep)

if inpath and self.server not in ("127.0.0.1", "localhost"):
if self.remote and inpath:
with open(inpath, "rb") as infile:
indata = infile.read()
inpath = None
Expand All @@ -88,8 +99,8 @@ def convert(
with open(outpath, "wb") as outfile:
outfile.write(result.data)
else:
# Pipe result to stdout
sys.stdout.buffer.write(result.data)
# Return the result as a blob
return result.data

def compare(
self,
Expand Down Expand Up @@ -135,7 +146,7 @@ def compare(
elif filetype is None:
filetype = os.path.splitext(outpath)[-1].strip(os.path.extsep)

if self.server not in ("127.0.0.1", "localhost"):
if self.remote:
if oldpath:
with open(oldpath, "rb") as infile:
olddata = infile.read()
Expand All @@ -161,8 +172,8 @@ def compare(
with open(outpath, "wb") as outfile:
outfile.write(result.data)
else:
# Pipe result to stdout
sys.stdout.buffer.write(result.data)
# Return the result as a blob
return result.data


def converter_main():
Expand Down Expand Up @@ -204,11 +215,21 @@ def converter_main():
)
parser.set_defaults(update_index=True)
parser.add_argument(
"--interface", default="127.0.0.1", help="The interface used by the server"
"--host", default="127.0.0.1", help="The host the server runs on"
)
parser.add_argument("--port", default="2003", help="The port used by the server")
parser.add_argument(
"--host-location",
default="auto",
choices=["auto", "remote", "local"],
help="The host location determines the handling of files. If you run the client on the "
"same machine as the server, it can be set to local, and the files are sent as paths. "
"If they are different machines, it is remote and the files are sent as binary data. "
"Default is auto, and it will send the file as a path if the host is 127.0.0.1 or "
"localhost, and binary data for other hosts.",
)
args = parser.parse_args()
client = UnoClient(args.interface, args.port)
client = UnoClient(args.host, args.port, args.host_location)

if args.outfile == "-":
# Set outfile to None, to get the data returned from the function,
Expand All @@ -218,23 +239,22 @@ def converter_main():
if args.infile == "-":
# Get data from stdin
indata = sys.stdin.buffer.read()
client.convert(
indata=indata,
outpath=args.outfile,
convert_to=args.convert_to,
filtername=args.filter,
filter_options=args.filter_options,
update_index=args.update_index,
)
args.infile = None
else:
client.convert(
inpath=args.infile,
outpath=args.outfile,
convert_to=args.convert_to,
filtername=args.filter,
filter_options=args.filter_options,
update_index=args.update_index,
)
indata = None

result = client.convert(
inpath=args.infile,
indata=indata,
outpath=args.outfile,
convert_to=args.convert_to,
filtername=args.filter,
filter_options=args.filter_options,
update_index=args.update_index,
)

if args.outfile is None:
sys.stdout.buffer.write(result)


def comparer_main():
Expand All @@ -259,31 +279,52 @@ def comparer_main():
help="The file type/extension of the result file (ex pdf). Required when using stdout",
)
parser.add_argument(
"--interface", default="127.0.0.1", help="The interface used by the server"
"--host", default="127.0.0.1", help="The host the server run on"
)
parser.add_argument("--port", default="2003", help="The port used by the server")
parser.add_argument(
"--host-location",
default="auto",
choices=["auto", "remote", "local"],
help="The host location determines the handling of files. If you run the client on the "
"same machine as the server, it can be set to local, and the files are sent as paths. "
"If they are different machines, it is remote and the files are sent as binary data. "
"Default is auto, and it will send the file as a path if the host is 127.0.0.1 or "
"localhost, and binary data for other hosts.",
)
args = parser.parse_args()

client = UnoClient(args.interface, args.port)
client = UnoClient(args.host, args.port, args.host_location)

if args.outfile == "-":
# Set outfile to None, to get the data returned from the function,
# instead of written to a file.
args.outfile = None

if args.oldfile == "-" and args.newfile == "-":
raise RuntimeError("You can't read both files from stdin")

if args.oldfile == "-":
# Get data from stdin
indata = sys.stdin.buffer.read()
client.compare(
olddata=indata,
newpath=args.newfile,
outpath=args.outfile,
filetype=args.file_type,
)
olddata = sys.stdin.buffer.read()
newdata = None
args.oldfile = None

elif args.newfile == "-":
newdata = sys.stdin.buffer.read()
olddata = None
args.newfile = None
else:
client.compare(
oldpath=args.oldfile,
newpath=args.newfile,
outpath=args.outfile,
filetype=args.file_type,
)
olddata = newdata = None

result = client.compare(
oldpath=args.oldfile,
olddata=olddata,
newpath=args.newfile,
newdata=newdata,
outpath=args.outfile,
filetype=args.file_type,
)

if args.outfile is None:
sys.stdout.buffer.write(result)
4 changes: 2 additions & 2 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def test_convert_not_local():
with tempfile.NamedTemporaryFile(suffix=".pdf") as outfile:
sys.argv = [
"unoconverter",
"--interface",
"--host",
hostname,
"--port=2103",
infile,
Expand Down Expand Up @@ -240,7 +240,7 @@ def test_compare_not_local():
with tempfile.NamedTemporaryFile(suffix=".pdf") as outfile:
sys.argv = [
"unoconverter",
"--interface",
"--host",
hostname,
"--port=2103",
infile1,
Expand Down

0 comments on commit ffa546a

Please sign in to comment.