Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions emulator/brainflow_emulator/cyton_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import time

import pkg_resources
from importlib.resources import files
from brainflow_emulator.emulate_common import TestFailureError, Listener, log_multilines
from serial import Serial

Expand All @@ -18,7 +18,15 @@ def read(port, num_bytes):


def get_isntaller():
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))
try:
resource = files(__name__).joinpath('com0com').joinpath('setup_com0com_W7_x64_signed.exe')
return str(resource)
except (TypeError, AttributeError):
# Fallback for:
# 1. Python < 3.9 (importlib.resources.files not available)
# 2. NixOS/packaging edge cases where importlib.resources may not work
import pkg_resources
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))
Comment on lines 24 to 29

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these except blocks trying to accomplish? Are they for backwards compatibility with older versions of python? If so, I'd suggest adding a comment to indicate that they exist for that reason, and I'd like to note that importlib has existed in python since 3.1. If not, you should definitely add a comment explaining why. Regardless, it's probably also worth emitting a warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Pandapip1 Thank you for the detailed review and suggestion.
Yes, exactly- except (TypeError, AttributeError) blocks provide backward compatibility.
The TypeError catches cases where importlib.resources.files() returns something unexpected, and AttributeError catches when the method doesn't exist (Python < 3.9).
While importlib.resources has existed since Python 3.1, the files() API wasn't introduced until Python 3.9. This is the modern, recommended way to access package resources.

I have added comment.



def install_com0com():
Expand Down
12 changes: 10 additions & 2 deletions emulator/brainflow_emulator/freeeeg32_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import time

import pkg_resources
from importlib.resources import files
from brainflow_emulator.emulate_common import TestFailureError, log_multilines
from brainflow_emulator.freeeeg32_emulator import Listener
from serial import Serial
Expand All @@ -19,7 +19,15 @@ def read(port, num_bytes):


def get_isntaller():
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))
try:
resource = files(__name__).joinpath('com0com').joinpath('setup_com0com_W7_x64_signed.exe')
return str(resource)
except (TypeError, AttributeError):
# Fallback for:
# 1. Python < 3.9 (importlib.resources.files not available)
# 2. NixOS/packaging edge cases where importlib.resources may not work
import pkg_resources
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))


def install_com0com():
Expand Down
12 changes: 10 additions & 2 deletions emulator/brainflow_emulator/knightboard_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import time

import pkg_resources
from importlib.resources import files
from brainflow_emulator.emulate_common import TestFailureError, log_multilines
from brainflow_emulator.knightboard_emulator import Listener
from serial import Serial
Expand All @@ -19,7 +19,15 @@ def read(port, num_bytes):


def get_isntaller():
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))
try:
resource = files(__name__).joinpath('com0com').joinpath('setup_com0com_W7_x64_signed.exe')
return str(resource)
except (TypeError, AttributeError):
# Fallback for:
# 1. Python < 3.9 (importlib.resources.files not available)
# 2. NixOS/packaging edge cases where importlib.resources may not work
import pkg_resources
return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe'))


def install_com0com():
Expand Down
14 changes: 11 additions & 3 deletions python_package/brainflow/board_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import List

import numpy
import pkg_resources
from importlib.resources import files
from brainflow.exit_codes import BrainFlowExitCodes, BrainFlowError
from brainflow.utils import LogLevels
from numpy.ctypeslib import ndpointer
Expand All @@ -21,7 +21,7 @@ class BoardIds(enum.IntEnum):
STREAMING_BOARD = -2 #:
SYNTHETIC_BOARD = -1 #:
CYTON_BOARD = 0 #:
GANGLION_BOARD = 1 #:
GANGLION_BOARD = 1 #:
CYTON_DAISY_BOARD = 2 #:
GALEA_BOARD = 3 #:
GANGLION_WIFI_BOARD = 4 #:
Expand Down Expand Up @@ -173,7 +173,15 @@ def __init__(self):
dll_path = 'lib/libBoardController.dylib'
else:
dll_path = 'lib/libBoardController.so'
full_path = pkg_resources.resource_filename(__name__, dll_path)
try:
resource = files(__name__).joinpath(dll_path)
full_path = str(resource)
except (TypeError, AttributeError):
# Fallback for:
# 1. Python < 3.9 (importlib.resources.files not available)
# 2. NixOS/packaging edge cases where importlib.resources may not work
import pkg_resources
full_path = pkg_resources.resource_filename(__name__, dll_path)
if os.path.isfile(full_path):
dir_path = os.path.abspath(os.path.dirname(full_path))
# for python we load dll by direct path but this dll may depend on other dlls and they will not be found!
Expand Down
12 changes: 10 additions & 2 deletions python_package/brainflow/data_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import List, Tuple

import numpy
import pkg_resources
from importlib.resources import files
from brainflow.exit_codes import BrainFlowExitCodes, BrainFlowError
from brainflow.utils import check_memory_layout_row_major, LogLevels
from numpy.ctypeslib import ndpointer
Expand Down Expand Up @@ -153,7 +153,15 @@ def __init__(self):
dll_path = 'lib/libDataHandler.dylib'
else:
dll_path = 'lib/libDataHandler.so'
full_path = pkg_resources.resource_filename(__name__, dll_path)
try:
resource = files(__name__).joinpath(dll_path)
full_path = str(resource)
except (TypeError, AttributeError):
# Fallback for:
# 1. Python < 3.9 (importlib.resources.files not available)
# 2. NixOS/packaging edge cases where importlib.resources may not work
import pkg_resources
full_path = pkg_resources.resource_filename(__name__, dll_path)
if os.path.isfile(full_path):
dir_path = os.path.abspath(os.path.dirname(full_path))
# for python 3.8 PATH env var doesnt work anymore
Expand Down
12 changes: 10 additions & 2 deletions python_package/brainflow/ml_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import List

import numpy
import pkg_resources
from importlib.resources import files
from brainflow.board_shim import BrainFlowError, LogLevels
from brainflow.exit_codes import BrainFlowExitCodes
from numpy.ctypeslib import ndpointer
Expand Down Expand Up @@ -78,7 +78,15 @@ def __init__(self):
dll_path = 'lib/libMLModule.dylib'
else:
dll_path = 'lib/libMLModule.so'
full_path = pkg_resources.resource_filename(__name__, dll_path)
try:
resource = files(__name__).joinpath(dll_path)
full_path = str(resource)
except (TypeError, AttributeError):
# Fallback for:
# 1. Python < 3.9 (importlib.resources.files not available)
# 2. NixOS/packaging edge cases where importlib.resources may not work
import pkg_resources
full_path = pkg_resources.resource_filename(__name__, dll_path)
if os.path.isfile(full_path):
# for python we load dll by direct path but this dll may depend on other dlls and they will not be found!
# to solve it we can load all of them before loading the main one or change PATH\LD_LIBRARY_PATH env var.
Expand Down
Loading