2015-01-14 04:12:56 +00:00
|
|
|
|
;;; funcs.el --- Python Layer functions File for Spacemacs
|
|
|
|
|
;;
|
2017-01-06 03:51:13 +00:00
|
|
|
|
;; Copyright (c) 2012-2017 Sylvain Benner & Contributors
|
2015-01-14 04:12:56 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
|
;;
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
;;
|
|
|
|
|
;;; License: GPLv3
|
|
|
|
|
|
2014-11-10 04:54:10 +00:00
|
|
|
|
;; from http://pedrokroger.net/2010/07/configuring-emacs-as-a-python-ide-2/
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs/python-annotate-pdb ()
|
2014-11-10 04:54:10 +00:00
|
|
|
|
"Highlight break point lines."
|
|
|
|
|
(interactive)
|
2016-11-06 02:40:16 +00:00
|
|
|
|
(highlight-lines-matching-regexp "import \\(pdb\\|ipdb\\|pudb\\|wdb\\)")
|
|
|
|
|
(highlight-lines-matching-regexp "\\(pdb\\|ipdb\\|pudb\\|wdb\\).set_trace()"))
|
2014-11-10 04:54:10 +00:00
|
|
|
|
|
2016-07-18 19:40:40 +00:00
|
|
|
|
(defun spacemacs/pyenv-executable-find (command)
|
|
|
|
|
"Find executable taking pyenv shims into account."
|
|
|
|
|
(if (executable-find "pyenv")
|
|
|
|
|
(progn
|
|
|
|
|
(let ((pyenv-string (shell-command-to-string (concat "pyenv which " command))))
|
|
|
|
|
(unless (string-match "not found" pyenv-string)
|
|
|
|
|
pyenv-string)))
|
|
|
|
|
(executable-find command)))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/python-setup-shell (&rest args)
|
|
|
|
|
(if (spacemacs/pyenv-executable-find "ipython")
|
|
|
|
|
(progn (setq python-shell-interpreter "ipython")
|
|
|
|
|
(if (version< (replace-regexp-in-string "\n$" "" (shell-command-to-string "ipython --version")) "5")
|
|
|
|
|
(setq python-shell-interpreter-args "-i")
|
|
|
|
|
(setq python-shell-interpreter-args "--simple-prompt -i")))
|
|
|
|
|
(progn
|
|
|
|
|
(setq python-shell-interpreter-args "-i")
|
|
|
|
|
(setq python-shell-interpreter "python"))))
|
|
|
|
|
|
2016-11-06 03:14:30 +00:00
|
|
|
|
(defun spacemacs/python-setup-checkers (&rest args)
|
|
|
|
|
(let ((pylint (spacemacs/pyenv-executable-find "pylint"))
|
|
|
|
|
(flake8 (spacemacs/pyenv-executable-find "flake8")))
|
|
|
|
|
(when pylint
|
|
|
|
|
(flycheck-set-checker-executable "python-pylint" pylint))
|
|
|
|
|
(when flake8
|
|
|
|
|
(flycheck-set-checker-executable "python-flake8" flake8))
|
|
|
|
|
))
|
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs/python-toggle-breakpoint ()
|
2014-11-10 04:54:10 +00:00
|
|
|
|
"Add a break point, highlight it."
|
|
|
|
|
(interactive)
|
2016-10-16 23:37:22 +00:00
|
|
|
|
(let ((trace (cond ((spacemacs/pyenv-executable-find "wdb") "import wdb; wdb.set_trace()")
|
|
|
|
|
((spacemacs/pyenv-executable-find "ipdb") "import ipdb; ipdb.set_trace()")
|
2016-07-25 21:27:15 +00:00
|
|
|
|
((spacemacs/pyenv-executable-find "pudb") "import pudb; pudb.set_trace()")
|
2016-10-20 08:03:18 +00:00
|
|
|
|
((spacemacs/pyenv-executable-find "ipdb3") "import ipdb; ipdb.set_trace()")
|
|
|
|
|
((spacemacs/pyenv-executable-find "pudb3") "import pudb; pudb.set_trace()")
|
2015-05-02 22:54:22 +00:00
|
|
|
|
(t "import pdb; pdb.set_trace()")))
|
2014-11-10 04:54:10 +00:00
|
|
|
|
(line (thing-at-point 'line)))
|
|
|
|
|
(if (and line (string-match trace line))
|
|
|
|
|
(kill-whole-line)
|
|
|
|
|
(progn
|
|
|
|
|
(back-to-indentation)
|
2016-11-07 10:36:26 +00:00
|
|
|
|
(insert trace)
|
|
|
|
|
(insert "\n")
|
2014-11-10 04:54:10 +00:00
|
|
|
|
(python-indent-line)))))
|
2015-04-24 05:12:30 +00:00
|
|
|
|
|
|
|
|
|
;; from https://www.snip2code.com/Snippet/127022/Emacs-auto-remove-unused-import-statemen
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs/python-remove-unused-imports()
|
2015-04-24 05:12:30 +00:00
|
|
|
|
"Use Autoflake to remove unused function"
|
|
|
|
|
"autoflake --remove-all-unused-imports -i unused_imports.py"
|
|
|
|
|
(interactive)
|
2015-04-25 02:15:02 +00:00
|
|
|
|
(if (executable-find "autoflake")
|
|
|
|
|
(progn
|
|
|
|
|
(shell-command (format "autoflake --remove-all-unused-imports -i %s"
|
|
|
|
|
(shell-quote-argument (buffer-file-name))))
|
|
|
|
|
(revert-buffer t t t))
|
|
|
|
|
(message "Error: Cannot find autoflake executable.")))
|
2015-12-07 12:58:36 +00:00
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs//pyenv-mode-set-local-version ()
|
2016-01-25 23:00:19 +00:00
|
|
|
|
"Set pyenv version from \".python-version\" by looking in parent directories."
|
2015-12-07 12:58:36 +00:00
|
|
|
|
(interactive)
|
2015-12-20 03:33:24 +00:00
|
|
|
|
(let ((root-path (locate-dominating-file default-directory
|
|
|
|
|
".python-version")))
|
2016-01-25 23:00:19 +00:00
|
|
|
|
(when root-path
|
2015-12-20 03:33:24 +00:00
|
|
|
|
(let* ((file-path (expand-file-name ".python-version" root-path))
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(version
|
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(insert-file-contents-literally file-path)
|
2017-01-27 09:06:47 +00:00
|
|
|
|
(nth 0 (split-string (buffer-substring-no-properties
|
|
|
|
|
(line-beginning-position)
|
|
|
|
|
(line-end-position)))))))
|
2015-12-20 03:33:24 +00:00
|
|
|
|
(if (member version (pyenv-mode-versions))
|
|
|
|
|
(pyenv-mode-set version)
|
|
|
|
|
(message "pyenv: version `%s' is not installed (set by %s)"
|
2016-01-25 23:00:19 +00:00
|
|
|
|
version file-path))))))
|
2016-04-13 02:38:48 +00:00
|
|
|
|
|
2016-10-25 17:25:57 +00:00
|
|
|
|
(defun spacemacs//pyvenv-mode-set-local-virtualenv ()
|
|
|
|
|
"Set pyvenv virtualenv from \".venv\" by looking in parent directories."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let ((root-path (locate-dominating-file default-directory
|
|
|
|
|
".venv")))
|
|
|
|
|
(when root-path
|
|
|
|
|
(let* ((file-path (expand-file-name ".venv" root-path))
|
|
|
|
|
(virtualenv
|
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(insert-file-contents-literally file-path)
|
|
|
|
|
(buffer-substring-no-properties (line-beginning-position)
|
|
|
|
|
(line-end-position)))))
|
|
|
|
|
(pyvenv-workon virtualenv)))))
|
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
|
|
|
|
|
;; Tests
|
|
|
|
|
|
2016-04-13 02:38:48 +00:00
|
|
|
|
(defun spacemacs//disable-semantic-idle-summary-mode ()
|
2016-04-16 02:08:35 +00:00
|
|
|
|
"Disable semantic-idle-summary in Python mode.
|
2016-04-13 02:38:48 +00:00
|
|
|
|
Anaconda provides more useful information but can not do it properly
|
|
|
|
|
when this mode is enabled since the minibuffer is cleared all the time."
|
2016-04-16 02:08:35 +00:00
|
|
|
|
(semantic-idle-summary-mode 0))
|
|
|
|
|
|
2016-10-15 08:34:11 +00:00
|
|
|
|
(defun spacemacs//python-imenu-create-index-use-semantic-maybe ()
|
2016-04-16 02:08:35 +00:00
|
|
|
|
"Use semantic if the layer is enabled."
|
2016-10-15 08:34:11 +00:00
|
|
|
|
(setq imenu-create-index-function 'spacemacs/python-imenu-create-index))
|
|
|
|
|
|
|
|
|
|
;; fix for issue #2569 (https://github.com/syl20bnr/spacemacs/issues/2569) and
|
|
|
|
|
;; Emacs 24.5 and older. use `semantic-create-imenu-index' only when
|
|
|
|
|
;; `semantic-mode' is enabled, otherwise use `python-imenu-create-index'
|
|
|
|
|
(defun spacemacs/python-imenu-create-index ()
|
|
|
|
|
(if (bound-and-true-p semantic-mode)
|
|
|
|
|
(semantic-create-imenu-index)
|
|
|
|
|
(python-imenu-create-index)))
|
2016-04-13 21:50:53 +00:00
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs//python-get-main-testrunner ()
|
2016-04-13 21:50:53 +00:00
|
|
|
|
"Get the main test runner."
|
|
|
|
|
(if (listp python-test-runner) (car python-test-runner) python-test-runner))
|
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs//python-get-secondary-testrunner ()
|
2016-04-13 21:50:53 +00:00
|
|
|
|
"Get the secondary test runner"
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(cdr (assoc (spacemacs//python-get-main-testrunner) '((pytest . nose)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
(nose . pytest)))))
|
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs//python-call-correct-test-function (arg funcalist)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
"Call a test function based on the chosen test framework.
|
|
|
|
|
ARG is the universal-argument which chooses between the main and
|
|
|
|
|
the secondary test runner. FUNCALIST is an alist of the function
|
|
|
|
|
to be called for each testrunner. "
|
2016-10-20 11:18:05 +00:00
|
|
|
|
(let* ((test-runner (if arg
|
|
|
|
|
(spacemacs//python-get-secondary-testrunner)
|
|
|
|
|
(spacemacs//python-get-main-testrunner)))
|
|
|
|
|
(test-function (assq test-runner funcalist)))
|
|
|
|
|
(if test-function
|
|
|
|
|
(funcall (cdr (assoc test-runner funcalist)))
|
|
|
|
|
(user-error "This test function is not available with the `%S' runner."
|
|
|
|
|
test-runner))))
|
2016-04-13 21:50:53 +00:00
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs/python-test-all (arg)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
"Run all tests."
|
|
|
|
|
(interactive "P")
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(spacemacs//python-call-correct-test-function arg '((pytest . pytest-all)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
(nose . nosetests-all))))
|
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs/python-test-pdb-all (arg)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
"Run all tests in debug mode."
|
|
|
|
|
(interactive "P")
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(spacemacs//python-call-correct-test-function arg '((pytest . pytest-pdb-all)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
(nose . nosetests-pdb-all))))
|
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs/python-test-module (arg)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
"Run all tests in the current module."
|
|
|
|
|
(interactive "P")
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(spacemacs//python-call-correct-test-function arg '((pytest . pytest-module)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
(nose . nosetests-module))))
|
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs/python-test-pdb-module (arg)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
"Run all tests in the current module in debug mode."
|
|
|
|
|
(interactive "P")
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(spacemacs//python-call-correct-test-function
|
|
|
|
|
arg
|
|
|
|
|
'((pytest . pytest-pdb-module)
|
|
|
|
|
(nose . nosetests-pdb-module))))
|
2016-04-13 21:50:53 +00:00
|
|
|
|
|
2016-10-20 11:18:05 +00:00
|
|
|
|
(defun spacemacs/python-test-suite (arg)
|
|
|
|
|
"Run all tests in the current suite."
|
|
|
|
|
(interactive "P")
|
|
|
|
|
(spacemacs//python-call-correct-test-function arg '((nose . nosetests-suite))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/python-test-pdb-suite (arg)
|
|
|
|
|
"Run all tests in the current suite in debug mode."
|
|
|
|
|
(interactive "P")
|
|
|
|
|
(spacemacs//python-call-correct-test-function arg '((nose . nosetests-pdb-suite))))
|
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs/python-test-one (arg)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
"Run current test."
|
|
|
|
|
(interactive "P")
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(spacemacs//python-call-correct-test-function arg '((pytest . pytest-one)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
(nose . nosetests-one))))
|
|
|
|
|
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(defun spacemacs/python-test-pdb-one (arg)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
"Run current test in debug mode."
|
|
|
|
|
(interactive "P")
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(spacemacs//python-call-correct-test-function arg '((pytest . pytest-pdb-one)
|
2016-04-13 21:50:53 +00:00
|
|
|
|
(nose . nosetests-pdb-one))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//bind-python-testing-keys ()
|
|
|
|
|
"Bind the keys for testing in Python."
|
2016-06-06 01:53:14 +00:00
|
|
|
|
(spacemacs/declare-prefix-for-mode 'python-mode "mt" "test")
|
2016-04-13 21:50:53 +00:00
|
|
|
|
(spacemacs/set-leader-keys-for-major-mode 'python-mode
|
2016-06-06 01:53:14 +00:00
|
|
|
|
"tA" 'spacemacs/python-test-pdb-all
|
|
|
|
|
"ta" 'spacemacs/python-test-all
|
|
|
|
|
"tB" 'spacemacs/python-test-pdb-module
|
|
|
|
|
"tb" 'spacemacs/python-test-module
|
|
|
|
|
"tT" 'spacemacs/python-test-pdb-one
|
|
|
|
|
"tt" 'spacemacs/python-test-one
|
|
|
|
|
"tM" 'spacemacs/python-test-pdb-module
|
2016-10-20 11:18:05 +00:00
|
|
|
|
"tm" 'spacemacs/python-test-module
|
|
|
|
|
"tS" 'spacemacs/python-test-pdb-suite
|
|
|
|
|
"ts" 'spacemacs/python-test-suite))
|
2016-08-21 15:49:20 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//python-sort-imports ()
|
|
|
|
|
;; py-isort-before-save checks the major mode as well, however we can prevent
|
|
|
|
|
;; it from loading the package unnecessarily by doing our own check
|
|
|
|
|
(when (and python-sort-imports-on-save
|
|
|
|
|
(derived-mode-p 'python-mode))
|
|
|
|
|
(py-isort-before-save)))
|