Skip to content

Instantly share code, notes, and snippets.

@ayufan
Created March 2, 2025 16:37
Show Gist options
  • Save ayufan/494775584ec3c7d5a51be5158b759fc8 to your computer and use it in GitHub Desktop.
Save ayufan/494775584ec3c7d5a51be5158b759fc8 to your computer and use it in GitHub Desktop.
Klipper Loader to allow extras out of box
#!/usr/bin/env python
import sys
import os
import fnmatch
import klippy
klippy_path = os.path.dirname(klippy.__file__)
matching_path = os.path.join(klippy_path, 'extras', '*.py')
org_exists = os.path.exists
def new_os_path_exists(path):
if org_exists(path):
return True
if fnmatch.fnmatch(path, matching_path):
module_path = os.path.relpath(path, klippy_path)
for sys_path in sys.path:
if sys_path == klippy_path:
break
if org_exists(os.path.join(sys_path, module_path)):
return True
return False
os.path.exists = new_os_path_exists
if __name__ == '__main__':
klippy.main()
@ayufan
Copy link
Author

ayufan commented Mar 2, 2025

Run:

PYTHONPATH=/path/to/dir/with/modules:/path/to/klipper/klippy ./klippy-loader.py

When using [hello_world] it will look for module in a following order:

  • /path/to/dir/with/modules/extras/hello_world.py
  • /path/to/dir/with/modules/extras/hello_world/__init__.py
  • /path/to/klipper/klippy/extras/hello_world.py
  • /path/to/klipper/klippy/extras/hello_world/__init__.py

It will start Klipper from /path/to/klipper/klippy/klippy.py folder.

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