Last active
August 29, 2015 14:03
-
-
Save blink1073/6f11399968effabb6556 to your computer and use it in GitHub Desktop.
Spyderlib Asynchronous Introspection Mockup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
PLUGINS = ['jedi', 'rope', 'fallback'] | |
def get_introspection(plugin, task): | |
# TODO: make this a QThread | |
name, source_code, offset, filename = task | |
try: | |
func = getattr(plugin, name) | |
func(source_code, offset, filename) | |
except Exception: | |
print('send a qt signal back with the task and no result') | |
else: | |
print('send a qt signal back with the task and result') | |
class IntrospectionManager(object): | |
def __init__(self, editor_widget): | |
self.editor_widget = editor_widget | |
self.plugins = self.get_plugins() | |
self.incoming = [] | |
self.busy = False | |
self.plugin_num = 0 | |
def get_completion_list(self, source_code, offset, filename): | |
self.handle_task('get_completion_list', source_code, offset, | |
filename) | |
def get_calltip_and_docs(self, source_code, offset, filename): | |
self.handle_task('get_calltip_and_docs', source_code, offset, | |
filename) | |
def get_definition_location(self, source_code, offset, filename): | |
self.handle_task('get_definition_location', source_code, offset, | |
filename) | |
@staticmethod | |
def get_plugins(): | |
plugins = [] | |
for plugin_name in PLUGINS: | |
mod_name = plugin_name + '_plugin' | |
try: | |
mod = __import__('spyderlib.utils.introspection.' + mod_name, | |
fromlist=[mod_name]) | |
cls = getattr(mod, '%sPlugin' % plugin_name.capitalize()) | |
plugin = cls() | |
plugin.load_plugin(self.editor_widget) | |
except Exception: | |
pass | |
else: | |
plugins.append(plugin) | |
return plugins | |
def handle_task(self, name, source_code, offset, filename): | |
task = (name, source_code, offset, filename) | |
if self.busy: | |
self.incoming = task | |
return | |
plugin = self.plugins[self.plugin_num] | |
thread = get_introspection(plugin, task) | |
thread.setDaemon(True) | |
thread.start() | |
def handle_result(self, task, result): | |
if self.incoming: | |
incoming = self.incoming | |
self.incoming = [] | |
self.handle_task(*incoming) | |
return | |
name, source_code, offset, filename = task | |
if result is None: | |
if self.plugin_num == len(self.plugins) - 1: | |
self.busy = False | |
return | |
else: | |
self.plugin_num += 1 | |
self.busy = False | |
self.handle_task(task) | |
else: | |
self.plugin_num = 0 | |
self.handle_state(task, result) | |
def handle_state(self, task, result): | |
# TODO: get the current source_code, offset and filename | |
# if we can use the data, do so |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment