Renamed the OpenOffice class to Office. Updated several pieces of text for LibreOffice. Added the -e (file name extension) switch to set custom file name extension.

This commit is contained in:
依瑪貓 2016-12-23 11:38:51 +08:00
parent 0e2e537916
commit b1ac321444
2 changed files with 39 additions and 23 deletions

4
TODO
View File

@ -1,6 +1,10 @@
obasync TODO obasync TODO
* File extension. * File extension.
* Encoding
* LibreOffice switch (--xxx or -xxx)
* LibreOffice on MacOSX (how to install?)
* Linux system-bundled LibreOffice.
* Writes the README (reStructureText). * Writes the README (reStructureText).
* Documentation. * Documentation.
* Python 3 * Python 3

View File

@ -42,11 +42,11 @@ def main():
# Parses the arguments # Parses the arguments
parse_args() parse_args()
# Connects to the OpenOffice # Connects to the OpenOffice/LibreOffice
oo = OpenOffice(args.port) oo = Office(args.port)
# Synchronize the Basic macros. # Synchronize the Basic macros.
sync_macros(oo) sync_macros(oo, args.ext)
print("Done. %02d:%02d elapsed." % print("Done. %02d:%02d elapsed." %
(int((time.time() - t_start) / 60), (int((time.time() - t_start) / 60),
@ -75,6 +75,10 @@ def parse_args():
"-p", "--port", metavar="N", type=int, default=2002, "-p", "--port", metavar="N", type=int, default=2002,
help=("The TCP port to communicate with " help=("The TCP port to communicate with "
"OpenOffice/LibreOffice with (default: %(default)s)")) "OpenOffice/LibreOffice with (default: %(default)s)"))
parser.add_argument(
"-e", "--ext", metavar=".EXT", default=".vb",
help=("The file name extension of the source files. "
"(default: %(default)s)"))
parser.add_argument( parser.add_argument(
"-r", "--run", metavar="Module.Macro", "-r", "--run", metavar="Module.Macro",
help="The macro to run after the upload, if any.") help="The macro to run after the upload, if any.")
@ -87,28 +91,31 @@ def parse_args():
# Obtains the library name from the path # Obtains the library name from the path
if args.library is None: if args.library is None:
args.library = os.path.basename(args.projdir) args.library = os.path.basename(args.projdir)
# Adjusts the file name extension.
if args.ext[0] != ".":
args.ext = "." + args.ext
return return
def sync_macros(oo): def sync_macros(oo, ext):
""" Synchronize the Basic macros. """ """ Synchronize the Basic macros. """
global args global args
libraries = oo.service_manager.createInstance( libraries = oo.service_manager.createInstance(
"com.sun.star.script.ApplicationScriptLibraryContainer") "com.sun.star.script.ApplicationScriptLibraryContainer")
# Downloads the macros from OpenOffice # Downloads the macros from OpenOffice/LibreOffice Basic
if args.get: if args.get:
modules = read_basic_modules(libraries, args.library) modules = read_basic_modules(libraries, args.library)
if len(modules) == 0: if len(modules) == 0:
print("ERROR: Library %s does not exist" % args.library, print("ERROR: Library %s does not exist" % args.library,
file=sys.stderr) file=sys.stderr)
return return
update_source_dir(args.projdir, modules) update_source_dir(args.projdir, modules, ext)
# Uploads the macros onto OpenOffice # Uploads the macros onto OpenOffice/LibreOffice Basic
else: else:
modules = read_in_source_dir(args.projdir) modules = read_in_source_dir(args.projdir, ext)
if len(modules) == 0: if len(modules) == 0:
print("ERROR: Found no source macros in %s" % print("ERROR: Found no source macros in %s" %
args.projdir, args.projdir,
@ -128,31 +135,33 @@ def sync_macros(oo):
return return
def read_in_source_dir(projdir): def read_in_source_dir(projdir, ext):
""" Reads-in the source macros. """ """ Reads-in the source macros. """
modules = {} modules = {}
for entry in os.listdir(projdir): for entry in os.listdir(projdir):
path = os.path.join(projdir, entry) path = os.path.join(projdir, entry)
if os.path.isfile(path) and entry.lower().endswith(".vb"): if os.path.isfile(path) \
and entry.lower().endswith(ext.lower()):
modname = entry[0:-3] modname = entry[0:-3]
modules[modname] = read_file(path) modules[modname] = read_file(path)
return modules return modules
def update_source_dir(projdir, modules): def update_source_dir(projdir, modules, ext):
""" Updates the source macros. """ """ Updates the source macros. """
curmods = {} curmods = {}
is_in_sync = True is_in_sync = True
for entry in os.listdir(projdir): for entry in os.listdir(projdir):
path = os.path.join(projdir, entry) path = os.path.join(projdir, entry)
if os.path.isfile(path) and entry.lower().endswith(".vb"): if os.path.isfile(path) \
modname = entry[0:-3] and entry.lower().endswith(ext.lower()):
modname = entry[0:-len(ext)]
curmods[modname] = entry curmods[modname] = entry
for modname in sorted(modules.keys()): for modname in sorted(modules.keys()):
if modname not in curmods: if modname not in curmods:
path = os.path.join(projdir, modname + ".vb") path = os.path.join(projdir, modname + ext)
write_file(path, modules[modname]) write_file(path, modules[modname])
print("%s.vb added." % modname, file=sys.stderr) print("%s added." % (modname + ext), file=sys.stderr)
is_in_sync = False is_in_sync = False
else: else:
path = os.path.join(projdir, curmods[modname]) path = os.path.join(projdir, curmods[modname])
@ -172,7 +181,8 @@ def update_source_dir(projdir, modules):
def read_basic_modules(libraries, libname): def read_basic_modules(libraries, libname):
""" Reads the OpenOffice Basic macros from the macros storage. """ """ Reads the OpenOffice/LibreOffice Basic macros from the macros
storage. """
modules = {} modules = {}
if not libraries.hasByName(libname): if not libraries.hasByName(libname):
return modules return modules
@ -184,7 +194,7 @@ def read_basic_modules(libraries, libname):
def update_basic_modules(libraries, libname, modules, oo): def update_basic_modules(libraries, libname, modules, oo):
""" Updates the OpenOffice Basic macros storage. """ """ Updates the OpenOffice/LibreOffice Basic macro storage. """
if not libraries.hasByName(libname): if not libraries.hasByName(libname):
libraries.createLibrary(libname) libraries.createLibrary(libname)
print("Script library %s created." % libname, file=sys.stderr) print("Script library %s created." % libname, file=sys.stderr)
@ -274,11 +284,11 @@ def update_file(path, content):
return is_updated return is_updated
class OpenOffice: class Office:
""" The OpenOffice connection. """ """ The OpenOffice/LibreOffice connection. """
def __init__(self, port=2002): def __init__(self, port=2002):
"""Initializes the object.""" """ Initializes the object."""
self.port = port self.port = port
self.bootstrap_context = None self.bootstrap_context = None
self.service_manager = None self.service_manager = None
@ -286,7 +296,7 @@ class OpenOffice:
self.connect() self.connect()
def connect(self): def connect(self):
"""Connects to the running OpenOffice process.""" """ Connects to the running OpenOffice/LibreOffice process."""
# Obtains the local context # Obtains the local context
local_context = uno.getComponentContext() local_context = uno.getComponentContext()
# Obtains the local service manager # Obtains the local service manager
@ -311,7 +321,8 @@ class OpenOffice:
"com.sun.star.frame.Desktop", self.bootstrap_context) "com.sun.star.frame.Desktop", self.bootstrap_context)
def start_oo(self): def start_oo(self):
"""Starts the OpenOffice in server listening mode""" """ Starts the OpenOffice/LibreOffice in server listening
mode. """
# For MS-Windows, which does not have fork() # For MS-Windows, which does not have fork()
if os.name == "nt": if os.name == "nt":
from subprocess import Popen from subprocess import Popen
@ -342,7 +353,8 @@ class OpenOffice:
"-accept=socket,host=localhost,port=%d;urp;" % "-accept=socket,host=localhost,port=%d;urp;" %
self.port) self.port)
except OSError: except OSError:
print("%s: Failed to run the OpenOffice server." % ooexec, print("%s: Failed to run the"
" OpenOffice/LibreOffice server." % ooexec,
file=sys.stderr) file=sys.stderr)
sys.exit(1) sys.exit(1)