Skip to content

Commit e10c7f7

Browse files
committed
Refactoring code checking
1 parent 99f3cda commit e10c7f7

File tree

5 files changed

+71
-62
lines changed

5 files changed

+71
-62
lines changed

autoload/pymode/lint.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fun! pymode#lint#Check() "{{{
1515
let g:pymode_lint_buffer = bufnr('%')
1616

1717
py from pymode import lint
18+
py queue.stop_queue(False)
1819
py lint.check_file()
1920

2021
endfunction " }}}

ftplugin/python/init-pymode.vim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
if exists('did_init_pymode_vim')
1+
if pymode#Default('g:pymode_init', 1)
22
finish
33
endif
4-
let did_init_pymode_vim = 1
54

65
let g:pymode_version = "0.6.17"
76

pylibs/pymode/interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get_current_buffer():
1818

1919

2020
def show_message(message):
21-
vim.command("call pymode#WideMessage('%s')" % message)
21+
vim.command("call pymode#WideMessage('{0}')".format(message))
2222

2323

2424
def command(cmd):

pylibs/pymode/lint.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,36 @@ def check_file():
2828
if s
2929
])
3030

31-
buffer = get_current_buffer()
31+
buf = get_current_buffer()
3232
complexity = int(get_option('lint_mccabe_complexity') or 0)
3333

34-
add_task(run_checkers, checkers=checkers, ignore=ignore,
35-
title='Code checking',
36-
callback=parse_result,
37-
buffer=buffer,
38-
select=select,
39-
complexity=complexity)
34+
add_task(
35+
run_checkers,
36+
callback=parse_result,
37+
title='Code checking',
4038

39+
checkers=checkers,
40+
ignore=ignore,
41+
buf=buf,
42+
select=select,
43+
complexity=complexity)
4144

42-
def run_checkers(task=None, checkers=None, ignore=None,
43-
buffer=None, select=None, complexity=None):
4445

45-
buffer = (task and task.buffer) or buffer
46-
filename = buffer.name
46+
def run_checkers(checkers=None, ignore=None, buf=None, select=None,
47+
complexity=None, callback=None):
48+
49+
filename = buf.name
4750
result = []
4851

4952
pylint_options = '--rcfile={0} -r n'.format(get_var('lint_config')).split()
5053

51-
result = run(filename, ignore=ignore, select=select, linters=checkers,
54+
return run(filename, ignore=ignore, select=select, linters=checkers,
5255
pylint=pylint_options, complexity=complexity)
5356

54-
if task:
55-
task.result = result
56-
task.finished = True
57-
task.done = 100
58-
5957

60-
def parse_result(result, bnum):
61-
command(('let g:qf_list = {0}'.format(repr(result)).replace('\': u', '\': ')))
62-
command('call pymode#lint#Parse({0})'.format(bnum))
58+
def parse_result(result, buf=None, **kwargs):
59+
command(('let g:qf_list = {0}'.format(repr(result)).replace(
60+
'\': u', '\': ')))
61+
command('call pymode#lint#Parse({0})'.format(buf.number))
6362

6463
# pymode:lint_ignore=W0622

pylibs/pymode/queue.py

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,70 @@
11
import threading
2+
from Queue import Queue, Empty
3+
24
from .interface import show_message
3-
import time
5+
6+
7+
MAX_LIFE = 60
8+
CHECK_INTERVAL = .2
9+
RESULTS = Queue()
410

511

612
class Task(threading.Thread):
713

8-
def __init__(self, buffer, callback=None, title=None, *args, **kwargs):
9-
self.buffer = buffer
10-
self._stop = threading.Event()
11-
self.result = None
12-
self.callback = callback
13-
self.done = 0
14-
self.finished = False
15-
self.title = title
14+
def __init__(self, *args, **kwargs):
15+
self.stop = threading.Event()
1616
threading.Thread.__init__(self, *args, **kwargs)
1717

1818
def run(self):
19-
" Run tasks. "
20-
self._Thread__target(task=self, *self._Thread__args, **self._Thread__kwargs)
21-
22-
# Wait for result parsing
23-
while not self.stopped():
24-
time.sleep(.2)
19+
""" Run the task.
20+
"""
21+
try:
22+
args, kwargs = self._Thread__args, self._Thread__kwargs
23+
checking = self._Thread__target(*args, **kwargs)
24+
if not self.stop.isSet():
25+
RESULTS.put((checking, args, kwargs))
2526

26-
def stop(self):
27-
" Stop task. "
28-
self._stop.set()
29-
30-
def stopped(self):
31-
return self._stop.isSet()
32-
33-
34-
def stop_queue():
35-
" Stop all tasks. "
36-
for thread in threading.enumerate():
37-
if isinstance(thread, Task):
38-
thread.stop()
39-
show_message('%s stopped.' % thread.title)
27+
except Exception as e:
28+
if not self.stop.isSet():
29+
RESULTS.put(e)
4030

4131

42-
def add_task(target, callback=None, buffer=None, title=None, *args, **kwargs):
32+
def add_task(target, title=None, *args, **kwargs):
4333
" Add all tasks. "
4434

45-
task = Task(buffer, title=title, target=target, callback=callback, args=args, kwargs=kwargs)
35+
task = Task(target=target, args=args, kwargs=kwargs)
4636
task.daemon = True
4737
task.start()
4838

49-
show_message('%s started.' % task.title)
39+
show_message('{0} started.'.format(title))
5040

5141

52-
def check_task():
53-
" Check tasks for result. "
42+
def stop_queue(message=True):
43+
""" Stop all tasks.
44+
"""
45+
with RESULTS.mutex:
46+
RESULTS.queue.clear()
47+
5448
for thread in threading.enumerate():
5549
if isinstance(thread, Task):
56-
if thread.finished:
57-
thread.stop()
58-
thread.callback(thread.result, thread.buffer.number)
59-
else:
60-
show_message('%s %s%%' % (thread.title, thread.done))
50+
thread.stop.set()
51+
if message:
52+
show_message("Task stopped.")
53+
54+
55+
def check_task():
56+
""" Checking running tasks.
57+
"""
58+
try:
59+
result = RESULTS.get(False)
60+
assert isinstance(result, tuple)
61+
except Empty:
62+
return False
63+
except AssertionError:
64+
return False
65+
result, _, kwargs = result
66+
callback = kwargs.pop('callback')
67+
callback(result, **kwargs)
68+
69+
70+
# lint_ignore=W0703

0 commit comments

Comments
 (0)