Skip to content

Commit 760fcfc

Browse files
authored
Merge pull request #544 from holdenweb/new-globals
Refactor to avoid the use of a special global object.
2 parents a4830f5 + a07e853 commit 760fcfc

11 files changed

Lines changed: 335 additions & 430 deletions

meshtastic/__main__.py

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

1717
import meshtastic.test
1818
import meshtastic.util
19+
from meshtastic import mt_config
1920
from meshtastic import channel_pb2, config_pb2, portnums_pb2, remote_hardware, BROADCAST_ADDR
2021
from meshtastic.version import get_active_version
2122
from meshtastic.ble_interface import BLEInterface
22-
from meshtastic.globals import Globals
23-
2423

2524
def onReceive(packet, interface):
2625
"""Callback invoked when a packet arrives"""
27-
our_globals = Globals.getInstance()
28-
args = our_globals.get_args()
26+
args = mt_config.args
2927
try:
3028
d = packet.get("decoded")
3129
logging.debug(f"in onReceive() d:{d}")
@@ -69,7 +67,7 @@ def getPref(node, comp_name):
6967
# Note: protobufs has the keys in snake_case, so snake internally
7068
snake_name = meshtastic.util.camel_to_snake(name[1])
7169
logging.debug(f"snake_name:{snake_name} camel_name:{camel_name}")
72-
logging.debug(f"use camel:{Globals.getInstance().get_camel_case()}")
70+
logging.debug(f"use camel:{mt_config.camel_case}")
7371

7472
# First validate the input
7573
localConfig = node.localConfig
@@ -86,7 +84,7 @@ def getPref(node, comp_name):
8684
break
8785

8886
if not found:
89-
if Globals.getInstance().get_camel_case():
87+
if mt_config.camel_case:
9088
print(
9189
f"{localConfig.__class__.__name__} and {moduleConfig.__class__.__name__} do not have an attribute {snake_name}."
9290
)
@@ -105,7 +103,7 @@ def getPref(node, comp_name):
105103
config_values = getattr(config, config_type.name)
106104
if not wholeField:
107105
pref_value = getattr(config_values, pref.name)
108-
if Globals.getInstance().get_camel_case():
106+
if mt_config.camel_case:
109107
print(f"{str(config_type.name)}.{camel_name}: {str(pref_value)}")
110108
logging.debug(
111109
f"{str(config_type.name)}.{camel_name}: {str(pref_value)}"
@@ -171,7 +169,7 @@ def setPref(config, comp_name, valStr) -> bool:
171169
if e:
172170
val = e.number
173171
else:
174-
if Globals.getInstance().get_camel_case():
172+
if mt_config.camel_case:
175173
print(
176174
f"{name[0]}.{camel_name} does not have an enum called {val}, so you can not set it."
177175
)
@@ -210,7 +208,7 @@ def setPref(config, comp_name, valStr) -> bool:
210208
config_type.message_type.ignore_incoming.extend([val])
211209

212210
prefix = f"{name[0]}." if config_type.message_type is not None else ""
213-
if Globals.getInstance().get_camel_case():
211+
if mt_config.camel_case:
214212
print(f"Set {prefix}{camel_name} to {valStr}")
215213
else:
216214
print(f"Set {prefix}{snake_name} to {valStr}")
@@ -225,8 +223,7 @@ def onConnected(interface):
225223
False # Should we wait for an acknowledgment if we send to a remote node?
226224
)
227225
try:
228-
our_globals = Globals.getInstance()
229-
args = our_globals.get_args()
226+
args = mt_config.args
230227

231228
# do not print this line if we are exporting the config
232229
if not args.export_config:
@@ -477,7 +474,7 @@ def onConnected(interface):
477474
print("Writing modified preferences to device")
478475
node.writeConfig(field)
479476
else:
480-
if Globals.getInstance().get_camel_case():
477+
if mt_config.camel_case:
481478
print(
482479
f"{node.localConfig.__class__.__name__} and {node.moduleConfig.__class__.__name__} do not have an attribute {pref[0]}."
483480
)
@@ -590,7 +587,7 @@ def onConnected(interface):
590587
# handle changing channels
591588

592589
if args.ch_add:
593-
channelIndex = our_globals.get_channel_index()
590+
channelIndex = mt_config.channel_index
594591
if channelIndex is not None:
595592
# Since we set the channel index after adding a channel, don't allow --ch-index
596593
meshtastic.util.our_exit(
@@ -621,12 +618,12 @@ def onConnected(interface):
621618
n.writeChannel(ch.index)
622619
if channelIndex is None:
623620
print(f"Setting newly-added channel's {ch.index} as '--ch-index' for further modifications")
624-
our_globals.set_channel_index(ch.index)
621+
mt_config.channel_index = ch.index
625622

626623
if args.ch_del:
627624
closeNow = True
628625

629-
channelIndex = our_globals.get_channel_index()
626+
channelIndex = mt_config.channel_index
630627
if channelIndex is None:
631628
meshtastic.util.our_exit(
632629
"Warning: Need to specify '--ch-index' for '--ch-del'.", 1
@@ -642,7 +639,7 @@ def onConnected(interface):
642639

643640
def setSimpleConfig(modem_preset):
644641
"""Set one of the simple modem_config"""
645-
channelIndex = our_globals.get_channel_index()
642+
channelIndex = mt_config.channel_index
646643
if channelIndex is not None and channelIndex > 0:
647644
meshtastic.util.our_exit(
648645
"Warning: Cannot set modem preset for non-primary channel", 1
@@ -677,7 +674,7 @@ def setSimpleConfig(modem_preset):
677674
if args.ch_set or args.ch_enable or args.ch_disable:
678675
closeNow = True
679676

680-
channelIndex = our_globals.get_channel_index()
677+
channelIndex = mt_config.channel_index
681678
if channelIndex is None:
682679
meshtastic.util.our_exit("Warning: Need to specify '--ch-index'.", 1)
683680
ch = interface.getNode(args.dest).channels[channelIndex]
@@ -832,7 +829,7 @@ def printConfig(config):
832829
names = []
833830
for field in config.message_type.fields:
834831
tmp_name = f"{config_section.name}.{field.name}"
835-
if Globals.getInstance().get_camel_case():
832+
if mt_config.camel_case:
836833
tmp_name = meshtastic.util.snake_to_camel(tmp_name)
837834
names.append(tmp_name)
838835
for temp_name in sorted(names):
@@ -877,7 +874,7 @@ def export_config(interface):
877874
if owner_short:
878875
configObj["owner_short"] = owner_short
879876
if channel_url:
880-
if Globals.getInstance().get_camel_case():
877+
if mt_config.camel_case:
881878
configObj["channelUrl"] = channel_url
882879
else:
883880
configObj["channel_url"] = channel_url
@@ -889,11 +886,11 @@ def export_config(interface):
889886
# Convert inner keys to correct snake/camelCase
890887
prefs = {}
891888
for pref in config:
892-
if Globals.getInstance().get_camel_case():
889+
if mt_config.camel_case:
893890
prefs[meshtastic.util.snake_to_camel(pref)] = config[pref]
894891
else:
895892
prefs[pref] = config[pref]
896-
if Globals.getInstance().get_camel_case():
893+
if mt_config.camel_case:
897894
configObj["config"] = config
898895
else:
899896
configObj["config"] = config
@@ -905,7 +902,7 @@ def export_config(interface):
905902
for pref in module_config:
906903
if len(module_config[pref]) > 0:
907904
prefs[pref] = module_config[pref]
908-
if Globals.getInstance().get_camel_case():
905+
if mt_config.camel_case:
909906
configObj["module_config"] = prefs
910907
else:
911908
configObj["module_config"] = prefs
@@ -919,9 +916,8 @@ def export_config(interface):
919916
def common():
920917
"""Shared code for all of our command line wrappers"""
921918
logfile = None
922-
our_globals = Globals.getInstance()
923-
args = our_globals.get_args()
924-
parser = our_globals.get_parser()
919+
args = mt_config.args
920+
parser = mt_config.parser
925921
logging.basicConfig(
926922
level=logging.DEBUG if (args.debug or args.listen) else logging.INFO,
927923
format="%(levelname)s file:%(filename)s %(funcName)s line:%(lineno)s %(message)s",
@@ -937,7 +933,7 @@ def common():
937933

938934
if args.ch_index is not None:
939935
channelIndex = int(args.ch_index)
940-
our_globals.set_channel_index(channelIndex)
936+
mt_config.channel_index = channelIndex
941937

942938
if not args.dest:
943939
args.dest = BROADCAST_ADDR
@@ -972,7 +968,7 @@ def common():
972968
# Note: using "line buffering"
973969
# pylint: disable=R1732
974970
logfile = open(args.seriallog, "w+", buffering=1, encoding="utf8")
975-
our_globals.set_logfile(logfile)
971+
mt_config.logfile = logfile
976972

977973
subscribe()
978974
if args.ble_scan:
@@ -1063,9 +1059,8 @@ def addConnectionArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentParse
10631059

10641060
def initParser():
10651061
"""Initialize the command line argument parsing."""
1066-
our_globals = Globals.getInstance()
1067-
parser = our_globals.get_parser()
1068-
args = our_globals.get_args()
1062+
parser = mt_config.parser
1063+
args = mt_config.args
10691064

10701065
# The "Help" group includes the help option and other informational stuff about the CLI itself
10711066
outerHelpGroup = parser.add_argument_group('Help')
@@ -1286,7 +1281,7 @@ def initParser():
12861281
)
12871282

12881283
group.add_argument(
1289-
"--request-telemetry",
1284+
"--request-telemetry",
12901285
help="Request telemetry from a node. "
12911286
"You need pass the destination ID as argument with '--dest'. "
12921287
"For repeaters, the nodeNum is required.",
@@ -1431,34 +1426,32 @@ def initParser():
14311426

14321427

14331428
args = parser.parse_args()
1434-
our_globals.set_args(args)
1435-
our_globals.set_parser(parser)
1429+
mt_config.args = args
1430+
mt_config.parser = parser
14361431

14371432

14381433
def main():
14391434
"""Perform command line meshtastic operations"""
1440-
our_globals = Globals.getInstance()
14411435
parser = argparse.ArgumentParser(
14421436
add_help=False,
14431437
epilog="If no connection arguments are specified, we search for a compatible serial device, "
14441438
"and if none is found, then attempt a TCP connection to localhost.")
1445-
our_globals.set_parser(parser)
1439+
mt_config.parser = parser
14461440
initParser()
14471441
common()
1448-
logfile = our_globals.get_logfile()
1442+
logfile = mt_config.logfile
14491443
if logfile:
14501444
logfile.close()
14511445

14521446

14531447
def tunnelMain():
14541448
"""Run a meshtastic IP tunnel"""
1455-
our_globals = Globals.getInstance()
14561449
parser = argparse.ArgumentParser(add_help=False)
1457-
our_globals.set_parser(parser)
1450+
mt_config.parser = parser
14581451
initParser()
1459-
args = our_globals.get_args()
1452+
args = mt_config.args
14601453
args.tunnel = True
1461-
our_globals.set_args(args)
1454+
mt_config.args = args
14621455
common()
14631456

14641457

meshtastic/globals.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

meshtastic/mt_config.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Globals singleton class.
3+
4+
The Global object is gone, as are all its setters and getters. Instead the
5+
module itself is the singleton namespace, which can be imported into
6+
whichever module is used. The associated tests have also been removed,
7+
since we now rely on built in Python mechanisms.
8+
9+
This is intended to make the Python read more naturally, and to make the
10+
intention of the code clearer and more compact. It is merely a sticking
11+
plaster over the use of shared mt_config, but the coupling issues wil be dealt
12+
with rather more easily once the code is simplified by this change.
13+
14+
"""
15+
16+
def reset():
17+
"""
18+
Restore the namespace to pristine condition.
19+
"""
20+
# pylint: disable=W0603
21+
global args, parser, channel_index, logfile, tunnelInstance, camel_case
22+
args = None
23+
parser = None
24+
channel_index = None
25+
logfile = None
26+
tunnelInstance = None
27+
# TODO: to migrate to camel_case for v1.3 change this value to True
28+
camel_case = False
29+
30+
# These assignments are used instead of calling reset()
31+
# purely to shut pylint up.
32+
args = None
33+
parser = None
34+
channel_index = None
35+
logfile = None
36+
tunnelInstance = None
37+
camel_case = False

0 commit comments

Comments
 (0)