Skip to content

Commit

Permalink
Add full Python3 support
Browse files Browse the repository at this point in the history
Support Vim compiled with either +python or +python3.
Incorporates the pull request by @sankhesh .
  • Loading branch information
sankhesh authored and WolfgangMehner committed Oct 7, 2016
1 parent ff5a1c2 commit 6b2fae8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 26 deletions.
72 changes: 57 additions & 15 deletions autoload/do.vim
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ endfunction
" function will reload them.
"
function! do#ReloadOptions()
python do_async.reload_options()
if has("python")
python do_async.reload_options()
elseif has("python3")
python3 do_async.reload_options()
endif
endfunction

""
Expand Down Expand Up @@ -121,7 +125,11 @@ function! do#Execute(command, ...)
call do#error("Supplied command is empty")
else
let s:previous_command = l:command
python do_async.execute(vim.eval("l:command"), int(vim.eval("l:quiet")) == 1)
if has("python")
python do_async.execute(vim.eval("l:command"), int(vim.eval("l:quiet")) == 1)
elseif has("python3")
python3 do_async.execute(vim.eval("l:command"), int(vim.eval("l:quiet")) == 1)
endif
endif
endfunction

Expand Down Expand Up @@ -270,7 +278,11 @@ endfunction
" @param string file_path The path to the file to write log information
"
function! do#EnableLogger(file_path)
python do_async.enable_logger(vim.eval("a:file_path"))
if has("python")
python do_async.enable_logger(vim.eval("a:file_path"))
elseif has("python3")
python3 do_async.enable_logger(vim.eval("a:file_path"))
endif
endfunction

""
Expand All @@ -279,7 +291,11 @@ endfunction
" The command window details currently running and finished processes.
"
function! do#ToggleCommandWindow()
python do_async.toggle_command_window()
if has("python")
python do_async.toggle_command_window()
elseif has("python3")
python3 do_async.toggle_command_window()
endif
endfunction

""
Expand All @@ -288,7 +304,11 @@ endfunction
" Executed automatically via an autocommand.
"
function! do#MarkCommandWindowAsClosed()
python do_async.mark_command_window_as_closed()
if has("python")
python do_async.mark_command_window_as_closed()
elseif has("python3")
python3 do_async.mark_command_window_as_closed()
endif
endfunction

""
Expand All @@ -297,14 +317,22 @@ endfunction
" Executed automatically via an autocommand.
"
function! do#MarkProcessWindowAsClosed()
python do_async.mark_process_window_as_closed()
if has("python")
python do_async.mark_process_window_as_closed()
elseif has("python3")
python3 do_async.mark_process_window_as_closed()
endif
endfunction

""
" Trigger selection of a process in the command window.
"
function! do#ShowProcessFromCommandWindow()
python do_async.show_process_from_command_window()
if has("python")
python do_async.show_process_from_command_window()
elseif has("python3")
python3 do_async.show_process_from_command_window()
endif
endfunction

""
Expand All @@ -328,12 +356,21 @@ function! do#AssignAutocommands()
execute "nnoremap <silent> " . do#get("do_refresh_key") . " :call do#nop()<CR>"
execute "inoremap <silent> " . do#get("do_refresh_key") . ' <C-O>:call do#nop()<CR>'
augroup vim_do
au CursorHold * python do_async.check()
au CursorHoldI * python do_async.check()
au CursorMoved * python do_async.check()
au CursorMovedI * python do_async.check()
au FocusGained * python do_async.check()
au FocusLost * python do_async.check()
if has("python")
au CursorHold * python do_async.check()
au CursorHoldI * python do_async.check()
au CursorMoved * python do_async.check()
au CursorMovedI * python do_async.check()
au FocusGained * python do_async.check()
au FocusLost * python do_async.check()
elseif has("python3")
au CursorHold * python3 do_async.check()
au CursorHoldI * python3 do_async.check()
au CursorMoved * python3 do_async.check()
au CursorMovedI * python3 do_async.check()
au FocusGained * python3 do_async.check()
au FocusLost * python3 do_async.check()
endif
augroup END
let &updatetime=do#get("do_update_time")
endfunction
Expand Down Expand Up @@ -371,7 +408,12 @@ function! s:getVisualSelection()
endfunction

" Initialize do
python do_async = Do()
autocmd VimLeavePre * python do_async.stop()
if has("python")
python do_async = Do()
autocmd VimLeavePre * python do_async.stop()
elseif has("python3")
python3 do_async = Do()
autocmd VimLeavePre * python3 do_async.stop()
endif

" vim: expandtab: tabstop=4: shiftwidth=4
13 changes: 8 additions & 5 deletions autoload/python/async.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import Queue
import threading
import subprocess
import shlex
import select
import sys
from utils import log
import os

if sys.version[0] == '2':
import Queue as queue
else:
import queue as queue

class AsyncProcessReader(threading.Thread):
def __init__(self, process, output_q):
Expand Down Expand Up @@ -57,7 +60,7 @@ def _readfds(self):
class ProcessPool:
def __init__(self):
self.__threads = []
self.__output_q = Queue.Queue(0)
self.__output_q = queue.Queue(0)

def execute(self, cmd):
subproc = subprocess.Popen(cmd, shell=True,
Expand All @@ -80,7 +83,7 @@ def get_outputs(self):
try:
for result in iter(self.__output_q.get_nowait, None):
results.append(result)
except Queue.Empty:
except queue.Empty:
pass

return results
Expand Down
4 changes: 2 additions & 2 deletions autoload/python/do.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def add(self, process, pid):
self.__processes[pid] = process

def get_by_pid(self, pid):
return next((p for p in self.__processes.values() if p.get_pid() == pid), None)
return next((p for p in list(self.__processes.values()) if p.get_pid() == pid), None)

def update(self, pid, exit_status, stdout, stderr):
process = self.__processes[pid]
Expand All @@ -133,7 +133,7 @@ def all_finished(self):
return len(self.get_running()) == 0

def get_running(self):
return filter(lambda p: p.is_running(), self.__processes.values())
return [p for p in list(self.__processes.values()) if p.is_running()]

def kill_all(self):
for process in self.get_running():
Expand Down
2 changes: 1 addition & 1 deletion autoload/python/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __str__(self):
self.__process.get_status(),
self.__formatted_time(),
self.__process.get_pid())
max_length = max(map(len, values)) + 12
max_length = max(list(map(len, values))) + 12

title = "=" * max_length + "\n"
title += " [command] %s\n" % values[0]
Expand Down
4 changes: 2 additions & 2 deletions autoload/python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __init__(self,string,level = Logger.INFO):

@classmethod
def log(cls, string, level = Logger.INFO):
for k, l in cls.loggers.iteritems():
for k, l in cls.loggers.items():
l.log(string,level)

@classmethod
Expand All @@ -146,7 +146,7 @@ def remove_logger(cls, type):

@classmethod
def shutdown(cls):
for k, l in cls.loggers.iteritems():
for k, l in cls.loggers.items():
l.shutdown()
cls.loggers = {}

Expand Down
2 changes: 1 addition & 1 deletion plugin/do.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"=============================================================================
" }}}

if !has("python")
if !has("python") && !has("python3")
finish
endif

Expand Down

0 comments on commit 6b2fae8

Please sign in to comment.