Fixed the target parameter with Python 3 from LibreOffice. Added a list of available paths when there are more than one documents but the target was not specified.

This commit is contained in:
依瑪貓 2017-05-03 10:45:39 +08:00
parent 721fb0d1e6
commit 07ba0ab207

View File

@ -234,9 +234,11 @@ def parse_args():
if args.storage_type is None: if args.storage_type is None:
args.storage_type = "user" args.storage_type = "user"
# For Python 2 only.
# Paths are understood locally, despite of the content encoding. # Paths are understood locally, despite of the content encoding.
if args.target is not None: if sys.version_info.major == 2:
args.target = args.target.decode(locale.getpreferredencoding()) if args.target is not None:
args.target = args.target.decode(locale.getpreferredencoding())
return return
@ -296,6 +298,8 @@ def find_doc(oo, target):
print("ERROR: Found no opened document to store the macros", print("ERROR: Found no opened document to store the macros",
file=sys.stderr) file=sys.stderr)
sys.exit(1) sys.exit(1)
file_content_provider = oo.service_manager.createInstance(
"com.sun.star.ucb.FileContentProvider")
# There are opened documents. # There are opened documents.
if target is None: if target is None:
if len(opened) == 1: if len(opened) == 1:
@ -303,9 +307,9 @@ def find_doc(oo, target):
print("ERROR: There are more than one opened documens." print("ERROR: There are more than one opened documens."
" Please specify the file path.", " Please specify the file path.",
file=sys.stderr) file=sys.stderr)
for path in get_doc_paths(opened, file_content_provider):
print("* %s" % path, file=sys.stderr)
sys.exit(1) sys.exit(1)
file_content_provider = oo.service_manager.createInstance(
"com.sun.star.ucb.FileContentProvider")
matched = [] matched = []
for doc in opened: for doc in opened:
if doc.hasLocation(): if doc.hasLocation():
@ -321,28 +325,40 @@ def find_doc(oo, target):
print("ERROR: Found no matching document to store the macros.", print("ERROR: Found no matching document to store the macros.",
file=sys.stderr) file=sys.stderr)
print("Opened documents:", file=sys.stderr) print("Opened documents:", file=sys.stderr)
for doc in opened: for path in get_doc_paths(opened, file_content_provider):
if doc.hasLocation():
path = file_content_provider.getSystemPathFromFileURL(
doc.getLocation())
else:
path = doc.getTitle()
print("* %s" % path, file=sys.stderr) print("* %s" % path, file=sys.stderr)
sys.exit(1) sys.exit(1)
else: else:
print("ERROR: There are more than one matching documents.", print("ERROR: There are more than one matching documents.",
file=sys.stderr) file=sys.stderr)
print("Matching documents:", file=sys.stderr) print("Matching documents:", file=sys.stderr)
for doc in matched: for path in get_doc_paths(matched, file_content_provider):
if doc.hasLocation():
path = file_content_provider.getSystemPathFromFileURL(
doc.getLocation())
else:
path = doc.getTitle()
print("* %s" % path, file=sys.stderr) print("* %s" % path, file=sys.stderr)
sys.exit(1) sys.exit(1)
def get_doc_paths(docs, file_content_provider):
"""Returns the paths (or the titles) of the documents.
Arguments:
docs: A list of office documents.
file_content_provider: A FileContentProvider service instance.
Returns:
A list of paths, or the titles if there is no path yet,
of the documents.
"""
paths = []
for doc in docs:
if doc.hasLocation():
paths.append(
file_content_provider.getSystemPathFromFileURL(
doc.getLocation()))
else:
paths.append(doc.getTitle())
return sorted(paths)
def read_in_source_dir(projdir, ext, encoding): def read_in_source_dir(projdir, ext, encoding):
"""Read-in the source files. """Read-in the source files.