Skip to content

refactor: modernize panda.py to Python 3 with bug fixes, type hints, and CLI#1

Open
Blizzard39 wants to merge 3 commits intoPardus-Linux:masterfrom
Blizzard39:master
Open

refactor: modernize panda.py to Python 3 with bug fixes, type hints, and CLI#1
Blizzard39 wants to merge 3 commits intoPardus-Linux:masterfrom
Blizzard39:master

Conversation

@Blizzard39
Copy link
Copy Markdown

Summary

This PR modernizes panda.py from Python 2 to Python 3, fixes several bugs,
and improves overall code quality and maintainability.

Bug Fixes

  • get_driver_types()self.driver_name in "fglrx" was checking if
    the driver name is a substring of the literal string "fglrx" (e.g.
    "fx" in "fglrx" is True). Fixed with a proper dict-key lookup.
  • update_grub_entries()configured = line != new_line was
    overwriting the flag on every kernel line instead of accumulating it.
    The final value only reflected the last matched line. Fixed with |=.
  • Unclosed file handles — multiple open(...).read() calls throughout
    the codebase leaked file descriptors. All converted to with blocks /
    Path.read_text().
  • filter() in Python 3 returns a lazy iterator, not a list. Converted
    to a set comprehension to preserve correct behaviour.

Python 3 Modernisation

  • All print statements → print() functions (then replaced by logging)
  • os.pathpathlib.Path throughout
  • %-string formatting → f-strings
  • pisi is imported lazily only when actually needed (avoids import errors
    when pisi is not installed)

New Features

  • Type hints on all public methods and most private helpers
  • logging replaces bare print statements; --debug flag exposes
    DEBUG-level output
  • Proper argparse CLI replaces the ad-hoc __main__ test block:
    panda --state, panda --set vendor|os|generic, panda --packages, etc.
  • Module-level constants (SYSDIR, DRIVERS_DB, OS_DRIVER_MAP, …) make
    the code easy to test and mock
  • GRUB_NEW temp file is cleaned up after writing

Testing

Manually tested against a mock filesystem layout mirroring /sys/bus/pci,
/etc/kernel/, and /boot/grub/grub.conf on Python 3.11.

Notes

  • The public API is backwards-compatible except for the rename of
    get_grub_state()get_driver_state() and
    update_grub_entries()set_driver_state(), which better describe
    what the methods actually do.
  • setup.py can optionally be replaced with pyproject.toml (PEP 517);
    happy to include that as a separate commit if desired.

Updated setup.py to use PEP 517/518 format with build-system and project metadata.
…and CLI

## Summary

This PR modernizes `panda.py` from Python 2 to Python 3, fixes several bugs,
and improves overall code quality and maintainability.

## Bug Fixes

- **`get_driver_types()`** — `self.driver_name in "fglrx"` was checking if
  the driver name is a *substring* of the literal string `"fglrx"` (e.g.
  `"fx" in "fglrx"` is `True`). Fixed with a proper dict-key lookup.
- **`update_grub_entries()`** — `configured = line != new_line` was
  *overwriting* the flag on every kernel line instead of accumulating it.
  The final value only reflected the last matched line. Fixed with `|=`.
- **Unclosed file handles** — multiple `open(...).read()` calls throughout
  the codebase leaked file descriptors. All converted to `with` blocks /
  `Path.read_text()`.
- **`filter()` in Python 3** returns a lazy iterator, not a list. Converted
  to a set comprehension to preserve correct behaviour.

## Python 3 Modernisation

- All `print` statements → `print()` functions (then replaced by `logging`)
- `os.path` → `pathlib.Path` throughout
- `%`-string formatting → f-strings
- `pisi` is imported lazily only when actually needed (avoids import errors
  when pisi is not installed)

## New Features

- **Type hints** on all public methods and most private helpers
- **`logging`** replaces bare `print` statements; `--debug` flag exposes
  `DEBUG`-level output
- **Proper `argparse` CLI** replaces the ad-hoc `__main__` test block:
  `panda --state`, `panda --set vendor|os|generic`, `panda --packages`, etc.
- Module-level constants (`SYSDIR`, `DRIVERS_DB`, `OS_DRIVER_MAP`, …) make
  the code easy to test and mock
- `GRUB_NEW` temp file is cleaned up after writing

## Testing

Manually tested against a mock filesystem layout mirroring `/sys/bus/pci`,
`/etc/kernel/`, and `/boot/grub/grub.conf` on Python 3.11.

## Notes

- The public API is backwards-compatible except for the rename of
  `get_grub_state()` → `get_driver_state()` and
  `update_grub_entries()` → `set_driver_state()`, which better describe
  what the methods actually do.
- `setup.py` can optionally be replaced with `pyproject.toml` (PEP 517);
  happy to include that as a separate commit if desired.
Refactor panda-helper to use Python 3 and improve command structure.
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.

1 participant