Skip to content

Reduce cyclomatic complexity of load_additional_plugins#3475

Open
wavebyrd wants to merge 6039 commits intonicolargo:developfrom
wavebyrd:refactor-load-additional-plugins
Open

Reduce cyclomatic complexity of load_additional_plugins#3475
wavebyrd wants to merge 6039 commits intonicolargo:developfrom
wavebyrd:refactor-load-additional-plugins

Conversation

@wavebyrd
Copy link
Copy Markdown

Summary

Refactors load_additional_plugins in glances/stats.py to reduce its cyclomatic complexity from 19 to well under 15, as flagged in #3460.

Problem

The load_additional_plugins method had a deeply nested structure with an inner function definition, multiple conditional branches for path resolution, and interleaved plugin discovery/loading logic - all contributing to a complexity score of 19.

There was also a subtle bug: fd.read() was called twice on the same file handle, so the second or condition (checking for <Name>Plugin) always compared against an empty string since the cursor had already reached EOF after the first read.

Solution

Extracted three focused helper methods:

  • _resolve_plugin_dir(args, config) - resolves the plugin directory from config and args
  • _find_plugins_in_dir(plugin_dir) - scans a directory for valid plugin modules
  • _load_additional_plugin(plugin_name, args, config, timer) - imports and registers a single plugin

The main load_additional_plugins method now reads as a clean pipeline: resolve path, scan for plugins, load each one.

The fd.read() bug is fixed by reading the file content into a local variable once and checking both patterns against it.

Changes

glances/stats.py:

  • Extracted _resolve_plugin_dir, _find_plugins_in_dir, _load_additional_plugin as separate methods
  • Simplified load_additional_plugins to use the helpers with an early return
  • Fixed double fd.read() bug in plugin file scanning
  • No other behavioral changes

Testing

  • Linting: ruff check glances/stats.py passes clean
  • Formatting: ruff format --check glances/stats.py passes clean
  • Complexity: ruff check --select C90 with max-complexity=10 passes (all extracted methods are well under the threshold)

Checklist

  • PR targets the develop branch
  • Code follows PEP8 (verified via Ruff)
  • Ran ruff format --check and ruff check with no issues
  • No functional regressions or behavioral changes (aside from the fd.read() fix)

drake7707 and others added 30 commits December 15, 2025 07:57
Include GTT to mem% for integrated AMD GPUs
License classifiers have been superseded by license expressions (see https://peps.python.org/pep-0639/).
Detailed changes:
- Enable autoescape=True for Jinja2 environment to prevent XSS (B701).
- Add timeout=15s to requests.get in nginx amp (B113).
- Add timeout=15s to post in restful export (B113).
…ty-bandit

fix(security): resolve B701 (Jinja2) and B113 (timeout) vulnerabilities
Update license specification to SPDX format
Fix quicklook in case psutil.cpu_freq().max=0.0
nicolargo and others added 28 commits March 4, 2026 16:15
Added installation instructions for Glances using UVx.
…hub.com:sdoshi2061/glances into sdoshi2061-mem-plugin_e8a559ce-ab5b-4674-91e2-9c4f42636da2
Unauthenticated Configuration Secrets Exposure
SQL Injection via Process Names in TimescaleDB Export
Updated security patch descriptions with correct CVE identifiers.
Extract three helper methods from load_additional_plugins in stats.py:

- _resolve_plugin_dir: handles config/args path resolution
- _find_plugins_in_dir: scans directories for valid plugin modules
- _load_additional_plugin: handles import and registration of a single plugin

This also fixes a subtle bug where fd.read() was called twice on the
same file handle, meaning the second check always ran against an empty
string. The file content is now read once into a local variable.

No behavioral changes beyond the fd.read() fix. Reduces the function
complexity from 19 to well under 15.
@wavebyrd wavebyrd force-pushed the refactor-load-additional-plugins branch from 5d2c940 to bd913e9 Compare March 13, 2026 21:23
@nicolargo
Copy link
Copy Markdown
Owner

Sorry @wavebyrd but i can not review a PR with more than 600 files modified (perhaps a format/lint issue in your dev environment (https://github.com/nicolargo/glances/wiki/How-to-contribute-to-Glances-%3F).

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.