2017-05-05 22:03:19 +00:00
|
|
|
" Author: w0rp <devw0rp@gmail.com>
|
|
|
|
" Description: Functions for integrating with Python linters.
|
2017-05-06 18:11:43 +00:00
|
|
|
|
2018-09-16 01:38:26 +00:00
|
|
|
call ale#Set('python_auto_pipenv', '0')
|
2021-07-25 04:39:05 +00:00
|
|
|
call ale#Set('python_auto_poetry', '0')
|
2018-09-16 01:38:26 +00:00
|
|
|
|
2017-10-01 17:47:54 +00:00
|
|
|
let s:sep = has('win32') ? '\' : '/'
|
2017-07-05 12:07:55 +00:00
|
|
|
" bin is used for Unix virtualenv directories, and Scripts is for Windows.
|
|
|
|
let s:bin_dir = has('unix') ? 'bin' : 'Scripts'
|
2017-05-20 12:34:53 +00:00
|
|
|
let g:ale_virtualenv_dir_names = get(g:, 'ale_virtualenv_dir_names', [
|
|
|
|
\ '.env',
|
2018-07-14 11:24:46 +00:00
|
|
|
\ '.venv',
|
2017-05-20 12:34:53 +00:00
|
|
|
\ 'env',
|
|
|
|
\ 've-py3',
|
2017-06-07 20:45:48 +00:00
|
|
|
\ 've',
|
2017-05-20 12:34:53 +00:00
|
|
|
\ 'virtualenv',
|
2017-12-18 09:33:11 +00:00
|
|
|
\ 'venv',
|
2017-05-20 12:34:53 +00:00
|
|
|
\])
|
|
|
|
|
2017-07-11 20:57:37 +00:00
|
|
|
function! ale#python#FindProjectRootIni(buffer) abort
|
|
|
|
for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
|
2018-05-30 19:09:33 +00:00
|
|
|
" If you change this, update ale-python-root documentation.
|
2017-07-11 20:57:37 +00:00
|
|
|
if filereadable(l:path . '/MANIFEST.in')
|
|
|
|
\|| filereadable(l:path . '/setup.cfg')
|
|
|
|
\|| filereadable(l:path . '/pytest.ini')
|
|
|
|
\|| filereadable(l:path . '/tox.ini')
|
2021-09-15 12:07:45 +00:00
|
|
|
\|| filereadable(l:path . '/.pyre_configuration.local')
|
2017-12-17 12:38:46 +00:00
|
|
|
\|| filereadable(l:path . '/mypy.ini')
|
2022-05-15 03:20:16 +00:00
|
|
|
\|| filereadable(l:path . '/.mypy.ini')
|
2017-11-08 17:58:56 +00:00
|
|
|
\|| filereadable(l:path . '/pycodestyle.cfg')
|
2019-05-18 13:42:19 +00:00
|
|
|
\|| filereadable(l:path . '/.flake8')
|
2018-08-02 03:07:11 +00:00
|
|
|
\|| filereadable(l:path . '/.flake8rc')
|
2019-02-08 21:44:34 +00:00
|
|
|
\|| filereadable(l:path . '/pylama.ini')
|
2019-02-17 17:12:24 +00:00
|
|
|
\|| filereadable(l:path . '/pylintrc')
|
|
|
|
\|| filereadable(l:path . '/.pylintrc')
|
2018-07-14 11:26:50 +00:00
|
|
|
\|| filereadable(l:path . '/Pipfile')
|
|
|
|
\|| filereadable(l:path . '/Pipfile.lock')
|
2020-10-15 11:35:47 +00:00
|
|
|
\|| filereadable(l:path . '/poetry.lock')
|
|
|
|
\|| filereadable(l:path . '/pyproject.toml')
|
2021-07-20 09:14:45 +00:00
|
|
|
\|| filereadable(l:path . '/.tool-versions')
|
2017-07-11 20:57:37 +00:00
|
|
|
return l:path
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2017-05-06 18:11:43 +00:00
|
|
|
" Given a buffer number, find the project root directory for Python.
|
|
|
|
" The root directory is defined as the first directory found while searching
|
|
|
|
" upwards through paths, including the current directory, until a path
|
2017-07-12 08:00:42 +00:00
|
|
|
" containing an init file (one from MANIFEST.in, setup.cfg, pytest.ini,
|
2017-12-17 12:36:07 +00:00
|
|
|
" tox.ini) is found. If it is not possible to find the project root directory
|
2017-07-12 08:00:42 +00:00
|
|
|
" via init file, then it will be defined as the first directory found
|
|
|
|
" searching upwards through paths, including the current directory, until no
|
|
|
|
" __init__.py files is found.
|
2017-05-06 18:11:43 +00:00
|
|
|
function! ale#python#FindProjectRoot(buffer) abort
|
2017-07-11 20:57:37 +00:00
|
|
|
let l:ini_root = ale#python#FindProjectRootIni(a:buffer)
|
|
|
|
|
|
|
|
if !empty(l:ini_root)
|
2019-02-06 18:05:13 +00:00
|
|
|
return l:ini_root
|
2017-07-11 20:57:37 +00:00
|
|
|
endif
|
|
|
|
|
2017-05-06 18:11:43 +00:00
|
|
|
for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
|
|
|
|
if !filereadable(l:path . '/__init__.py')
|
|
|
|
return l:path
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Given a buffer number, find a virtualenv path for Python.
|
|
|
|
function! ale#python#FindVirtualenv(buffer) abort
|
|
|
|
for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
|
2017-07-05 14:51:31 +00:00
|
|
|
" Skip empty path components returned in MSYS.
|
|
|
|
if empty(l:path)
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
|
2017-05-20 12:34:53 +00:00
|
|
|
for l:dirname in ale#Var(a:buffer, 'virtualenv_dir_names')
|
2017-10-01 17:47:54 +00:00
|
|
|
let l:venv_dir = ale#path#Simplify(
|
|
|
|
\ join([l:path, l:dirname], s:sep)
|
|
|
|
\)
|
|
|
|
let l:script_filename = ale#path#Simplify(
|
|
|
|
\ join([l:venv_dir, s:bin_dir, 'activate'], s:sep)
|
|
|
|
\)
|
|
|
|
|
|
|
|
if filereadable(l:script_filename)
|
2017-05-20 12:34:53 +00:00
|
|
|
return l:venv_dir
|
|
|
|
endif
|
|
|
|
endfor
|
2017-05-06 18:11:43 +00:00
|
|
|
endfor
|
|
|
|
|
2017-10-10 20:49:47 +00:00
|
|
|
return $VIRTUAL_ENV
|
2017-05-06 18:11:43 +00:00
|
|
|
endfunction
|
2017-06-18 10:03:31 +00:00
|
|
|
|
|
|
|
" Given a buffer number and a command name, find the path to the executable.
|
|
|
|
" First search on a virtualenv for Python, if nothing is found, try the global
|
|
|
|
" command. Returns an empty string if cannot find the executable
|
|
|
|
function! ale#python#FindExecutable(buffer, base_var_name, path_list) abort
|
|
|
|
if ale#Var(a:buffer, a:base_var_name . '_use_global')
|
|
|
|
return ale#Var(a:buffer, a:base_var_name . '_executable')
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:virtualenv = ale#python#FindVirtualenv(a:buffer)
|
|
|
|
|
|
|
|
if !empty(l:virtualenv)
|
|
|
|
for l:path in a:path_list
|
2017-10-01 17:47:54 +00:00
|
|
|
let l:ve_executable = ale#path#Simplify(
|
|
|
|
\ join([l:virtualenv, s:bin_dir, l:path], s:sep)
|
|
|
|
\)
|
2017-06-18 10:03:31 +00:00
|
|
|
|
2017-11-04 10:46:19 +00:00
|
|
|
if executable(l:ve_executable)
|
2017-06-18 10:03:31 +00:00
|
|
|
return l:ve_executable
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
|
2017-06-27 09:06:03 +00:00
|
|
|
return ale#Var(a:buffer, a:base_var_name . '_executable')
|
2017-06-18 10:03:31 +00:00
|
|
|
endfunction
|
Add python_[linter]_auto_pipenv options for python linters (fixes #1656)
When set to true, and the buffer is currently inside a pipenv,
GetExecutable will return "pipenv", which will trigger the existing
functionality to append the correct pipenv arguments to run each linter.
Defaults to false.
I was going to implement ale#python#PipenvPresent by invoking
`pipenv --venv` or `pipenv --where`, but it seemed to be abominably
slow, even to the point where the test suite wasn't even finishing
("Tried to run tests 3 times"). The diff is:
diff --git a/autoload/ale/python.vim b/autoload/ale/python.vim
index 7baae079..8c100d41 100644
--- a/autoload/ale/python.vim
+++ b/autoload/ale/python.vim
@@ -106,5 +106,9 @@ endfunction
" Detects whether a pipenv environment is present.
function! ale#python#PipenvPresent(buffer) abort
- return findfile('Pipfile.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''
+ let l:cd_string = ale#path#BufferCdString(a:buffer)
+ let l:output = systemlist(l:cd_string . 'pipenv --where')[0]
+ " `pipenv --where` returns the path to the dir containing the Pipfile
+ " if in a pipenv, or some error text otherwise.
+ return strpart(l:output, 0, 18) !=# "No Pipfile present"
endfunction
Using vim's `findfile` is much faster, behaves correctly in the majority
of situations, and also works reliably when the `pipenv` command doesn't
exist.
2018-07-12 03:02:23 +00:00
|
|
|
|
2019-02-08 21:44:34 +00:00
|
|
|
" Handle traceback.print_exception() output starting in the first a:limit lines.
|
|
|
|
function! ale#python#HandleTraceback(lines, limit) abort
|
|
|
|
let l:nlines = len(a:lines)
|
|
|
|
let l:limit = a:limit > l:nlines ? l:nlines : a:limit
|
|
|
|
let l:start = 0
|
|
|
|
|
|
|
|
while l:start < l:limit
|
|
|
|
if a:lines[l:start] is# 'Traceback (most recent call last):'
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:start += 1
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
if l:start >= l:limit
|
|
|
|
return []
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:end = l:start + 1
|
|
|
|
|
|
|
|
" Traceback entries are always prefixed with 2 spaces.
|
|
|
|
" SyntaxError marker (if present) is prefixed with at least 4 spaces.
|
|
|
|
" Final exc line starts with exception class name (never a space).
|
|
|
|
while l:end < l:nlines && a:lines[l:end][0] is# ' '
|
|
|
|
let l:end += 1
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
let l:exc_line = l:end < l:nlines
|
|
|
|
\ ? a:lines[l:end]
|
|
|
|
\ : 'An exception was thrown.'
|
|
|
|
|
|
|
|
return [{
|
|
|
|
\ 'lnum': 1,
|
|
|
|
\ 'text': l:exc_line . ' (See :ALEDetail)',
|
|
|
|
\ 'detail': join(a:lines[(l:start):(l:end)], "\n"),
|
|
|
|
\}]
|
|
|
|
endfunction
|
|
|
|
|
Add python_[linter]_auto_pipenv options for python linters (fixes #1656)
When set to true, and the buffer is currently inside a pipenv,
GetExecutable will return "pipenv", which will trigger the existing
functionality to append the correct pipenv arguments to run each linter.
Defaults to false.
I was going to implement ale#python#PipenvPresent by invoking
`pipenv --venv` or `pipenv --where`, but it seemed to be abominably
slow, even to the point where the test suite wasn't even finishing
("Tried to run tests 3 times"). The diff is:
diff --git a/autoload/ale/python.vim b/autoload/ale/python.vim
index 7baae079..8c100d41 100644
--- a/autoload/ale/python.vim
+++ b/autoload/ale/python.vim
@@ -106,5 +106,9 @@ endfunction
" Detects whether a pipenv environment is present.
function! ale#python#PipenvPresent(buffer) abort
- return findfile('Pipfile.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''
+ let l:cd_string = ale#path#BufferCdString(a:buffer)
+ let l:output = systemlist(l:cd_string . 'pipenv --where')[0]
+ " `pipenv --where` returns the path to the dir containing the Pipfile
+ " if in a pipenv, or some error text otherwise.
+ return strpart(l:output, 0, 18) !=# "No Pipfile present"
endfunction
Using vim's `findfile` is much faster, behaves correctly in the majority
of situations, and also works reliably when the `pipenv` command doesn't
exist.
2018-07-12 03:02:23 +00:00
|
|
|
" Detects whether a pipenv environment is present.
|
|
|
|
function! ale#python#PipenvPresent(buffer) abort
|
|
|
|
return findfile('Pipfile.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''
|
|
|
|
endfunction
|
2021-07-25 04:39:05 +00:00
|
|
|
|
|
|
|
" Detects whether a poetry environment is present.
|
|
|
|
function! ale#python#PoetryPresent(buffer) abort
|
|
|
|
return findfile('poetry.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''
|
|
|
|
endfunction
|