Skip to content

Commit 88b7361

Browse files
committed
Move protobufs to meshtastic.protobuf python namespace
1 parent 0b9af0d commit 88b7361

14 files changed

Lines changed: 65 additions & 26 deletions

.github/workflows/update_protobufs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ jobs:
3737
git config --global user.email 'bot@noreply.github.com'
3838
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
3939
git add protobufs
40-
git add meshtastic
40+
git add meshtastic/protobuf
4141
git commit -m "Update protobuf submodule" && git push || echo "No changes to commit"

bin/regen-protobufs.sh

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,53 @@
11
#!/bin/bash
22

3+
set -e
4+
35
#Uncomment to run hack
46
#gsed -i 's/import "\//import ".\//g' ./protobufs/meshtastic/*
57
#gsed -i 's/package meshtastic;//g' ./protobufs/meshtastic/*
68

79
# protoc looks for mypy plugin in the python path
810
source $(poetry env info --path)/bin/activate
911

10-
./nanopb-0.4.8/generator-bin/protoc -I=protobufs --python_out ./ --mypy_out ./ ./protobufs/meshtastic/*.proto
11-
./nanopb-0.4.8/generator-bin/protoc -I=protobufs --python_out ./meshtastic/ --mypy_out ./meshtastic/ ./protobufs/nanopb.proto
12+
# Put our temp files in the poetry build directory
13+
TMPDIR=./build/meshtastic/protofixup
14+
echo "Fixing up protobuf paths in ${TMPDIR} temp directory"
15+
16+
17+
# Ensure a clean build
18+
rm -r "${TMPDIR}"
1219

13-
# workaround for import bug in protoc https://github.com/protocolbuffers/protobuf/issues/1491#issuecomment-690618628
20+
INDIR=${TMPDIR}/in/meshtastic/protobuf
21+
OUTDIR=${TMPDIR}/out
22+
PYIDIR=${TMPDIR}/out
23+
mkdir -p "${OUTDIR}" "${INDIR}" "${PYIDIR}"
24+
cp ./protobufs/meshtastic/*.proto "${INDIR}"
1425

26+
# OS-X sed is apparently a little different and expects an arg for -i
1527
if [[ $OSTYPE == 'darwin'* ]]; then
16-
sed -i '' -E 's/^(import.*_pb2)/from . \1/' meshtastic/*.py
17-
# automate the current workaround (may be related to Meshtastic-protobufs issue #27 https://github.com/meshtastic/protobufs/issues/27)
18-
sed -i '' -E "s/^None = 0/globals()['None'] = 0/" meshtastic/mesh_pb2.py
28+
SEDCMD="sed -i '' -E"
1929
else
20-
sed -i -e 's/^import.*_pb2/from . \0/' meshtastic/*.py
21-
# automate the current workaround (may be related to Meshtastic-protobufs issue #27 https://github.com/meshtastic/protobufs/issues/27)
22-
sed -i -e "s/^None = 0/globals()['None'] = 0/" meshtastic/mesh_pb2.py
30+
SEDCMD="sed -i -E"
2331
fi
32+
33+
34+
# change the package names to meshtastic.protobuf
35+
$SEDCMD 's/^package meshtastic;/package meshtastic.protobuf;/' "${INDIR}/"*.proto
36+
# fix the imports to match
37+
$SEDCMD 's/^import "meshtastic\//import "meshtastic\/protobuf\//' "${INDIR}/"*.proto
38+
39+
# Generate the python files
40+
./nanopb-0.4.8/generator-bin/protoc -I=$TMPDIR/in --python_out "${OUTDIR}" "--mypy_out=${PYIDIR}" $INDIR/*.proto
41+
42+
# Change "from meshtastic.protobuf import" to "from . import"
43+
$SEDCMD 's/^from meshtastic.protobuf import/from . import/' "${OUTDIR}"/meshtastic/protobuf/*pb2*.py[i]
44+
45+
# Create a __init__.py in the out directory
46+
touch "${OUTDIR}/meshtastic/protobuf/__init__.py"
47+
48+
# Copy to the source controlled tree
49+
mkdir -p meshtastic/protobuf
50+
rm -rf meshtastic/protobuf/*pb2*.py
51+
cp "${OUTDIR}/meshtastic/protobuf"/* meshtastic/protobuf
52+
53+
exit 0

meshtastic/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect
8181
from pubsub import pub # type: ignore[import-untyped]
8282
from tabulate import tabulate
8383

84-
from meshtastic import (
84+
from meshtastic.node import Node
85+
from meshtastic.util import DeferredExecution, Timeout, catchAndIgnore, fixme, stripnl
86+
87+
from .protobuf import (
8588
admin_pb2,
8689
apponly_pb2,
8790
channel_pb2,
@@ -93,10 +96,10 @@ def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect
9396
remote_hardware_pb2,
9497
storeforward_pb2,
9598
telemetry_pb2,
99+
)
100+
from . import (
96101
util,
97102
)
98-
from meshtastic.node import Node
99-
from meshtastic.util import DeferredExecution, Timeout, catchAndIgnore, fixme, stripnl
100103

101104
# Note: To follow PEP224, comments should be after the module variable.
102105

meshtastic/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import meshtastic.test
1818
import meshtastic.util
1919
from meshtastic import mt_config
20-
from meshtastic import channel_pb2, config_pb2, portnums_pb2, remote_hardware, BROADCAST_ADDR
20+
from meshtastic.protobuf import channel_pb2, config_pb2, portnums_pb2
21+
from meshtastic import remote_hardware, BROADCAST_ADDR
2122
from meshtastic.version import get_active_version
2223
from meshtastic.ble_interface import BLEInterface
2324
from meshtastic.mesh_interface import MeshInterface

meshtastic/mesh_interface.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
from tabulate import tabulate
1919

2020
import meshtastic.node
21-
from meshtastic import (
21+
22+
from meshtastic.protobuf import (
2223
mesh_pb2,
2324
portnums_pb2,
2425
telemetry_pb2,
26+
)
27+
from meshtastic import (
2528
BROADCAST_ADDR,
2629
BROADCAST_NUM,
2730
LOCAL_ADDR,

meshtastic/node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from typing import Union
99

10-
from meshtastic import admin_pb2, apponly_pb2, channel_pb2, localonly_pb2, mesh_pb2, portnums_pb2
10+
from meshtastic.protobuf import admin_pb2, apponly_pb2, channel_pb2, localonly_pb2, mesh_pb2, portnums_pb2
1111
from meshtastic.util import (
1212
Timeout,
1313
camel_to_snake,

meshtastic/remote_hardware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from pubsub import pub # type: ignore[import-untyped]
66

7-
from meshtastic import portnums_pb2, remote_hardware_pb2
7+
from meshtastic.protobuf import portnums_pb2, remote_hardware_pb2
88
from meshtastic.util import our_exit
99

1010

meshtastic/tests/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
)
2222
from meshtastic import mt_config
2323

24-
from ..channel_pb2 import Channel # pylint: disable=E0611
24+
from ..protobuf.channel_pb2 import Channel # pylint: disable=E0611
2525

2626
# from ..ble_interface import BLEInterface
2727
from ..node import Node

meshtastic/tests/test_mesh_interface.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import pytest
88
from hypothesis import given, strategies as st
99

10-
from .. import mesh_pb2, config_pb2, BROADCAST_ADDR, LOCAL_ADDR
10+
from ..protobuf import mesh_pb2, config_pb2
11+
from .. import BROADCAST_ADDR, LOCAL_ADDR
1112
from ..mesh_interface import MeshInterface, _timeago
1213
from ..node import Node
1314

meshtastic/tests/test_node.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
import pytest
88

9-
from .. import localonly_pb2, config_pb2
10-
from ..channel_pb2 import Channel # pylint: disable=E0611
9+
from ..protobuf import localonly_pb2, config_pb2
10+
from ..protobuf.channel_pb2 import Channel # pylint: disable=E0611
1111
from ..node import Node
1212
from ..serial_interface import SerialInterface
1313
from ..mesh_interface import MeshInterface

0 commit comments

Comments
 (0)