Renamed the -e (file name extension) switch to -x. Added the -e (encoding) switch to set the source file encoding. Default to the local OS encoding. On Traditional Chinese MS-Windows, this is CP950. This is mostly for MS-Windows.

This commit is contained in:
依瑪貓 2016-12-23 14:44:10 +08:00
parent b1ac321444
commit 6b0aafbdef
2 changed files with 26 additions and 22 deletions

2
TODO
View File

@ -1,7 +1,5 @@
obasync TODO obasync TODO
* File extension.
* Encoding
* LibreOffice switch (--xxx or -xxx) * LibreOffice switch (--xxx or -xxx)
* LibreOffice on MacOSX (how to install?) * LibreOffice on MacOSX (how to install?)
* Linux system-bundled LibreOffice. * Linux system-bundled LibreOffice.

View File

@ -8,6 +8,7 @@ import argparse
import os import os
import sys import sys
import time import time
import locale
def append_uno_path(): def append_uno_path():
@ -46,7 +47,7 @@ def main():
oo = Office(args.port) oo = Office(args.port)
# Synchronize the Basic macros. # Synchronize the Basic macros.
sync_macros(oo, args.ext) sync_macros(oo, args.ext, args.encoding)
print("Done. %02d:%02d elapsed." % print("Done. %02d:%02d elapsed." %
(int((time.time() - t_start) / 60), (int((time.time() - t_start) / 60),
@ -76,9 +77,14 @@ def parse_args():
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( parser.add_argument(
"-e", "--ext", metavar=".EXT", default=".vb", "-x", "--ext", metavar=".EXT", default=".vb",
help=("The file name extension of the source files. " help=("The file name extension of the source files. "
"(default: %(default)s)")) "(default: %(default)s)"))
parser.add_argument(
"-e", "--encoding", metavar="CS",
default=locale.getpreferredencoding(),
help=("The encoding 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.")
@ -98,7 +104,7 @@ def parse_args():
return return
def sync_macros(oo, ext): def sync_macros(oo, ext, encoding):
""" Synchronize the Basic macros. """ """ Synchronize the Basic macros. """
global args global args
@ -111,11 +117,11 @@ def sync_macros(oo, ext):
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, ext) update_source_dir(args.projdir, modules, ext, encoding)
# Uploads the macros onto OpenOffice/LibreOffice Basic # Uploads the macros onto OpenOffice/LibreOffice Basic
else: else:
modules = read_in_source_dir(args.projdir, ext) modules = read_in_source_dir(args.projdir, ext, encoding)
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,
@ -135,7 +141,7 @@ def sync_macros(oo, ext):
return return
def read_in_source_dir(projdir, ext): def read_in_source_dir(projdir, ext, encoding):
""" Reads-in the source macros. """ """ Reads-in the source macros. """
modules = {} modules = {}
for entry in os.listdir(projdir): for entry in os.listdir(projdir):
@ -143,11 +149,11 @@ def read_in_source_dir(projdir, ext):
if os.path.isfile(path) \ if os.path.isfile(path) \
and entry.lower().endswith(ext.lower()): and entry.lower().endswith(ext.lower()):
modname = entry[0:-3] modname = entry[0:-3]
modules[modname] = read_file(path) modules[modname] = read_file(path, encoding)
return modules return modules
def update_source_dir(projdir, modules, ext): def update_source_dir(projdir, modules, ext, encoding):
""" Updates the source macros. """ """ Updates the source macros. """
curmods = {} curmods = {}
is_in_sync = True is_in_sync = True
@ -160,12 +166,12 @@ def update_source_dir(projdir, modules, ext):
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 + ext) path = os.path.join(projdir, modname + ext)
write_file(path, modules[modname]) write_file(path, modules[modname], encoding)
print("%s added." % (modname + ext), 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])
if update_file(path, modules[modname]): if update_file(path, modules[modname], encoding):
print("%s updated." % curmods[modname], print("%s updated." % curmods[modname],
file=sys.stderr) file=sys.stderr)
is_in_sync = False is_in_sync = False
@ -234,47 +240,47 @@ def create_dialog_library(oo, libname):
libraries.storeLibraries() libraries.storeLibraries()
def read_file(path): def read_file(path, encoding):
""" Reads a file, and deals with Python 3 / 2 compatibility. """ """ Reads a file, and deals with Python 3 / 2 compatibility. """
if sys.version_info.major == 2: if sys.version_info.major == 2:
f = open(path) f = open(path)
content = f.read().decode("utf-8").replace("\r\n", "\n") content = f.read().decode(encoding).replace("\r\n", "\n")
f.close() f.close()
else: else:
f = open(path, encoding="utf-8") f = open(path, encoding=encoding)
content = f.read().replace("\r\n", "\n") content = f.read().replace("\r\n", "\n")
f.close() f.close()
return content return content
def write_file(path, content): def write_file(path, content, encoding):
""" Writes to a file, and deals with Python 3 / 2 """ Writes to a file, and deals with Python 3 / 2
compatibility. """ compatibility. """
if sys.version_info.major == 2: if sys.version_info.major == 2:
f = open(path, "w") f = open(path, "w")
f.write(content.encode("utf-8")) f.write(content.encode(encoding))
f.close() f.close()
else: else:
f = open(path, "w", encoding="utf-8") f = open(path, "w", encoding=encoding)
f.write(content) f.write(content)
f.close() f.close()
return return
def update_file(path, content): def update_file(path, content, encoding):
""" Updates a file, and deals with Python 3 / 2 """ Updates a file, and deals with Python 3 / 2
compatibility. """ compatibility. """
is_updated = False is_updated = False
if sys.version_info.major == 2: if sys.version_info.major == 2:
f = open(path, "r+") f = open(path, "r+")
if content != f.read().decode("utf-8").replace("\r\n", "\n"): if content != f.read().decode(encoding).replace("\r\n", "\n"):
f.seek(0) f.seek(0)
f.truncate(0) f.truncate(0)
f.write(content.encode("utf-8")) f.write(content.encode(encoding))
is_updated = True is_updated = True
f.close() f.close()
else: else:
f = open(path, "r+", encoding="utf-8") f = open(path, "r+", encoding=encoding)
if content != f.read().replace("\r\n", "\n"): if content != f.read().replace("\r\n", "\n"):
f.seek(0) f.seek(0)
f.truncate(0) f.truncate(0)