Skip to content

Python cleanup - #173

Open
slycordinator wants to merge 4 commits into
mmaher88:masterfrom
slycordinator:python_cleanup
Open

slycordinator wants to merge 4 commits into
mmaher88:masterfrom
slycordinator:python_cleanup

Conversation

@slycordinator

Copy link
Copy Markdown

Python script cleanup

Replaces low level os library to use pathlib.Path for file operations and loop over pathlib.Path.rglob for files instead of using string paths or glob libary.
Replaces Optional[object] with object | None for hype hints
For repeated re.sub calls, uses re.compile early.
Reorders/condenses imports according to ruff
And in case of scripts/generate-placeholders.py, moves logitech_receiver imports early in script to fail before starting to work on other stuff (which would otherwise make the work pointless)

convert to use pathlib.Path for file operations

* allows files to be opened directly with their own path object [file_path.open()] instead of the low-level open function [open(file_path)]

* made extract_depo return list of Path objects

* replaced os.makedirs(dir, exist_ok=True) with dir_path.mkdir(parents=True, exist_ok=True)

* simplified looking for '.depot' files using pathlib.Path.rglob

* remove/count path objects by checking Path.suffix
* moved solaar imports to head of file
- arguably better for it to crash early before trying other operations

* moved repeated/looped re.sub patterns into their own compiled patterns to be reused

* changed file operations to use pathlib.Path instead of string paths
* generate-placholders.py - correct typo from previous

* optionsplus_extractor/canonicalize.py - ruff changed the imports into alphabetical with capitals first

* optionsplus_extractor/cli.py
- ruff changed order of imports
- replace Optional with typehint of "object | None"
- use re.compile for slugify reuse

* descriptor.py
consolidate imports

@mmaher88 mmaher88 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for the cleanup.

  • Please restore the parent-directory creation in extract_depot(). Only output_dir is created now, so depot entries with subpaths raise FileNotFoundError.
  • os.listdir -> rglob (extract-depot --all and sources.load_device_db) changes the search from top-level only to recursive. Please revert that unless it's intended, and if it is, call it out in the description.
  • Moving the Solaar import to module level means --help now fails without Solaar installed, and a missing special_keys no longer degrades gracefully. Either keep those imports lazy or note the change.
  • Minor: use not in instead of not x in.

The rest looks good.

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.

2 participants