Skip to content

Commit

Permalink
Stub of elisp API
Browse files Browse the repository at this point in the history
  • Loading branch information
813gan committed Sep 11, 2024
1 parent 3958bf7 commit e63015d
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion emacspy.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
;; Keywords: python
;; Version: 1.0
;; Package: emacspy
;; Package-Requires:
;; Package-Requires: ((python-environment))

;;; Commentary:
;; # emacspy.el

;;; Code:

(eval-when-compile (require 'subr-x))
(require 'python-environment)

(defun emacspy--hash-table-to-lists (hash)
"Utility function that convert `HASH' into (list keys values)."
Expand All @@ -26,4 +27,47 @@

(require 'emacspy-module)

(defun python-environment-packages-paths (&optional root)
(let* ((ret nil)
(dirs (directory-files (python-environment-lib "" root)
't directory-files-no-dot-files-regexp 't)))
(dolist (dir dirs ret)
(push (format "%s/site-packages" dir) ret)) ))

;; emacspy public API

(defun emacspy-python-pip-install (subinterpreter packages &optional virtualenv)
"Install packages for `SUBINTERPRETER' from string list `PACKAGES'."
(let* ((packages-shell-arg (mapcar 'shell-quote-argument packages)))
(python-environment-run-block (append '("pip" "install" "--") packages-shell-arg)
subinterpreter virtualenv)))

(defun emacspy-python-environment-make (subinterpreter &optional packages virtualenv)
"Create venv for `SUBINTERPRETER' and install modules from string list `PACKAGES'."
(python-environment-make-block subinterpreter virtualenv)
(when packages
(emacspy-python-pip-install subinterpreter packages virtualenv) ))

(defun emacspy-setup-subinterpreter (subinterpreter &rest pythonpaths)
"Create Python subinterpreter called `SUBINTERPRETER' and add strings `PYTHONPATHS' to `sys.path'."
(py-make-interpreter subinterpreter)
(py-import subinterpreter "sys")
(py-get-object-attr subinterpreter "sys" "path" "__emacspy_syspath")
(when (python-environment-exists-p subinterpreter)
(dolist (path (python-environment-packages-paths subinterpreter))
(py-call-method subinterpreter "__emacspy_syspath" "append" nil path)))
(dolist (path pythonpaths)
(py-call-method subinterpreter "__emacspy_syspath" "append" nil path)) )

(defun emacspy--import (subinterpreter &rest imports)
"Import modules from `IMPORTS' in `SUBINTERPRETER'."
(dolist (import imports)
(cond
((consp import)
(py-import subinterpreter (car import) (cdr import) ))
((stringp import)
(py-import subinterpreter import)) )))

(provide 'emacspy)

;;; emacspy.el ends here

0 comments on commit e63015d

Please sign in to comment.