Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.4 KiB

  1. (defun rlbr/get-venv-name (&optional library-root)
  2. "Generate venv name based off of the base-name of the library root"
  3. (file-name-base
  4. (directory-file-name
  5. (if library-root
  6. library-root
  7. (elpy-library-root)))))
  8. (defun rlbr/handle-name-conflicts (venv-name)
  9. "Deal with potential name conflicts in venv"
  10. (let ((venv-conflicts)
  11. (venv-partition-name))
  12. (setq venv-partition-name (rlbr/split-venv-with-number venv-name))
  13. (setq venv-conflicts
  14. (seq-filter
  15. (lambda (item) (string-equal (cdr item) venv-name))
  16. (mapcar #'rlbr/split-venv-with-number (pyvenv-virtualenv-list))))
  17. (when venv-conflicts
  18. (setcar venv-partition-name (1+ (apply 'max (mapcar #'car venv-conflicts)))))
  19. (rlbr/join-venv-with-number venv-partition-name)))
  20. (defun rlbr/setup-python-venv-dirlocals (&optional library-root)
  21. "Setup .dir-locals file in library root and tell vc system to ignore .dir-locals file"
  22. (let (library-root (if library-root
  23. library-root
  24. (elpy-library-root)))
  25. ))
  26. (defun rlbr/init-python-venv-in-library-root (&optional library-root)
  27. "If no venv is specified in the library root .dir-locals file, prompt to either create one or use default"
  28. (let ((venv-name (rlbr/get-venv-name)))
  29. (setq venv-name (rlbr/handle-name-conflicts venv-name))
  30. (if (y-or-n-p (format "Create venv '%'?" venv-name))
  31. (pyvenv-create venv-name python-command))))