-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpython.lua
More file actions
269 lines (227 loc) · 7.18 KB
/
Copy pathpython.lua
File metadata and controls
269 lines (227 loc) · 7.18 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
local M = {}
local config = require("pyrepl.config")
local packages = { "jupyter-console", "pynvim", "cairosvg", "pillow" }
local tools = { uv = "uv pip install -p %s", pip = "%s -m pip install" }
local python_path_cache
local console_path_cache
local console_completions_cache
local console_completions_running
---Resolve Python path from the following candidates:
---config.python_path -> vim.g.python3_host_prog -> "python3"
---@return string
function M.get_python_path()
if python_path_cache then
return python_path_cache
end
local candidates = {
config.get_state().python_path or "",
vim.g.python3_host_prog or "",
"python3",
}
for _, candidate in ipairs(candidates) do
candidate = vim.fn.expand(candidate)
if vim.fn.executable(candidate) == 1 then
python_path_cache = vim.fn.exepath(candidate)
return python_path_cache
end
end
error(config.get_message_prefix() .. "python executable not found, configure `python_path`", 0)
end
---@return string
function M.get_console_path()
if console_path_cache then
return console_path_cache
end
local candidates = vim.api.nvim_get_runtime_file("src/pyrepl/console.py", false)
if candidates and #candidates > 0 then
console_path_cache = candidates[1]
return console_path_cache
end
error(config.get_message_prefix() .. "console script not found, potential bug", 0)
end
---@class pyrepl.KernelSpec
---@field name string
---@field resource_dir string
---List available Jupyter kernels.
---@return pyrepl.KernelSpec
local function list_kernels()
local cmd = {
M.get_python_path(),
"-m",
"jupyter_client.kernelspecapp",
"list",
"--json",
}
local obj = vim.system(cmd, { text = true }):wait()
if obj.code ~= 0 then
error(config.get_message_prefix() .. "failed to list kernels, try `:PyreplInstall`", 0)
end
local ok, specs = pcall(vim.json.decode, obj.stdout)
if not ok then
error(config.get_message_prefix() .. "failed to decode kernelspecs json", 0)
end
local kernels = {}
for name, spec in pairs(specs["kernelspecs"]) do
local index = #kernels + 1
local item = { name = name, resource_dir = spec.resource_dir }
if name == config.get_state().preferred_kernel then
index = 1
end
table.insert(kernels, index, item)
end
return kernels
end
---Prompt user to choose kernel and call callback with that choice.
---@param callback fun(kernel: string)
function M.prompt_kernel(callback)
local ok, kernels = pcall(list_kernels)
if not ok then
vim.notify(kernels --[[@as string]], vim.log.levels.ERROR)
return
end
vim.ui.select(kernels, {
prompt = "Select Jupyter kernel",
format_item = function(item)
return string.format("%s (%s)", item.name, item.resource_dir)
end,
}, function(choice)
if choice then
callback(choice.name)
end
end)
end
---Feed the command to install required packages to the command line.
---@param tool string
function M.install_packages(tool)
if not tools[tool] then
vim.notify(
config.get_message_prefix() .. string.format("unknown tool '%s'", tool),
vim.log.levels.ERROR
)
return
end
local ok, python_path = pcall(M.get_python_path)
if not ok then
vim.notify(python_path, vim.log.levels.ERROR)
return
end
local packages_string = table.concat(packages, " ")
local cmd = tools[tool]:format(python_path) .. " " .. packages_string
vim.api.nvim_feedkeys(":!" .. cmd, "n", true)
end
---@param stdout string|nil
local function parse_console_flags(stdout)
local seen = {}
local completions = {}
for line in (stdout or ""):gmatch("[^\r\n]+") do
for flag in line:gmatch("%-%-[%w][%w%._-]*") do
if not seen[flag] then
seen[flag] = true
table.insert(completions, flag)
end
end
end
table.sort(completions, function(a, b)
return a > b
end)
return completions
end
---@param reload boolean|nil
function M.load_console_completions(reload)
if reload then
console_completions_cache = nil
end
if console_completions_cache then
return
end
if console_completions_running then
return
end
console_completions_running = true
pcall(function()
vim.system({
M.get_python_path(),
"-m",
"jupyter",
"console",
"--help-all",
}, { text = true }, function(result)
if result.code == 0 then
console_completions_cache = parse_console_flags(result.stdout)
end
console_completions_running = false
end)
end)
end
---Get completions for Jupyter Console CLI flags.
---@param arglead string
---@return string[]
function M.get_console_completions(arglead)
M.load_console_completions()
return vim.tbl_filter(function(item)
return vim.startswith(item, arglead)
end, console_completions_cache or {})
end
---Get completions for tools.
---@param arglead string
---@return string[]
function M.get_tool_completions(arglead)
return vim.tbl_filter(function(item)
return vim.startswith(item, arglead)
end, vim.tbl_keys(tools))
end
---Check if required Python packages are importable.
---@return boolean
function M.check_dependencies()
local ok, python_path = pcall(M.get_python_path)
if not ok then
return false
end
local obj = vim.system(
{ python_path, "-c", "import jupyter_console, pynvim" },
{ text = true }
):wait()
return obj.code == 0
end
---Check dependencies and, if `auto_install` is set, install them silently.
---Calls callback only after dependencies are confirmed present.
---@param callback fun()
function M.ensure_dependencies(callback)
if M.check_dependencies() then
callback()
return
end
local tool = config.get_state().auto_install
if not tool or not tools[tool] then
vim.notify(
config.get_message_prefix()
.. "dependencies missing, run `:PyreplInstall pip|uv` or set `auto_install`",
vim.log.levels.ERROR
)
return
end
local ok, python_path = pcall(M.get_python_path)
if not ok then
vim.notify(python_path, vim.log.levels.ERROR)
return
end
local packages_string = table.concat(packages, " ")
local cmd_str = tools[tool]:format(python_path) .. " " .. packages_string
vim.notify(config.get_message_prefix() .. "installing dependencies...")
vim.system(
{ "/bin/sh", "-c", cmd_str },
{ text = true },
vim.schedule_wrap(function(obj)
if obj.code == 0 then
vim.notify(config.get_message_prefix() .. "dependencies installed")
callback()
else
vim.notify(
config.get_message_prefix() .. "installation failed\n" .. (obj.stderr or ""),
vim.log.levels.ERROR
)
end
end)
)
end
return M