-
Notifications
You must be signed in to change notification settings - Fork 2
/
run-command-recipes-python.el
136 lines (115 loc) · 5.2 KB
/
run-command-recipes-python.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
;;; run-command-recipes-python.el --- Recipe of `run-command' for `python` -*- lexical-binding: t; -*-
;; Author: semenInRussia <[email protected]>
;; Version: 0.1.0
;; Keywords: extensions run-command
;; Homepage: https://github.com/semenInRussia/emacs-run-command-recipes
;; URL: https://github.com/semenInRussia/emacs-run-command-recipes/blob/main/docs/python.md
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; For use this code put the following to your Emacs configuration:
;;
;; (run-command-recipes-use-one 'python)
;;
;;; Code:
(require 'dash)
(require 'f)
(require 'run-command-recipes-project)
(require 'run-command-recipes-lib)
(defcustom run-command-recipes-python-modes
'(python-mode)
"List of modes in which will work recipe of `run-command' `python'."
:type '(repeat symbol)
:group 'run-command-recipes)
(defcustom run-command-recipes-python-run-command "python {file-name}"
"Shell command, which just run current python file, and nothing else.
See `run-command-recipes-lib-build' for understand what's {file-name}."
:type 'string
:group 'run-command-recipes)
(defcustom run-command-recipes-python-test-filename "test_.*\\.py"
"Regexp which math with test's file of Python."
:type 'string
:group 'run-command-recipes)
(defcustom run-command-recipes-python-pytest-file-command "pytest {file-name}"
"Command which run `pytest' on current file.
See `run-command-recipes-lib-build' for understand what's {file-name}."
:type 'string
:group 'run-command-recipes)
(defcustom run-command-recipes-python-interactively-run-command
"python -i {file-name}"
"Command which run python file interactively.
See `run-command-recipes-lib-build' for understand what's {file-name}."
:type 'string
:group 'run-command-recipes)
(defcustom run-command-recipes-python-tests-dirs
'("tests")
"List of directories for pytest's tests."
:type '(repeat string)
:group 'run-command-recipes)
(defcustom run-command-recipes-python-pytest-project-command "pytest"
"Command which run all tests of python project via pytest.
Command will runned on root of project."
:type 'string
:group 'run-command-recipes)
(defcustom run-command-recipes-python-subrecipes
'(run-command-recipes-python-pytest run-command-recipes-python-virgin)
"List of subrecipes for recipe of `run-command' for python."
:group 'run-command-recipes-python
:type '(repeat function))
(defun run-command-recipes-python ()
"Recipe of `run-command' for `python'."
(apply #'run-command-recipes-lib-compose-recipes
run-command-recipes-python-subrecipes))
(defun run-command-recipes-python-mode-p ()
"Return t if in current buffer will should work `run-command-recipes-python'."
(-contains-p run-command-recipes-python-modes major-mode))
(defun run-command-recipes-python-pytest ()
"Recipe of `run-command' for `pytest', subrecipe of recipe for `python'."
(run-command-recipes-lib-build
(when (executable-find "pytest")
(list
(when (run-command-recipes-python-test-buffer-p)
(list
:command-name "run-pytests-in-current-file"
:display "Pytest: run file tests"
:command-line run-command-recipes-python-pytest-file-command))
(when (run-command-recipes-python-pytest-project-p)
(list
:command-name "run-all-pytests-in-project"
:display "Pytest: run project tests"
:working-dir (run-command-recipes-project-root)
:command-line run-command-recipes-python-pytest-project-command))))))
(defun run-command-recipes-python-pytest-project-p ()
"Return t if current project has pytest test."
(run-command-recipes-project-root-has-one-of
run-command-recipes-python-tests-dirs))
(defun run-command-recipes-python-test-buffer-p ()
"Return t, when current file is test's file of python.
Implementation depends on `run-command-recipes-python-test-buffer-p-function'"
(--when-let
(buffer-file-name)
(string-match-p run-command-recipes-python-test-filename it)))
(defun run-command-recipes-python-virgin ()
"Recipe of `run-command' for virgin version of python, subrecipe of python."
(run-command-recipes-lib-build
(when (and (buffer-file-name) (run-command-recipes-python-mode-p))
(list
(list
:command-name "run-python-file"
:display "Python: execute file"
:command-line run-command-recipes-python-run-command)
(list
:command-name "interactively-run-python-file"
:display "Python: execute, run file interactively"
:command-line run-command-recipes-python-interactively-run-command
:runner 'run-command-runner-term)))))
(provide 'run-command-recipes-python)
;;; run-command-recipes-python.el ends here