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
Open
refactor: modernize panda.py to Python 3 with bug fixes, type hints, and CLI#1Blizzard39 wants to merge 3 commits intoPardus-Linux:masterfrom
Blizzard39 wants to merge 3 commits intoPardus-Linux:masterfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR modernizes
panda.pyfrom 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 ifthe driver name is a substring of the literal string
"fglrx"(e.g."fx" in "fglrx"isTrue). Fixed with a proper dict-key lookup.update_grub_entries()—configured = line != new_linewasoverwriting the flag on every kernel line instead of accumulating it.
The final value only reflected the last matched line. Fixed with
|=.open(...).read()calls throughoutthe codebase leaked file descriptors. All converted to
withblocks /Path.read_text().filter()in Python 3 returns a lazy iterator, not a list. Convertedto a set comprehension to preserve correct behaviour.
Python 3 Modernisation
printstatements →print()functions (then replaced bylogging)os.path→pathlib.Paththroughout%-string formatting → f-stringspisiis imported lazily only when actually needed (avoids import errorswhen pisi is not installed)
New Features
loggingreplaces bareprintstatements;--debugflag exposesDEBUG-level outputargparseCLI replaces the ad-hoc__main__test block:panda --state,panda --set vendor|os|generic,panda --packages, etc.SYSDIR,DRIVERS_DB,OS_DRIVER_MAP, …) makethe code easy to test and mock
GRUB_NEWtemp file is cleaned up after writingTesting
Manually tested against a mock filesystem layout mirroring
/sys/bus/pci,/etc/kernel/, and/boot/grub/grub.confon Python 3.11.Notes
get_grub_state()→get_driver_state()andupdate_grub_entries()→set_driver_state(), which better describewhat the methods actually do.
setup.pycan optionally be replaced withpyproject.toml(PEP 517);happy to include that as a separate commit if desired.