From 7efd1ec827d483fb8082b810746dc758ac59fa99 Mon Sep 17 00:00:00 2001 From: Shin'ichiro Kawasaki Date: Sat, 10 Oct 2020 17:32:52 +0900 Subject: [PATCH] scratch_link.py: Fix errors when it is run as package command When scratch_link.py is executed as the command specified by setuptools' entry_point, following errors happen. Fix them. - Package name can not be resolved to import modules. - The global variable sessionTypes can not be refereed from the main function. - Command line options are not parsed in main function. Signed-off-by: Shin'ichiro Kawasaki --- bluepy_scratch_link/__main__.py | 2 +- bluepy_scratch_link/scratch_link.py | 34 +++++++++++++++-------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/bluepy_scratch_link/__main__.py b/bluepy_scratch_link/__main__.py index 1b2f731..9edba89 100644 --- a/bluepy_scratch_link/__main__.py +++ b/bluepy_scratch_link/__main__.py @@ -1,6 +1,6 @@ #!/usr/bin/python -from scratch_link import main as _main +from bluepy_scratch_link.scratch_link import main as _main if __name__ == '__main__': _main() diff --git a/bluepy_scratch_link/scratch_link.py b/bluepy_scratch_link/scratch_link.py index c927f5d..c02c8d2 100755 --- a/bluepy_scratch_link/scratch_link.py +++ b/bluepy_scratch_link/scratch_link.py @@ -21,30 +21,17 @@ import bluetooth # for BLESession (e.g. BBC micro:bit) from bluepy.btle import Scanner, UUID, Peripheral, DefaultDelegate from bluepy.btle import BTLEDisconnectError, BTLEManagementError -import bluepy_helper_cap +from bluepy_scratch_link import bluepy_helper_cap import threading import time import queue # for websockets certificate -import gencert +from bluepy_scratch_link import gencert logLevel = logging.INFO -# handle command line options -if __name__ == "__main__": - opts = [opt for opt in sys.argv[1:] if opt.startswith("-")] - if "-h" in opts: - print((f"Usage: {sys.argv[0]} [OPTS]\n" - "OPTS:\t-h Show this help.\n" - "\t-d Print debug messages." - )) - sys.exit(1) - elif "-d" in opts: - print("Print debug messages") - logLevel = logging.DEBUG - # for logging logger = logging.getLogger(__name__) formatter = logging.Formatter(fmt='%(asctime)s %(message)s') @@ -652,6 +639,7 @@ class BLESession(Session): return self.status == self.DONE async def ws_handler(websocket, path): + sessionTypes = { '/scratch/ble': BLESession, '/scratch/bt': BTSession } try: logger.info(f"Start session for web socket path: {path}") loop = asyncio.get_event_loop() @@ -676,6 +664,19 @@ def stack_trace(): print(line) def main(): + opts = [opt for opt in sys.argv[1:] if opt.startswith("-")] + if "-h" in opts: + print((f"Usage: {sys.argv[0]} [OPTS]\n" + "OPTS:\t-h Show this help.\n" + "\t-d Print debug messages." + )) + sys.exit(1) + elif "-d" in opts: + print("Print debug messages") + logLevel = logging.DEBUG + handler.setLevel(logLevel) + logger.setLevel(logLevel) + # Prepare certificate of the WSS server gencert.prep_cert() @@ -684,7 +685,6 @@ def main(): localhost_cer = gencert.cert_file_path localhost_key = gencert.key_file_path ssl_context.load_cert_chain(localhost_cer, localhost_key) - sessionTypes = { '/scratch/ble': BLESession, '/scratch/bt': BTSession } start_server = websockets.serve( ws_handler, "device-manager.scratch.mit.edu", 20110, ssl=ssl_context @@ -701,3 +701,5 @@ def main(): except Exception as e: logger.info("Restarting scratch-link...") +if __name__ == "__main__": + main()