The Protobuf message definitions for the Meshtastic project (used by apps and the device firmware).
import importlib
import os
import sys
# IMPORTANT: Indicate the exact path to the folder where the folder `meshtastic` with *_pb2.py lies
# Example for your structure:
PB2_PARENT = r"C:\my_github\LoRa\protobufs\python_gen"
# ------------- Подготовка импорта локальных pb2 -------------
if not os.path.isdir(PB2_PARENT):
raise SystemExit(f"PB2_PARENT was not found: {PB2_PARENT}. Generate PB2 and put the Meshtastic folder there.")
# вставляем в начало sys.path — чтобы локальные pb2 перекрывали site-packages
if PB2_PARENT not in sys.path:
sys.path.insert(0, PB2_PARENT)
print(f"[i] PB2 parent: {PB2_PARENT} (inserted into sys.path)")
# List of candidate modules for finding the desired types (local)
CANDIDATE_MODULES = [
"meshtastic.meshtastic_pb2",
"meshtastic.protobuf.meshtastic_pb2",
"meshtastic.admin_pb2",
"meshtastic.protobuf.admin_pb2",
"meshtastic.protobuf.channel_pb2",
"meshtastic.channel_pb2",
"meshtastic.portnums_pb2",
"meshtastic.protobuf.portnums_pb2",
"meshtastic.mqtt_pb2",
"meshtastic.protobuf.mqtt_pb2",
"meshtastic_pb2",
"admin_pb2",
"channel_pb2",
"portnums_pb2",
]
_imported = {}
def try_import(name):
if name in _imported:
return _imported[name]
try:
m = importlib.import_module(name)
_imported[name] = m
print(f"[i] imported {name}")
return m
except Exception:
_imported[name] = None
return None
# I import all the candidates (faster than searching for attributes later)
for nm in CANDIDATE_MODULES:
try_import(nm)