Skip to content

Commit 7fcbbe9

Browse files
committed
refactor camel and snake naming in getPref
We have a lot of code duplication by checkin over and over again if field should be named in camel or snake notation. We simplify this by writing the choosen variant into a variable and just use that name across the code. No functional change.
1 parent 92a3986 commit 7fcbbe9

1 file changed

Lines changed: 6 additions & 18 deletions

File tree

meshtastic/__main__.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def getPref(node, comp_name):
9494
camel_name = meshtastic.util.snake_to_camel(name[1])
9595
# Note: protobufs has the keys in snake_case, so snake internally
9696
snake_name = meshtastic.util.camel_to_snake(name[1])
97+
uni_name = camel_name if mt_config.camel_case else snake_name
9798
logging.debug(f"snake_name:{snake_name} camel_name:{camel_name}")
9899
logging.debug(f"use camel:{mt_config.camel_case}")
99100

@@ -112,14 +113,9 @@ def getPref(node, comp_name):
112113
break
113114

114115
if not found:
115-
if mt_config.camel_case:
116-
print(
117-
f"{localConfig.__class__.__name__} and {moduleConfig.__class__.__name__} do not have an attribute {snake_name}."
118-
)
119-
else:
120-
print(
121-
f"{localConfig.__class__.__name__} and {moduleConfig.__class__.__name__} do not have attribute {snake_name}."
122-
)
116+
print(
117+
f"{localConfig.__class__.__name__} and {moduleConfig.__class__.__name__} do not have an attribute {uni_name}."
118+
)
123119
print("Choices are...")
124120
printConfig(localConfig)
125121
printConfig(moduleConfig)
@@ -131,16 +127,8 @@ def getPref(node, comp_name):
131127
config_values = getattr(config, config_type.name)
132128
if not wholeField:
133129
pref_value = getattr(config_values, pref.name)
134-
if mt_config.camel_case:
135-
print(f"{str(config_type.name)}.{camel_name}: {str(pref_value)}")
136-
logging.debug(
137-
f"{str(config_type.name)}.{camel_name}: {str(pref_value)}"
138-
)
139-
else:
140-
print(f"{str(config_type.name)}.{snake_name}: {str(pref_value)}")
141-
logging.debug(
142-
f"{str(config_type.name)}.{snake_name}: {str(pref_value)}"
143-
)
130+
print(f"{str(config_type.name)}.{uni_name}: {str(pref_value)}")
131+
logging.debug(f"{str(config_type.name)}.{uni_name}: {str(pref_value)}")
144132
else:
145133
print(f"{str(config_type.name)}:\n{str(config_values)}")
146134
logging.debug(f"{str(config_type.name)}: {str(config_values)}")

0 commit comments

Comments
 (0)