]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/python/bandit.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Do not set EDITOR/VISUAL for shell
[etc/vim.git] / .vim / bundle / ale / ale_linters / python / bandit.vim
1 " Author: Martino Pilia <martino.pilia@gmail.com>
2 " Description: bandit linting for python files
3
4 call ale#Set('python_bandit_executable', 'bandit')
5 call ale#Set('python_bandit_options', '')
6 call ale#Set('python_bandit_use_config', 1)
7 call ale#Set('python_bandit_use_global', get(g:, 'ale_use_global_executables', 0))
8 call ale#Set('python_bandit_auto_pipenv', 0)
9 call ale#Set('python_bandit_auto_poetry', 0)
10 call ale#Set('python_bandit_auto_uv', 0)
11
12 function! ale_linters#python#bandit#GetExecutable(buffer) abort
13     if (
14     \   ale#Var(a:buffer, 'python_auto_pipenv')
15     \   || ale#Var(a:buffer, 'python_bandit_auto_pipenv')
16     \) && ale#python#PipenvPresent(a:buffer)
17         return 'pipenv'
18     endif
19
20     if (
21     \   ale#Var(a:buffer, 'python_auto_poetry')
22     \   || ale#Var(a:buffer, 'python_bandit_auto_poetry')
23     \) && ale#python#PoetryPresent(a:buffer)
24         return 'poetry'
25     endif
26
27     if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_bandit_auto_uv'))
28     \ && ale#python#UvPresent(a:buffer)
29         return 'uv'
30     endif
31
32     return ale#python#FindExecutable(a:buffer, 'python_bandit', ['bandit'])
33 endfunction
34
35 function! ale_linters#python#bandit#GetCommand(buffer) abort
36     let l:executable = ale_linters#python#bandit#GetExecutable(a:buffer)
37     let l:flags = ' --format custom'
38     \   . ' --msg-template "{line}:{test_id}:{severity}:{msg}" '
39
40     if ale#Var(a:buffer, 'python_bandit_use_config')
41         let l:config_path = ale#path#FindNearestFile(a:buffer, '.bandit')
42
43         if !empty(l:config_path)
44             let l:flags = ' --ini ' . ale#Escape(l:config_path) . l:flags
45         endif
46     endif
47
48     let l:exec_args = l:executable =~? '\(pipenv\|poetry\|uv\)$'
49     \   ? ' run bandit'
50     \   : ''
51
52     return ale#Escape(l:executable) . l:exec_args
53     \   . l:flags
54     \   . ale#Pad(ale#Var(a:buffer, 'python_bandit_options'))
55     \   . ' -'
56 endfunction
57
58 function! ale_linters#python#bandit#Handle(buffer, lines) abort
59     " Custom format defined in GetCommand via --msg-template
60     let l:pattern = '\v^([0-9]+):(B[0-9]+):([A-Z]+):(.*)$'
61     let l:severity = {'LOW': 'I', 'MEDIUM': 'W', 'HIGH': 'E'}
62     let l:output = []
63
64     for l:match in ale#util#GetMatches(a:lines, l:pattern)
65         call add(l:output, {
66         \   'bufnr': a:buffer,
67         \   'lnum': str2nr(l:match[1]),
68         \   'code': l:match[2],
69         \   'type': l:severity[l:match[3]],
70         \   'text': l:match[4],
71         \})
72     endfor
73
74     return l:output
75 endfunction
76
77 call ale#linter#Define('python', {
78 \   'name': 'bandit',
79 \   'executable': function('ale_linters#python#bandit#GetExecutable'),
80 \   'command': function('ale_linters#python#bandit#GetCommand'),
81 \   'callback': 'ale_linters#python#bandit#Handle',
82 \})