refactor: modularize match, auto-complete, and loop execution handlers - #176
refactor: modularize match, auto-complete, and loop execution handlers#176bhagathkrishnacdac wants to merge 1 commit into
Conversation
Signed-off-by: bhagathkrishnacdac <bhagath.krishna@cdac.in>
There was a problem hiding this comment.
🟡 Not ready to approve
Terminal error handling now prints to stdout and restore_echoctl() can raise when old_flags is unset, creating user-visible regressions.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Pull request overview
This PR refactors bessctl’s core CLI class to reduce cognitive complexity by decomposing initialization, command matching, auto-completion, and per-line execution into smaller helper methods.
Changes:
- Extracted initialization responsibilities into
_setup_history_file,_setup_error_output, and_setup_interactive_mode. - Decomposed
match()and_do_complete()into phase-specific helpers to reduce nesting and clarify control flow. - Split
process_one_line()into_read_input_line()and_execute_command(), and tightened history-file error handling.
File summaries
| File | Description |
|---|---|
| bessctl/cli.py | Refactors CLI initialization, matching, completion, and command loop into helper methods; adjusts terminal/history error handling. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 4
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
| try: | ||
| return os.path.expanduser('~/.bess_history') | ||
| except: | ||
| return None |
| except Exception as e: | ||
| print(f"Unexpected terminal error: {e}") |
| import termios | ||
|
|
||
| cur_flags = termios.tcgetattr(sys.stdin) | ||
| new_flags = cur_flags | ||
| if self.old_flags[3] & termios.ECHOCTL: |
| except Exception as e: | ||
| print(f"Unexpected terminal error: {e}") |
gab-arrobo
left a comment
There was a problem hiding this comment.
When trying to apply locally, I get lots of warnings due to empty trailing whitespaces
pr.patch:42: trailing whitespace.
def __init__(self, cmdlist, fin=sys.stdin, fout=sys.stdout, ferr=None,
pr.patch:43: trailing whitespace.
interactive=None, history_file=None):
pr.patch:44: trailing whitespace.
self.cmdlist = cmdlist
pr.patch:45: trailing whitespace.
self.fin = fin
pr.patch:46: trailing whitespace.
self.fout = fout
warning: squelched 350 whitespace errors
warning: 355 lines add whitespace errors.
There was a problem hiding this comment.
🟡 Not ready to approve
The updated terminal exception handlers print to stdout and use f-strings (breaking Python 2 parsing and risking corrupted CLI output), and initialization introduces a bare except: that should be narrowed.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Review details
Suppressed comments (3)
bessctl/cli.py:95
- Avoid bare
except:here; it catches BaseException (e.g., KeyboardInterrupt/SystemExit) and can mask shutdown signals during CLI init. Narrow toException(or a specific OS/path exception) for safer behavior.
try:
return os.path.expanduser('~/.bess_history')
except:
return None
bessctl/cli.py:630
- This exception handler prints to stdout and uses an f-string, which can pollute CLI output and also breaks Python 2 parsing (the rest of the file still has Python 2 compatibility paths). Prefer writing to
self.ferrwith%formatting (orformat) instead.
except Exception as e:
print(f"Unexpected terminal error: {e}")
bessctl/cli.py:646
- This exception handler prints to stdout and uses an f-string, which can pollute CLI output and also breaks Python 2 parsing (the rest of the file still has Python 2 compatibility paths). Prefer writing to
self.ferrwith%formatting (orformat) instead.
except Exception as e:
print(f"Unexpected terminal error: {e}")
- Files reviewed: 1/1 changed files
- Comments generated: 0 new
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
Overview
This PR refactors the core CLI class to address cognitive complexity and deep nesting in several methods.
Key Changes