Skip to content

Commit 7b2a981

Browse files
Refactor REPL server to reduce complexity (#1499)
Co-authored-by: jan iversen <jancasacondor@gmail.com>
1 parent d6dce90 commit 7b2a981

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

pymodbus/repl/server/cli.py

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def print_help():
105105
)
106106

107107

108-
async def interactive_shell(server): # pylint: disable=too-complex
109-
"""Run CLI interactive shell."""
108+
def print_title():
109+
"""Print title - large if there are sufficient columns, otherwise small."""
110110
col = get_terminal_width()
111111
max_len = max( # pylint: disable=consider-using-generator
112112
[len(t) for t in TITLE.split("\n")]
@@ -117,16 +117,20 @@ async def interactive_shell(server): # pylint: disable=too-complex
117117
print_formatted_text(
118118
HTML(f'<u><b><style color="green">{SMALL_TITLE}</style></b></u>')
119119
)
120+
121+
122+
async def interactive_shell(server): # pylint: disable=too-complex
123+
"""Run CLI interactive shell."""
124+
print_title()
120125
info("")
121126
completer = NestedCompleter.from_nested_dict(COMMANDS)
122127
session = PromptSession(
123128
"SERVER > ", completer=completer, bottom_toolbar=BOTTOM_TOOLBAR
124129
)
125130

126131
# Run echo loop. Read text from stdin, and reply it back.
127-
while True: # pylint: disable=too-many-nested-blocks
132+
while True:
128133
try:
129-
invalid_command = False
130134
result = await session.prompt_async()
131135
if result == "exit":
132136
await server.web_app.shutdown()
@@ -139,51 +143,13 @@ async def interactive_shell(server): # pylint: disable=too-complex
139143
continue
140144
if command := result.split():
141145
if command[0] not in COMMANDS:
142-
invalid_command = True
143-
if invalid_command:
144146
warning(f"Invalid command or invalid usage of command - {command}")
145147
continue
146148
if len(command) == 1:
147149
warning(f'Usage: "{USAGE}"')
148150
else:
149-
args = command[1:]
150-
skip_next = False
151-
val_dict = {}
152-
for index, arg in enumerate(args):
153-
if skip_next:
154-
skip_next = False
155-
continue
156-
if "=" in arg:
157-
arg, value = arg.split("=")
158-
elif arg in COMMAND_ARGS:
159-
try:
160-
value = args[index + 1]
161-
skip_next = True
162-
except IndexError:
163-
error(f"Missing value for argument - {arg}")
164-
warning('Usage: "{USAGE}"')
165-
break
166-
valid = True
167-
if arg == "response_type":
168-
if value not in RESPONSE_TYPES:
169-
warning(f"Invalid response type request - {value}")
170-
warning(f"Choose from {RESPONSE_TYPES}")
171-
valid = False
172-
elif arg in { # pylint: disable=confusing-consecutive-elif
173-
"error_code",
174-
"delay_by",
175-
"clear_after",
176-
"data_len",
177-
}:
178-
try:
179-
value = int(value)
180-
except ValueError:
181-
warning(f"Expected integer value for {arg}")
182-
valid = False
183-
184-
if valid:
185-
val_dict[arg] = value
186-
if val_dict:
151+
val_dict = _process_args(command[1:])
152+
if val_dict: # pylint: disable=consider-using-assignment-expr
187153
server.update_manipulator_config(val_dict)
188154
# server.manipulator_config = val_dict
189155
# result = await run_command(tester, *command)
@@ -192,6 +158,44 @@ async def interactive_shell(server): # pylint: disable=too-complex
192158
return
193159

194160

161+
def _process_args(args) -> dict: # pylint: disable=too-complex
162+
"""Process arguments passed to CLI."""
163+
skip_next = False
164+
val_dict = {}
165+
for index, arg in enumerate(args):
166+
if skip_next:
167+
skip_next = False
168+
continue
169+
if "=" in arg:
170+
arg, value = arg.split("=")
171+
elif arg in COMMAND_ARGS:
172+
try:
173+
value = args[index + 1]
174+
skip_next = True
175+
except IndexError:
176+
error(f"Missing value for argument - {arg}")
177+
warning('Usage: "{USAGE}"')
178+
break
179+
if arg == "response_type":
180+
if value not in RESPONSE_TYPES:
181+
warning(f"Invalid response type request - {value}")
182+
warning(f"Choose from {RESPONSE_TYPES}")
183+
continue
184+
elif arg in { # pylint: disable=confusing-consecutive-elif
185+
"error_code",
186+
"delay_by",
187+
"clear_after",
188+
"data_len",
189+
}:
190+
try:
191+
value = int(value)
192+
except ValueError:
193+
warning(f"Expected integer value for {arg}")
194+
continue
195+
val_dict[arg] = value
196+
return val_dict
197+
198+
195199
async def main(server):
196200
"""Run main."""
197201
# with patch_stdout():

0 commit comments

Comments
 (0)