From b8d0337b6c278780a67efb8dd74e9cea3bec7e36 Mon Sep 17 00:00:00 2001 From: Josh Marshall Date: Fri, 20 Jul 2018 13:35:19 +1000 Subject: [PATCH] Python 2.7 compatibility --- Firmware/fibre/python/fibre/__init__.py | 2 +- Firmware/fibre/python/fibre/discovery.py | 17 +++++++++-------- Firmware/fibre/python/fibre/protocol.py | 7 ++++--- Firmware/fibre/python/fibre/serial_transport.py | 1 + Firmware/fibre/python/fibre/tcp_transport.py | 2 +- .../fibre/python/fibre/usbbulk_transport.py | 1 + Firmware/fibre/python/fibre/utils.py | 13 +++++++++++-- tools/odrive/dfu.py | 7 +++++-- tools/odrive/utils.py | 13 ++++++++++--- tools/run_tests.py | 3 ++- tools/setup.py | 9 ++++++++- 11 files changed, 53 insertions(+), 22 deletions(-) diff --git a/Firmware/fibre/python/fibre/__init__.py b/Firmware/fibre/python/fibre/__init__.py index 7309c8b2..0b8ed1b4 100644 --- a/Firmware/fibre/python/fibre/__init__.py +++ b/Firmware/fibre/python/fibre/__init__.py @@ -1,5 +1,5 @@ from .discovery import find_any, find_all -from .utils import Event, Logger +from .utils import Event, Logger, TimeoutError from .protocol import ChannelBrokenException, ChannelDamagedException from .shell import launch_shell diff --git a/Firmware/fibre/python/fibre/discovery.py b/Firmware/fibre/python/fibre/discovery.py index f1e62499..a3751633 100644 --- a/Firmware/fibre/python/fibre/discovery.py +++ b/Firmware/fibre/python/fibre/discovery.py @@ -11,7 +11,7 @@ import fibre.protocol import fibre.utils import fibre.remote_object from fibre.utils import Event, Logger -from fibre.protocol import ChannelBrokenException +from fibre.protocol import ChannelBrokenException, TimeoutError # Load all installed transport layers @@ -20,25 +20,25 @@ channel_types = {} try: import fibre.usbbulk_transport channel_types['usb'] = fibre.usbbulk_transport.discover_channels -except ModuleNotFoundError: +except ImportError: pass try: import fibre.serial_transport channel_types['serial'] = fibre.serial_transport.discover_channels -except ModuleNotFoundError: +except ImportError: pass try: import fibre.tcp_transport channel_types['tcp'] = fibre.tcp_transport.discover_channels -except ModuleNotFoundError: +except ImportError: pass try: import fibre.udp_transport channel_types['udp'] = fibre.udp_transport.discover_channels -except ModuleNotFoundError: +except ImportError: pass def noprint(text): @@ -102,9 +102,10 @@ def find_all(path, serial_number, prefix = search_spec.split(':')[0] the_rest = ':'.join(search_spec.split(':')[1:]) if prefix in channel_types: - threading.Thread(target=channel_types[prefix], - args=(the_rest, serial_number, did_discover_channel, search_cancellation_token, channel_termination_token, logger), - daemon=True).start() + t = threading.Thread(target=channel_types[prefix], + args=(the_rest, serial_number, did_discover_channel, search_cancellation_token, channel_termination_token, logger)) + t.daemon = True + t.start() else: raise Exception("Invalid path spec \"{}\"".format(search_spec)) diff --git a/Firmware/fibre/python/fibre/protocol.py b/Firmware/fibre/python/fibre/protocol.py index 50053a11..ec88a3fc 100644 --- a/Firmware/fibre/python/fibre/protocol.py +++ b/Firmware/fibre/python/fibre/protocol.py @@ -6,7 +6,7 @@ import sys import threading import traceback #import fibre.utils -from fibre.utils import Event, wait_any +from fibre.utils import Event, wait_any, TimeoutError import abc if sys.version_info >= (3, 4): @@ -65,7 +65,6 @@ def calc_crc16(remainder, value): #print(hex(calc_crc8(0x12, [1, 2, 3, 4, 5, 0x10, 0x13, 0x37]))) #print(hex(calc_crc16(0xfeef, [1, 2, 3, 4, 5, 0x10, 0x13, 0x37]))) - class DeviceInitException(Exception): pass @@ -256,7 +255,9 @@ class Channel(PacketSink): self._logger.debug("receiver thread is exiting: " + traceback.format_exc()) finally: self._channel_broken.set() - threading.Thread(target=receiver_thread, daemon=True).start() + t = threading.Thread(target=receiver_thread) + t.daemon = True + t.start() def remote_endpoint_operation(self, endpoint_id, input, expect_ack, output_length): if input is None: diff --git a/Firmware/fibre/python/fibre/serial_transport.py b/Firmware/fibre/python/fibre/serial_transport.py index b8141633..931a9415 100644 --- a/Firmware/fibre/python/fibre/serial_transport.py +++ b/Firmware/fibre/python/fibre/serial_transport.py @@ -10,6 +10,7 @@ import traceback import serial import serial.tools.list_ports import fibre +from fibre.utils import TimeoutError # TODO: make this customizable DEFAULT_BAUDRATE = 115200 diff --git a/Firmware/fibre/python/fibre/tcp_transport.py b/Firmware/fibre/python/fibre/tcp_transport.py index 5d7ee3ef..5cfb1692 100644 --- a/Firmware/fibre/python/fibre/tcp_transport.py +++ b/Firmware/fibre/python/fibre/tcp_transport.py @@ -4,7 +4,7 @@ import socket import time import traceback import fibre.protocol -from fibre.utils import wait_any +from fibre.utils import wait_any, TimeoutError def noprint(x): pass diff --git a/Firmware/fibre/python/fibre/usbbulk_transport.py b/Firmware/fibre/python/fibre/usbbulk_transport.py index c724e55a..dd32b106 100644 --- a/Firmware/fibre/python/fibre/usbbulk_transport.py +++ b/Firmware/fibre/python/fibre/usbbulk_transport.py @@ -8,6 +8,7 @@ import time import fibre.protocol import traceback import platform +from fibre.utils import TimeoutError # Currently we identify fibre-enabled devices by VID,PID # TODO: identify by USB descriptors diff --git a/Firmware/fibre/python/fibre/utils.py b/Firmware/fibre/python/fibre/utils.py index 511bd436..d0bbe946 100644 --- a/Firmware/fibre/python/fibre/utils.py +++ b/Firmware/fibre/python/fibre/utils.py @@ -12,10 +12,16 @@ try: # TODO: we should win32console anyway so we could just omit colorama import colorama colorama.init() -except ModuleNotFoundError: +except ImportError: print("Could not init terminal features.") sys.stdout.flush() pass + +if sys.version_info < (3, 3): + class TimeoutError(Exception): + pass +else: + TimeoutError = TimeoutError def get_serial_number_str(device): if hasattr(device, 'serial_number'): @@ -92,7 +98,10 @@ class Event(): def delayed_trigger(): if not self.wait(timeout=timeout): self.set() - threading.Thread(target=delayed_trigger, daemon=True).start() + threading.Thread(target=delayed_trigger) + t.daemon = True + t.start() + def wait_any(timeout=None, *events): """ diff --git a/tools/odrive/dfu.py b/tools/odrive/dfu.py index cf81fba6..0b0a55be 100755 --- a/tools/odrive/dfu.py +++ b/tools/odrive/dfu.py @@ -206,7 +206,7 @@ def show_deferred_message(message, cancellation_token): time.sleep(1) if not cancellation_token.is_set(): print(message) - t = threading.Thread(target=show_message_thread, args=(message, cancellation_token), daemon=True) + t = threading.Thread(target=show_message_thread, args=(message, cancellation_token)) t.daemon = True t.start() @@ -436,7 +436,10 @@ def launch_dfu(args, logger, cancellation_token): def find_device_in_dfu_mode_thread(): devices[0] = find_device_in_dfu_mode(serial_number, find_odrive_cancellation_token) find_odrive_cancellation_token.set() - threading.Thread(target=find_device_in_dfu_mode_thread, daemon=True).start() + t = threading.Thread(target=find_device_in_dfu_mode_thread) + t.daemon = True + t.start() + # Scan for ODrives not in DFU mode # We only scan on USB because DFU is only implemented over USB diff --git a/tools/odrive/utils.py b/tools/odrive/utils.py index a69c9ca1..dc3479f3 100755 --- a/tools/odrive/utils.py +++ b/tools/odrive/utils.py @@ -1,3 +1,4 @@ +from __future__ import print_function import sys import time @@ -12,7 +13,7 @@ try: import win32console import colorama colorama.init() -except ModuleNotFoundError: +except ImportError: print("Could not init terminal features.") print("Refer to install instructions at http://docs.odriverobotics.com/#downloading-and-installing-tools") sys.stdout.flush() @@ -72,8 +73,14 @@ def start_liveplotter(get_var_callback): fig.canvas.draw() fig.canvas.start_event_loop(1/plot_rate) - threading.Thread(target=fetch_data, daemon=True).start() - threading.Thread(target=plot_data, daemon=True).start() + fetch_t = threading.Thread(target=fetch_data) + fetch_t.daemon = True + fetch_t.start() + + plot_t = threading.Thread(target=plot_data) + plot_t.daemon = True + plot_t.start() + return cancellation_token; #plot_data() diff --git a/tools/run_tests.py b/tools/run_tests.py index 2e1359d6..357ff0bc 100755 --- a/tools/run_tests.py +++ b/tools/run_tests.py @@ -35,7 +35,8 @@ def for_all_parallel(objects, get_name, callback): # Start a thread for each element in the list all_threads = [] for element in objects: - thread = threading.Thread(target=run_callback, args=(element,), daemon=True) + thread = threading.Thread(target=run_callback, args=(element,)) + thread.daemon = True thread.start() all_threads.append(thread) diff --git a/tools/setup.py b/tools/setup.py index fcdea0bd..b2cfeb74 100644 --- a/tools/setup.py +++ b/tools/setup.py @@ -48,6 +48,10 @@ from setuptools import setup import os import sys +if sys.version_info < (3, 3): + import exceptions + PermissionError = exceptions.OSError + creating_package = "sdist" in sys.argv # Load version from Git tag @@ -78,7 +82,10 @@ if creating_package: fibre_link = os.path.join(os.path.dirname( os.path.realpath(__file__)), "fibre") if not os.path.exists(fibre_link): - os.symlink(fibre_src, fibre_link, True) + if sys.version_info > (3, 3): + os.symlink(fibre_src, fibre_link, target_is_directory=True) + else: + os.symlink(fibre_src, fibre_link) # TODO: find a better place for this if not creating_package: