From 5ce386d897949d4a3f6c93b0339e62489e8da27b Mon Sep 17 00:00:00 2001 From: Rik Bouwmeester Date: Thu, 12 Mar 2026 15:24:19 +0100 Subject: [PATCH] Refactor cflib2 package: split flat re-exports into dedicated submodules Move exception types, TOC cache classes, and subsystem types out of cflib2/__init__.py into dedicated submodules (cflib2.error, cflib2.toc_cache, cflib2.commander, cflib2.log, cflib2.param, cflib2.memory, cflib2.localization, cflib2.platform, cflib2.console, cflib2.high_level_commander). The top-level package now only exports Crazyflie and LinkContext. Relocate trajectory data types (Poly, Poly4D, CompressedStart, CompressedSegment) to memory.py. Update example imports accordingly. --- cflib2/__init__.py | 49 +-------------------- cflib2/{trajectory.py => commander.py} | 8 ++-- cflib2/console.py | 27 ++++++++++++ cflib2/error.py | 59 ++++++++++++++++++++++++++ cflib2/high_level_commander.py | 27 ++++++++++++ cflib2/localization.py | 43 +++++++++++++++++++ cflib2/log.py | 27 ++++++++++++ cflib2/memory.py | 27 ++++++++++++ cflib2/param.py | 27 ++++++++++++ cflib2/platform.py | 27 ++++++++++++ cflib2/toc_cache.py | 27 ++++++++++++ examples/swarm.py | 3 +- examples/toc_cache.py | 3 +- examples/trajectory.py | 2 +- tests/test_exceptions.py | 8 ++-- 15 files changed, 307 insertions(+), 57 deletions(-) rename cflib2/{trajectory.py => commander.py} (82%) create mode 100644 cflib2/console.py create mode 100644 cflib2/error.py create mode 100644 cflib2/high_level_commander.py create mode 100644 cflib2/localization.py create mode 100644 cflib2/log.py create mode 100644 cflib2/memory.py create mode 100644 cflib2/param.py create mode 100644 cflib2/platform.py create mode 100644 cflib2/toc_cache.py diff --git a/cflib2/__init__.py b/cflib2/__init__.py index ec86bea..5fa176a 100644 --- a/cflib2/__init__.py +++ b/cflib2/__init__.py @@ -22,51 +22,6 @@ # along with this program. If not, see . """Crazyflie Python Library""" -from cflib2._rust import ( - Crazyflie, - LinkContext, - # TOC cache classes (passed to Crazyflie.connect_from_uri) - NoTocCache, - InMemoryTocCache, - FileTocCache, - # Exceptions - CrazyflieError, - ProtocolVersionNotSupportedError, - ProtocolError, - ParamError, - LogError, - ConversionError, - LinkError, - DisconnectedError, - VariableNotFoundError, - SystemError, - AppchannelPacketTooLargeError, - InvalidArgumentError, - TimeoutError, - MemoryError, - InvalidParameterError, -) +from cflib2._rust import Crazyflie, LinkContext -__all__ = [ - "Crazyflie", - "LinkContext", - "NoTocCache", - "InMemoryTocCache", - "FileTocCache", - # Exceptions - "CrazyflieError", - "ProtocolVersionNotSupportedError", - "ProtocolError", - "ParamError", - "LogError", - "ConversionError", - "LinkError", - "DisconnectedError", - "VariableNotFoundError", - "SystemError", - "AppchannelPacketTooLargeError", - "InvalidArgumentError", - "TimeoutError", - "MemoryError", - "InvalidParameterError", -] +__all__ = ["Crazyflie", "LinkContext"] diff --git a/cflib2/trajectory.py b/cflib2/commander.py similarity index 82% rename from cflib2/trajectory.py rename to cflib2/commander.py index cc13cf8..03d06b2 100644 --- a/cflib2/trajectory.py +++ b/cflib2/commander.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- +# # ,---------, ____ _ __ # | ,-^-, | / __ )(_) /_______________ _____ ___ # | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ @@ -18,8 +20,8 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -"""Trajectory building primitives for the Crazyflie""" +"""Commander subsystem types""" -from cflib2._rust import Poly, Poly4D, CompressedStart, CompressedSegment +from cflib2._rust import Commander -__all__ = ["Poly", "Poly4D", "CompressedStart", "CompressedSegment"] +__all__ = ["Commander"] diff --git a/cflib2/console.py b/cflib2/console.py new file mode 100644 index 0000000..59f7781 --- /dev/null +++ b/cflib2/console.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# +# ,---------, ____ _ __ +# | ,-^-, | / __ )(_) /_______________ _____ ___ +# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ +# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ +# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ +# +# Copyright (C) 2025 Bitcraze AB +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +"""Console subsystem types""" + +from cflib2._rust import Console + +__all__ = ["Console"] diff --git a/cflib2/error.py b/cflib2/error.py new file mode 100644 index 0000000..0706496 --- /dev/null +++ b/cflib2/error.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +# +# ,---------, ____ _ __ +# | ,-^-, | / __ )(_) /_______________ _____ ___ +# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ +# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ +# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ +# +# Copyright (C) 2025 Bitcraze AB +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +"""Crazyflie exception types""" + +from cflib2._rust import ( + AppchannelPacketTooLargeError, + ConversionError, + CrazyflieError, + DisconnectedError, + InvalidArgumentError, + InvalidParameterError, + LinkError, + LogError, + MemoryError, + ParamError, + ProtocolError, + ProtocolVersionNotSupportedError, + SystemError, + TimeoutError, + VariableNotFoundError, +) + +__all__ = [ + "AppchannelPacketTooLargeError", + "ConversionError", + "CrazyflieError", + "DisconnectedError", + "InvalidArgumentError", + "InvalidParameterError", + "LinkError", + "LogError", + "MemoryError", + "ParamError", + "ProtocolError", + "ProtocolVersionNotSupportedError", + "SystemError", + "TimeoutError", + "VariableNotFoundError", +] diff --git a/cflib2/high_level_commander.py b/cflib2/high_level_commander.py new file mode 100644 index 0000000..25462e7 --- /dev/null +++ b/cflib2/high_level_commander.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# +# ,---------, ____ _ __ +# | ,-^-, | / __ )(_) /_______________ _____ ___ +# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ +# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ +# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ +# +# Copyright (C) 2025 Bitcraze AB +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +"""High-level commander subsystem types""" + +from cflib2._rust import HighLevelCommander + +__all__ = ["HighLevelCommander"] diff --git a/cflib2/localization.py b/cflib2/localization.py new file mode 100644 index 0000000..a8a41d3 --- /dev/null +++ b/cflib2/localization.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# +# ,---------, ____ _ __ +# | ,-^-, | / __ )(_) /_______________ _____ ___ +# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ +# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ +# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ +# +# Copyright (C) 2025 Bitcraze AB +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +"""Localization subsystem types""" + +from cflib2._rust import ( + EmergencyControl, + ExternalPose, + Lighthouse, + LighthouseAngleData, + LighthouseAngles, + Localization, + LocoPositioning, +) + +__all__ = [ + "EmergencyControl", + "ExternalPose", + "Lighthouse", + "LighthouseAngleData", + "LighthouseAngles", + "Localization", + "LocoPositioning", +] diff --git a/cflib2/log.py b/cflib2/log.py new file mode 100644 index 0000000..c30100f --- /dev/null +++ b/cflib2/log.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# +# ,---------, ____ _ __ +# | ,-^-, | / __ )(_) /_______________ _____ ___ +# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ +# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ +# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ +# +# Copyright (C) 2025 Bitcraze AB +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +"""Log subsystem types""" + +from cflib2._rust import Log, LogBlock, LogData, LogStream + +__all__ = ["Log", "LogBlock", "LogData", "LogStream"] diff --git a/cflib2/memory.py b/cflib2/memory.py new file mode 100644 index 0000000..5d4b1a4 --- /dev/null +++ b/cflib2/memory.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# +# ,---------, ____ _ __ +# | ,-^-, | / __ )(_) /_______________ _____ ___ +# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ +# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ +# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ +# +# Copyright (C) 2025 Bitcraze AB +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +"""Memory subsystem types""" + +from cflib2._rust import CompressedSegment, CompressedStart, Memory, Poly, Poly4D + +__all__ = ["CompressedSegment", "CompressedStart", "Memory", "Poly", "Poly4D"] diff --git a/cflib2/param.py b/cflib2/param.py new file mode 100644 index 0000000..e7f0874 --- /dev/null +++ b/cflib2/param.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# +# ,---------, ____ _ __ +# | ,-^-, | / __ )(_) /_______________ _____ ___ +# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ +# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ +# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ +# +# Copyright (C) 2025 Bitcraze AB +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +"""Parameter subsystem types""" + +from cflib2._rust import Param, PersistentParamState + +__all__ = ["Param", "PersistentParamState"] diff --git a/cflib2/platform.py b/cflib2/platform.py new file mode 100644 index 0000000..6e1fa33 --- /dev/null +++ b/cflib2/platform.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# +# ,---------, ____ _ __ +# | ,-^-, | / __ )(_) /_______________ _____ ___ +# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ +# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ +# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ +# +# Copyright (C) 2025 Bitcraze AB +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +"""Platform subsystem types""" + +from cflib2._rust import AppChannel, Platform + +__all__ = ["AppChannel", "Platform"] diff --git a/cflib2/toc_cache.py b/cflib2/toc_cache.py new file mode 100644 index 0000000..88b0585 --- /dev/null +++ b/cflib2/toc_cache.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# +# ,---------, ____ _ __ +# | ,-^-, | / __ )(_) /_______________ _____ ___ +# | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \ +# | / ,--' | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ +# +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/ +# +# Copyright (C) 2025 Bitcraze AB +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +"""TOC cache types""" + +from cflib2._rust import FileTocCache, InMemoryTocCache, NoTocCache + +__all__ = ["FileTocCache", "InMemoryTocCache", "NoTocCache"] diff --git a/examples/swarm.py b/examples/swarm.py index b705bd7..20e7c7b 100644 --- a/examples/swarm.py +++ b/examples/swarm.py @@ -40,7 +40,8 @@ import tyro -from cflib2 import Crazyflie, LinkContext, FileTocCache, NoTocCache +from cflib2 import Crazyflie, LinkContext +from cflib2.toc_cache import FileTocCache, NoTocCache @dataclass diff --git a/examples/toc_cache.py b/examples/toc_cache.py index 5515cfc..6c5a93a 100644 --- a/examples/toc_cache.py +++ b/examples/toc_cache.py @@ -49,7 +49,8 @@ import tyro -from cflib2 import Crazyflie, LinkContext, NoTocCache, InMemoryTocCache, FileTocCache +from cflib2 import Crazyflie, LinkContext +from cflib2.toc_cache import FileTocCache, InMemoryTocCache, NoTocCache @dataclass diff --git a/examples/trajectory.py b/examples/trajectory.py index fa75f78..5c4b18d 100644 --- a/examples/trajectory.py +++ b/examples/trajectory.py @@ -41,7 +41,7 @@ import tyro from cflib2 import Crazyflie, LinkContext -from cflib2.trajectory import Poly, Poly4D +from cflib2.memory import Poly, Poly4D # The trajectory to fly # See https://github.com/whoenig/uav_trajectories for a tool to generate diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index bf861c9..54be729 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -20,10 +20,10 @@ # along with this program. If not, see . import pytest -import cflib2 +from cflib2 import error -EXCEPTION_NAMES = [name for name in cflib2.__all__ if name.endswith("Error")] +EXCEPTION_NAMES = [name for name in error.__all__ if name.endswith("Error")] class TestExceptionHierarchy: @@ -31,5 +31,5 @@ class TestExceptionHierarchy: @pytest.mark.parametrize("name", EXCEPTION_NAMES) def test_exception_is_subclass_of_crazyflie_error(self, name: str) -> None: - exc_class = getattr(cflib2, name) - assert issubclass(exc_class, cflib2.CrazyflieError) + exc_class = getattr(error, name) + assert issubclass(exc_class, error.CrazyflieError)