Skip to content

refactor: replace nested conditionals in get_var_attrs with declarative token registry - #178

Open
bhagathkrishnacdac wants to merge 5 commits into
omec-project:mainfrom
bhagathkrishnacdac:bess-refactor-commands
Open

refactor: replace nested conditionals in get_var_attrs with declarative token registry#178
bhagathkrishnacdac wants to merge 5 commits into
omec-project:mainfrom
bhagathkrishnacdac:bess-refactor-commands

Conversation

@bhagathkrishnacdac

Copy link
Copy Markdown
Contributor

Summary
This PR refactors commands.py for improved code quality. This PR focuses on refactoring the CLI auto-completion logic to eliminate a massive if/elif chain in get_var_attrs.

Key Changes

  • Declarative Token Map (TOKEN_REGISTRY): Established a centralized map of autocomplete tokens to their respective types, descriptors, and candidate-provider lists.
  • Unified RPC Wrapper (_fetch_candidates): Introduced a standardized helper to wrap BESS RPC data queries, ensuring safe execution and uniform error-handling during active tab completion.
  • Dynamic Fetchers: Extracted dynamic data-gathering logic (e.g., worker status, port lists, module classes, active traffic classes) into isolated, single-responsibility helper functions.
  • Streamlined Dispatcher (get_var_attrs): Simplified the entry point to a lightweight lookup that uses inspect.signature to dynamically resolve parameter requirements for different candidate providers.

…ve token registry

Signed-off-by: bhagathkrishnacdac <bhagath.krishna@cdac.in>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors BESS CLI auto-completion token handling in bessctl by replacing a large conditional chain in get_var_attrs() with a declarative token registry and helper candidate fetchers, aiming to simplify maintenance of completion behavior.

Changes:

  • Introduced helper fetchers (_fetch_candidates, _get_workers, _get_ports, etc.) to centralize RPC-backed candidate collection.
  • Added a TOKEN_REGISTRY mapping tokens to (type, description, provider) to eliminate the large if/elif chain.
  • Simplified get_var_attrs() into a registry lookup + provider dispatch.
Suppressed comments (2)

bessctl/commands.py:285

  • [PORT_NUMBER] describes a port number but the text says "HTTP server address". This is user-facing help text shown during CLI guidance, so it should refer to the port.
    '[PORT_NUMBER]': ('int', 'HTTP server address to listen on (default: 5000)', []),

bessctl/commands.py:283

  • [PAUSE_WORKERS] description dropped the default value information that existed previously, which is useful in the interactive help output. Consider restoring the default note.
    '[PAUSE_WORKERS]': ('pause_workers', 'determines whether to pause workers', 
                        ['pause', 'no_pause']),

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread bessctl/commands.py Outdated
Comment thread bessctl/commands.py Outdated
Comment thread bessctl/commands.py
Signed-off-by: bhagathkrishnacdac <bhagath.krishna@cdac.in>
Signed-off-by: bhagathkrishnacdac <bhagath.krishna@cdac.in>
@andybavier

Copy link
Copy Markdown
Contributor

get_var_attrs() currently uses the provider’s parameter count to decide whether to pass partial_word. That appears to break _get_modules(cli, include_star=False): it has two parameters, so MODULE and MODULE... are invoked as _get_modules(cli, partial_word). Any nonempty partial word is truthy and therefore enables include_star, adding * to completions which seems unintended.

Suggest wrapping all module lookups:

'MODULE': (..., lambda cli, word: _get_modules(cli)),
'MODULE...': (..., lambda cli, word: _get_modules(cli)),
'[MODULE]': (..., lambda cli, word: _get_modules(cli, True)),

Comment thread bessctl/commands.py
'MCLASS': ('name', 'name of a module class', _get_mclasses),
'MCLASS...': ('name+', 'one or more module class names', _get_mclasses),
'[NEW_MODULE]': ('name', 'specify a name of the new module instance', []),
'MODULE': ('name', 'name of an existing module instance', _get_modules),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_var_attrs() currently uses the provider’s parameter count to decide whether to pass partial_word. That appears to break _get_modules(cli, include_star=False): it has two parameters, so MODULE and MODULE... are invoked as _get_modules(cli, partial_word). Any nonempty partial word is truthy and therefore enables include_star, adding * to completions which seems unintended.

Suggest wrapping all module lookups:

'MODULE': (..., lambda cli, word: _get_modules(cli)),
'MODULE...': (..., lambda cli, word: _get_modules(cli)),
'[MODULE]': (..., lambda cli, word: _get_modules(cli, True)),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants