Skip to content

Commit

Permalink
Python 2.7 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Marshall committed Jul 23, 2018
1 parent 3e1cfca commit b8d0337
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Firmware/fibre/python/fibre/__init__.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 9 additions & 8 deletions Firmware/fibre/python/fibre/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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

Expand All @@ -20,25 +20,25 @@
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):
Expand Down Expand Up @@ -102,9 +102,10 @@ def did_discover_channel(channel):
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))

Expand Down
7 changes: 4 additions & 3 deletions Firmware/fibre/python/fibre/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -256,7 +255,9 @@ def receiver_thread():
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:
Expand Down
1 change: 1 addition & 0 deletions Firmware/fibre/python/fibre/serial_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import serial
import serial.tools.list_ports
import fibre
from fibre.utils import TimeoutError

# TODO: make this customizable
DEFAULT_BAUDRATE = 115200
Expand Down
2 changes: 1 addition & 1 deletion Firmware/fibre/python/fibre/tcp_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
import traceback
import fibre.protocol
from fibre.utils import wait_any
from fibre.utils import wait_any, TimeoutError

def noprint(x):
pass
Expand Down
1 change: 1 addition & 0 deletions Firmware/fibre/python/fibre/usbbulk_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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
Expand Down
13 changes: 11 additions & 2 deletions Firmware/fibre/python/fibre/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
# 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'):
Expand Down Expand Up @@ -92,7 +98,10 @@ def trigger_after(self, timeout):
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):
"""
Expand Down
7 changes: 5 additions & 2 deletions tools/odrive/dfu.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def show_message_thread(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()

Expand Down Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions tools/odrive/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function

import sys
import time
Expand All @@ -12,7 +13,7 @@
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()
Expand Down Expand Up @@ -72,8 +73,14 @@ def did_close(evt):
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()
Expand Down
3 changes: 2 additions & 1 deletion tools/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def run_callback(element):
# 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)

Expand Down
9 changes: 8 additions & 1 deletion tools/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
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
Expand Down Expand Up @@ -78,7 +82,10 @@
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:
Expand Down

0 comments on commit b8d0337

Please sign in to comment.