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
15 changes: 15 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[flake8]
exclude =
.git,
__pycache__,
build,
dist,
*.egg-info,
venv,
py37_env,
*/dsdl_specs/*,
.eggs,
.tox,
docs/conf.py
max-line-length = 127
max-complexity = 10
20 changes: 17 additions & 3 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,28 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.7, 3.8, 3.9, '3.10']
python-version: ['3.7', 3.8, 3.9, '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v2

- name: Set up Python 3.7 from deadsnakes
if: matrix.python-version == '3.7'
run: |
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install -y python3.7 python3.7-dev python3.7-distutils
python3.7 -m venv py37_env --without-pip
source py37_env/bin/activate
curl -sS https://bootstrap.pypa.io/pip/3.7/get-pip.py | python
echo "PATH=$PWD/py37_env/bin:$PATH" >> $GITHUB_ENV

- name: Set up Python ${{ matrix.python-version }}
if: matrix.python-version != '3.7'
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand All @@ -33,8 +47,8 @@ jobs:
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
# exit-zero treats all errors as warnings
flake8 . --count --exit-zero --statistics

- name: Download DSDL Specifications
run: |
Expand Down
39 changes: 34 additions & 5 deletions dronecan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,35 @@
import os
import sys
import struct
import pkg_resources
import time
from logging import getLogger

# Remove deprecated pkg_resources
try:
# Use importlib.resources if available (Python 3.7+)
try:
from importlib import resources as importlib_resources
except ImportError:
# Fall back to importlib_resources backport for Python < 3.7
import importlib_resources

def get_resource_path(package, resource):
"""Get path to a resource file using importlib.resources"""
try:
# Python 3.9+
return importlib_resources.files(package).joinpath(resource)
except AttributeError:
# Python 3.7, 3.8 with importlib_resources backport
with importlib_resources.path(package, resource) as path:
return path
except ImportError:
# Last resort fallback to pkg_resources
import pkg_resources

def get_resource_path(package, resource):
"""Get path to a resource file using pkg_resources"""
return pkg_resources.resource_filename(package, resource)

try:
# noinspection PyStatementEffect
time.monotonic # Works natively in Python 3.3+
Expand Down Expand Up @@ -124,15 +149,19 @@ def load_dsdl(*paths, **args):
# noinspection PyBroadException
try:
if not args.get("exclude_dist", None):
dsdl_path = pkg_resources.resource_filename(__name__, "dsdl_specs") # @UndefinedVariable
# check if we are a package, if not directly use relative DSDL path
if not os.path.exists(dsdl_path):
try:
dsdl_path = str(get_resource_path(__name__, "dsdl_specs"))
except (ImportError, FileNotFoundError):
dsdl_path = None

# Check if we are a package, if not directly use relative DSDL path
if not dsdl_path or not os.path.exists(dsdl_path):
DSDL_paths = [ "../../DSDL", "../../../../../DroneCAN/DSDL", "../../../../dsdl"]
for p in DSDL_paths:
dpath = os.path.join(os.path.dirname(__file__), p)
if os.path.exists(dpath):
dsdl_path = dpath
logger.debug('Found DSDL at: '.format(dsdl_path))
logger.debug('Found DSDL at: {}'.format(dsdl_path))
break
if not os.path.exists(dsdl_path):
raise UAVCANException('failed to find DSDL path')
Expand Down