Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mavutil: Add option to set dialect directly from file #934

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions mavutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,28 @@ def set_dialect(dialect, with_type_annotations=None):
current_dialect = dialect
mavlink = mod

def set_dialect_from_file(file):
'''set the MAVLink dialect to work with from a file.
For example, set_dialect_from_file("/home/user/my_dialects/v20/my_special_dialect.py")
'''
global mavlink, current_dialect
import importlib.util

dialect = os.path.splitext(os.path.basename(file))[0]

spec = importlib.util.spec_from_file_location(dialect, file)
mod = importlib.util.module_from_spec(spec)
sys.modules[dialect] = mod
try:
spec.loader.exec_module(mod)
except FileNotFoundError:
print('Could not find dialect file %s' % file)
print('Stay on dialect %s' % current_dialect)
else:
current_dialect = dialect
mavlink = mod
print('Set dialect %s' % current_dialect)

# Set the default dialect. This is done here as it needs to be after the function declaration
set_dialect(os.environ['MAVLINK_DIALECT'])

Expand Down