Skip to content

Commit a07e853

Browse files
committed
Refactor to remove pylint issues.
Since one of pylint's complains was that the globals module was shadowing the built-in, and since the name `config` was already is use in several modules, globals.py was renamed as mt_config.py. All tests now pass, and the only remaining local pylint errors relate to the protobuf code, I'm hoping this will make the PR valid.
1 parent 0d57449 commit a07e853

9 files changed

Lines changed: 291 additions & 290 deletions

File tree

meshtastic/__main__.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616

1717
import meshtastic.test
1818
import meshtastic.util
19-
from meshtastic import globals
19+
from meshtastic import mt_config
2020
from meshtastic import channel_pb2, config_pb2, portnums_pb2, remote_hardware, BROADCAST_ADDR
2121
from meshtastic.version import get_active_version
2222
from meshtastic.ble_interface import BLEInterface
2323

2424
def onReceive(packet, interface):
2525
"""Callback invoked when a packet arrives"""
26-
args = globals.args
26+
args = mt_config.args
2727
try:
2828
d = packet.get("decoded")
2929
logging.debug(f"in onReceive() d:{d}")
@@ -67,7 +67,7 @@ def getPref(node, comp_name):
6767
# Note: protobufs has the keys in snake_case, so snake internally
6868
snake_name = meshtastic.util.camel_to_snake(name[1])
6969
logging.debug(f"snake_name:{snake_name} camel_name:{camel_name}")
70-
logging.debug(f"use camel:{globals.camel_case}")
70+
logging.debug(f"use camel:{mt_config.camel_case}")
7171

7272
# First validate the input
7373
localConfig = node.localConfig
@@ -84,7 +84,7 @@ def getPref(node, comp_name):
8484
break
8585

8686
if not found:
87-
if globals.camel_case:
87+
if mt_config.camel_case:
8888
print(
8989
f"{localConfig.__class__.__name__} and {moduleConfig.__class__.__name__} do not have an attribute {snake_name}."
9090
)
@@ -103,7 +103,7 @@ def getPref(node, comp_name):
103103
config_values = getattr(config, config_type.name)
104104
if not wholeField:
105105
pref_value = getattr(config_values, pref.name)
106-
if globals.camel_case:
106+
if mt_config.camel_case:
107107
print(f"{str(config_type.name)}.{camel_name}: {str(pref_value)}")
108108
logging.debug(
109109
f"{str(config_type.name)}.{camel_name}: {str(pref_value)}"
@@ -169,7 +169,7 @@ def setPref(config, comp_name, valStr) -> bool:
169169
if e:
170170
val = e.number
171171
else:
172-
if globals.camel_case:
172+
if mt_config.camel_case:
173173
print(
174174
f"{name[0]}.{camel_name} does not have an enum called {val}, so you can not set it."
175175
)
@@ -208,7 +208,7 @@ def setPref(config, comp_name, valStr) -> bool:
208208
config_type.message_type.ignore_incoming.extend([val])
209209

210210
prefix = f"{name[0]}." if config_type.message_type is not None else ""
211-
if globals.camel_case:
211+
if mt_config.camel_case:
212212
print(f"Set {prefix}{camel_name} to {valStr}")
213213
else:
214214
print(f"Set {prefix}{snake_name} to {valStr}")
@@ -223,7 +223,7 @@ def onConnected(interface):
223223
False # Should we wait for an acknowledgment if we send to a remote node?
224224
)
225225
try:
226-
args = globals.args
226+
args = mt_config.args
227227

228228
# do not print this line if we are exporting the config
229229
if not args.export_config:
@@ -474,7 +474,7 @@ def onConnected(interface):
474474
print("Writing modified preferences to device")
475475
node.writeConfig(field)
476476
else:
477-
if globals.camel_case:
477+
if mt_config.camel_case:
478478
print(
479479
f"{node.localConfig.__class__.__name__} and {node.moduleConfig.__class__.__name__} do not have an attribute {pref[0]}."
480480
)
@@ -587,7 +587,7 @@ def onConnected(interface):
587587
# handle changing channels
588588

589589
if args.ch_add:
590-
channelIndex = globals.channel_index
590+
channelIndex = mt_config.channel_index
591591
if channelIndex is not None:
592592
# Since we set the channel index after adding a channel, don't allow --ch-index
593593
meshtastic.util.our_exit(
@@ -618,12 +618,12 @@ def onConnected(interface):
618618
n.writeChannel(ch.index)
619619
if channelIndex is None:
620620
print(f"Setting newly-added channel's {ch.index} as '--ch-index' for further modifications")
621-
globals.channel_index = ch.index
621+
mt_config.channel_index = ch.index
622622

623623
if args.ch_del:
624624
closeNow = True
625625

626-
channelIndex = globals.channel_index
626+
channelIndex = mt_config.channel_index
627627
if channelIndex is None:
628628
meshtastic.util.our_exit(
629629
"Warning: Need to specify '--ch-index' for '--ch-del'.", 1
@@ -639,7 +639,7 @@ def onConnected(interface):
639639

640640
def setSimpleConfig(modem_preset):
641641
"""Set one of the simple modem_config"""
642-
channelIndex = globals.channel_index
642+
channelIndex = mt_config.channel_index
643643
if channelIndex is not None and channelIndex > 0:
644644
meshtastic.util.our_exit(
645645
"Warning: Cannot set modem preset for non-primary channel", 1
@@ -674,7 +674,7 @@ def setSimpleConfig(modem_preset):
674674
if args.ch_set or args.ch_enable or args.ch_disable:
675675
closeNow = True
676676

677-
channelIndex = globals.channel_index
677+
channelIndex = mt_config.channel_index
678678
if channelIndex is None:
679679
meshtastic.util.our_exit("Warning: Need to specify '--ch-index'.", 1)
680680
ch = interface.getNode(args.dest).channels[channelIndex]
@@ -829,7 +829,7 @@ def printConfig(config):
829829
names = []
830830
for field in config.message_type.fields:
831831
tmp_name = f"{config_section.name}.{field.name}"
832-
if globals.camel_case:
832+
if mt_config.camel_case:
833833
tmp_name = meshtastic.util.snake_to_camel(tmp_name)
834834
names.append(tmp_name)
835835
for temp_name in sorted(names):
@@ -874,7 +874,7 @@ def export_config(interface):
874874
if owner_short:
875875
configObj["owner_short"] = owner_short
876876
if channel_url:
877-
if globals.camel_case:
877+
if mt_config.camel_case:
878878
configObj["channelUrl"] = channel_url
879879
else:
880880
configObj["channel_url"] = channel_url
@@ -886,11 +886,11 @@ def export_config(interface):
886886
# Convert inner keys to correct snake/camelCase
887887
prefs = {}
888888
for pref in config:
889-
if globals.camel_case:
889+
if mt_config.camel_case:
890890
prefs[meshtastic.util.snake_to_camel(pref)] = config[pref]
891891
else:
892892
prefs[pref] = config[pref]
893-
if globals.camel_case:
893+
if mt_config.camel_case:
894894
configObj["config"] = config
895895
else:
896896
configObj["config"] = config
@@ -902,7 +902,7 @@ def export_config(interface):
902902
for pref in module_config:
903903
if len(module_config[pref]) > 0:
904904
prefs[pref] = module_config[pref]
905-
if globals.camel_case:
905+
if mt_config.camel_case:
906906
configObj["module_config"] = prefs
907907
else:
908908
configObj["module_config"] = prefs
@@ -916,8 +916,8 @@ def export_config(interface):
916916
def common():
917917
"""Shared code for all of our command line wrappers"""
918918
logfile = None
919-
args = globals.args
920-
parser = globals.parser
919+
args = mt_config.args
920+
parser = mt_config.parser
921921
logging.basicConfig(
922922
level=logging.DEBUG if (args.debug or args.listen) else logging.INFO,
923923
format="%(levelname)s file:%(filename)s %(funcName)s line:%(lineno)s %(message)s",
@@ -933,7 +933,7 @@ def common():
933933

934934
if args.ch_index is not None:
935935
channelIndex = int(args.ch_index)
936-
globals.channel_index = channelIndex
936+
mt_config.channel_index = channelIndex
937937

938938
if not args.dest:
939939
args.dest = BROADCAST_ADDR
@@ -968,7 +968,7 @@ def common():
968968
# Note: using "line buffering"
969969
# pylint: disable=R1732
970970
logfile = open(args.seriallog, "w+", buffering=1, encoding="utf8")
971-
globals.logfile = logfile
971+
mt_config.logfile = logfile
972972

973973
subscribe()
974974
if args.ble_scan:
@@ -1059,8 +1059,8 @@ def addConnectionArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentParse
10591059

10601060
def initParser():
10611061
"""Initialize the command line argument parsing."""
1062-
parser = globals.parser
1063-
args = globals.args
1062+
parser = mt_config.parser
1063+
args = mt_config.args
10641064

10651065
# The "Help" group includes the help option and other informational stuff about the CLI itself
10661066
outerHelpGroup = parser.add_argument_group('Help')
@@ -1426,8 +1426,8 @@ def initParser():
14261426

14271427

14281428
args = parser.parse_args()
1429-
globals.args = args
1430-
globals.parser = parser
1429+
mt_config.args = args
1430+
mt_config.parser = parser
14311431

14321432

14331433
def main():
@@ -1436,22 +1436,22 @@ def main():
14361436
add_help=False,
14371437
epilog="If no connection arguments are specified, we search for a compatible serial device, "
14381438
"and if none is found, then attempt a TCP connection to localhost.")
1439-
globals.parser = parser
1439+
mt_config.parser = parser
14401440
initParser()
14411441
common()
1442-
logfile = globals.logfile
1442+
logfile = mt_config.logfile
14431443
if logfile:
14441444
logfile.close()
14451445

14461446

14471447
def tunnelMain():
14481448
"""Run a meshtastic IP tunnel"""
14491449
parser = argparse.ArgumentParser(add_help=False)
1450-
globals.parser = parser
1450+
mt_config.parser = parser
14511451
initParser()
1452-
args = globals.args
1452+
args = mt_config.args
14531453
args.tunnel = True
1454-
globals.args = args
1454+
mt_config.args = args
14551455
common()
14561456

14571457

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
This is intended to make the Python read more naturally, and to make the
1010
intention of the code clearer and more compact. It is merely a sticking
11-
plaster over the use of shared globals, but the coupling issues wil be dealt
11+
plaster over the use of shared mt_config, but the coupling issues wil be dealt
1212
with rather more easily once the code is simplified by this change.
1313
1414
"""
@@ -17,6 +17,7 @@ def reset():
1717
"""
1818
Restore the namespace to pristine condition.
1919
"""
20+
# pylint: disable=W0603
2021
global args, parser, channel_index, logfile, tunnelInstance, camel_case
2122
args = None
2223
parser = None

meshtastic/tests/conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55

66
import pytest
77

8-
from meshtastic import globals
8+
from meshtastic import mt_config
99

1010
from ..mesh_interface import MeshInterface
1111

1212

1313
@pytest.fixture
14-
def reset_globals():
15-
"""Fixture to reset globals."""
14+
def reset_mt_config():
15+
"""Fixture to reset mt_config."""
1616
parser = None
1717
parser = argparse.ArgumentParser(add_help=False)
18-
globals.reset()
19-
globals.parser = parser
18+
mt_config.reset()
19+
mt_config.parser = parser
2020

2121

2222
@pytest.fixture

meshtastic/tests/test_init.py

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

77
import pytest
88

9-
from meshtastic import _onNodeInfoReceive, _onPositionReceive, _onTextReceive, globals
9+
from meshtastic import _onNodeInfoReceive, _onPositionReceive, _onTextReceive, mt_config
1010

1111
from ..serial_interface import SerialInterface
1212

@@ -15,7 +15,7 @@
1515
def test_init_onTextReceive_with_exception(caplog):
1616
"""Test _onTextReceive"""
1717
args = MagicMock()
18-
globals.args = args
18+
mt_config.args = args
1919
iface = MagicMock(autospec=SerialInterface)
2020
packet = {}
2121
with caplog.at_level(logging.DEBUG):
@@ -28,7 +28,7 @@ def test_init_onTextReceive_with_exception(caplog):
2828
def test_init_onPositionReceive(caplog):
2929
"""Test _onPositionReceive"""
3030
args = MagicMock()
31-
globals.args = args
31+
mt_config.args = args
3232
iface = MagicMock(autospec=SerialInterface)
3333
packet = {"from": "foo", "decoded": {"position": {}}}
3434
with caplog.at_level(logging.DEBUG):
@@ -40,7 +40,7 @@ def test_init_onPositionReceive(caplog):
4040
def test_init_onNodeInfoReceive(caplog, iface_with_nodes):
4141
"""Test _onNodeInfoReceive"""
4242
args = MagicMock()
43-
globals.args = args
43+
mt_config.args = args
4444
iface = iface_with_nodes
4545
iface.myInfo.my_node_num = 2475227164
4646
packet = {

0 commit comments

Comments
 (0)