Fixed various places as suggested by PyCharm.

This commit is contained in:
依瑪貓 2020-08-10 22:19:15 +08:00
parent a9d3de29cc
commit f6dc837539

View File

@ -1,6 +1,6 @@
#! /opt/openoffice4/program/python #! /opt/openoffice4/program/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# OpenOffice mobile presentation constroller, as Python # OpenOffice mobile presentation controller, as Python
# by imacat <imacat@mail.imacat.idv.tw>, 2014-02-28 # by imacat <imacat@mail.imacat.idv.tw>, 2014-02-28
# Copyright (c) 2016 imacat. # Copyright (c) 2016 imacat.
@ -22,37 +22,36 @@
""" """
from __future__ import print_function from __future__ import print_function
import BaseHTTPServer
import argparse import argparse
import os import os
import os.path import os.path
import sys
import ssl import ssl
import sys
import time import time
import random
import socket
import BaseHTTPServer
def append_uno_path(): def append_uno_path():
"""Append the path of the uno module to the import path.""" """Append the path of the uno module to the import path."""
is_found_uno = False
for p in sys.path: for p in sys.path:
if os.path.exists(os.path.join(p, "uno.py")): if os.path.exists(os.path.join(p, "uno.py")):
return return
# For uno.py on MacOS # For uno.py on MacOS
cand = "/Applications/OpenOffice.app/Contents/MacOS" candidate = "/Applications/OpenOffice.app/Contents/MacOS"
if os.path.exists(os.path.join(cand, "uno.py")): if os.path.exists(os.path.join(candidate, "uno.py")):
sys.path.append(cand) sys.path.append(candidate)
return return
# Find uno.py for MS-Windows # Find uno.py for MS-Windows
cand = sys.executable candidate = sys.executable
while cand != os.path.dirname(cand): while candidate != os.path.dirname(candidate):
cand = os.path.dirname(cand) candidate = os.path.dirname(candidate)
if os.path.exists(os.path.join(cand, "uno.py")): if os.path.exists(os.path.join(candidate, "uno.py")):
sys.path.append(cand) sys.path.append(candidate)
return return
append_uno_path() append_uno_path()
import uno import uno
from com.sun.star.beans import PropertyValue from com.sun.star.beans import PropertyValue
@ -62,13 +61,12 @@ from com.sun.star.connection import NoConnectException
def main(): def main():
"""The main program.""" """The main program."""
t_start = time.time()
global args global args
# Parses the arguments # Parses the arguments
parse_args() parse_args()
run(args.docfile, args.wwwport, args.usessl) run(args.doc_file, args.www_port, args.use_ssl)
def parse_args(): def parse_args():
@ -79,10 +77,10 @@ def parse_args():
description=("Synchronize the local Basic scripts" description=("Synchronize the local Basic scripts"
" with OpenOffice/LibreOffice Basic.")) " with OpenOffice/LibreOffice Basic."))
parser.add_argument( parser.add_argument(
"docfile", metavar="PRESENTATIOON", type=presentation_doc, "doc_file", metavar="PRESENTATION", type=presentation_doc,
help=("The presentation document to play.")) help="The presentation document to play.")
parser.add_argument( parser.add_argument(
"-w", "--wwwport", metavar="N", type=int, default=53177, "-w", "--www-port", metavar="N", type=int, default=53177,
help=("The TCP port for the web presentation controller " help=("The TCP port for the web presentation controller "
"(default: %(default)s)")) "(default: %(default)s)"))
parser.add_argument( parser.add_argument(
@ -94,47 +92,47 @@ def parse_args():
args = parser.parse_args() args = parser.parse_args()
# Obtain the absolute path # Obtain the absolute path
args.docfile = os.path.abspath(args.docfile) args.doc_file = os.path.abspath(args.doc_file)
# Check whether we are using SSL. # Check whether we are using SSL.
# TODO: Changed to an option or find the certificates automatically # TODO: Changed to an option or find the certificates automatically
docdir = os.path.dirname(os.path.abspath(sys.argv[0])) doc_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
keyfile = os.path.join(docdir, "server.key.pem") key_file = os.path.join(doc_dir, "server.key.pem")
crtfile = os.path.join(docdir, "server.crt.pem") crt_file = os.path.join(doc_dir, "server.crt.pem")
args.usessl = False args.use_ssl = False
if os.path.isfile(keyfile) and os.path.isfile(crtfile): if os.path.isfile(key_file) and os.path.isfile(crt_file):
args.usessl = (keyfile, crtfile) args.use_ssl = (key_file, crt_file)
return return
def presentation_doc(docfile): def presentation_doc(doc_file):
"""Check the supplied presentation document argument. """Check the supplied presentation document argument.
Arguments: Arguments:
docfile: The supplied presentation document argument. doc_file: The supplied presentation document argument.
Returns: Returns:
The supplied presentation document argument. The supplied presentation document argument.
""" """
s = docfile.lower() s = doc_file.lower()
if not (s.endswith(".odp") if not (s.endswith(".odp")
or s.endswith(".sxi") or s.endswith(".sxi")
or s.endswith(".ppt") or s.endswith(".ppt")
or s.endswith(".pptx")): or s.endswith(".pptx")):
raise argparse.ArgumentTypeError( raise argparse.ArgumentTypeError(
"%s: Not a presentation document" % docfile) "%s: Not a presentation document" % doc_file)
path = os.path.abspath(docfile) path = os.path.abspath(doc_file)
if not os.path.exists(path): if not os.path.exists(path):
raise argparse.ArgumentTypeError( raise argparse.ArgumentTypeError(
"%s: File does not exit" % docfile) "%s: File does not exit" % doc_file)
if not os.path.isfile(path): if not os.path.isfile(path):
raise argparse.ArgumentTypeError( raise argparse.ArgumentTypeError(
"%s: Not a file" % docfile) "%s: Not a file" % doc_file)
return docfile return doc_file
def run(doc, port, usessl): def run(doc, port, use_ssl):
""" """
Start the presentation and run the web presentation controller. Start the presentation and run the web presentation controller.
""" """
@ -145,21 +143,22 @@ def run(doc, port, usessl):
oo.check_valid() oo.check_valid()
server_address = ("", port) server_address = ("", port)
httpd = BaseHTTPServer.HTTPServer(server_address, MyHTTPRequestHandler) httpd = BaseHTTPServer.HTTPServer(server_address, MyHTTPRequestHandler)
if usessl is not False: if use_ssl is not False:
httpd.socket = ssl.wrap_socket( httpd.socket = ssl.wrap_socket(
httpd.socket, httpd.socket,
keyfile=usessl[0], keyfile=use_ssl[0],
certfile=usessl[1], certfile=use_ssl[1],
server_side=True) server_side=True)
httpd.serve_forever() httpd.serve_forever()
class PresentationController: class PresentationController:
"""The OpenOffice mobile presentation controller.""" """The OpenOffice mobile presentation controller."""
WIN_DETACHED_PROCESS = 0x00000008
def __init__(self, docfile): def __init__(self, doc_file):
"""Initialize the object.""" """Initialize the object."""
self.file = docfile self.file = doc_file
self.port = 2002 self.port = 2002
self.bootstrap_context = None self.bootstrap_context = None
self.service_manager = None self.service_manager = None
@ -257,11 +256,10 @@ class PresentationController:
from subprocess import Popen from subprocess import Popen
soffice = os.path.join( soffice = os.path.join(
os.path.dirname(uno.__file__), "soffice.exe") os.path.dirname(uno.__file__), "soffice.exe")
DETACHED_PROCESS = 0x00000008
Popen([soffice, Popen([soffice,
"-accept=socket,host=localhost,port=%d;urp;" % "-accept=socket,host=localhost,port=%d;urp;" %
self.port], self.port],
close_fds=True, creationflags=DETACHED_PROCESS) close_fds=True, creationflags=self.WIN_DETACHED_PROCESS)
time.sleep(2) time.sleep(2)
return return
@ -295,7 +293,8 @@ class PresentationController:
file=sys.stderr) file=sys.stderr)
sys.exit(1) sys.exit(1)
def __find_posix_soffice(self): @staticmethod
def __find_posix_soffice():
"""Find soffice on POSIX systems (Linux or MacOSX). """Find soffice on POSIX systems (Linux or MacOSX).
Returns: Returns:
@ -310,14 +309,14 @@ class PresentationController:
return soffice return soffice
# Now we have LibreOffice on MacOSX and Linux # Now we have LibreOffice on MacOSX and Linux
# OpenOffice/LibreOffice vender installation. # OpenOffice/LibreOffice vendor installation.
# LibreOffice on MacOSX. # LibreOffice on MacOSX.
soffice = "/Applications/LibreOffice.app/Contents/MacOS/soffice" soffice = "/Applications/LibreOffice.app/Contents/MacOS/soffice"
if os.path.exists(soffice): if os.path.exists(soffice):
return soffice return soffice
# Linux OpenOffice/LibreOffice vender installation. # Linux OpenOffice/LibreOffice vendor installation.
soffice = "/usr/bin/soffice" soffice = "/usr/bin/soffice"
if os.path.exists(soffice): if os.path.exists(soffice):
return soffice return soffice
@ -325,7 +324,8 @@ class PresentationController:
# Not found # Not found
return None return None
def __is_soffice_lo(self, soffice): @staticmethod
def __is_soffice_lo(soffice):
"""Check whether the soffice executable is LibreOffice. """Check whether the soffice executable is LibreOffice.
LibreOffice on POSIX systems accepts "--accept" instead of LibreOffice on POSIX systems accepts "--accept" instead of
@ -423,26 +423,26 @@ if __name__ == "__main__":
if len(sys.argv) != 2: if len(sys.argv) != 2:
sys.stderr.write("Please specify the presentation document.\n") sys.stderr.write("Please specify the presentation document.\n")
sys.exit(1) sys.exit(1)
docfile = os.path.abspath(sys.argv[1]) doc_file = os.path.abspath(sys.argv[1])
s = docfile.lower() s = doc_file.lower()
if not (s.endswith(".odp") if not (s.endswith(".odp")
or s.endswith(".sxi") or s.endswith(".sxi")
or s.endswith(".ppt") or s.endswith(".ppt")
or s.endswith(".pptx")): or s.endswith(".pptx")):
sys.stderr.write(docfile + ": not a presentation document.\n") sys.stderr.write(doc_file + ": not a presentation document.\n")
sys.exit(1) sys.exit(1)
if not os.path.exists(docfile): if not os.path.exists(doc_file):
sys.stderr.write(docfile + ": file does not exist.\n") sys.stderr.write(doc_file + ": file does not exist.\n")
sys.exit(1) sys.exit(1)
if not os.path.isfile(docfile): if not os.path.isfile(doc_file):
sys.stderr.write(docfile + ": file does not exist.\n") sys.stderr.write(doc_file + ": file does not exist.\n")
sys.exit(1) sys.exit(1)
mydir = os.path.dirname(os.path.abspath(sys.argv[0])) my_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
keyfile = os.path.join(mydir, "server.key.pem") key_file = os.path.join(my_dir, "server.key.pem")
crtfile = os.path.join(mydir, "server.crt.pem") crt_file = os.path.join(my_dir, "server.crt.pem")
usessl = False use_ssl = False
if os.path.isfile(keyfile) and os.path.isfile(crtfile): if os.path.isfile(key_file) and os.path.isfile(crt_file):
usessl = (keyfile, crtfile) use_ssl = (key_file, crt_file)
oo = PresentationController(docfile) oo = PresentationController(doc_file)
run() run()
""" """