completer.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. """Adapt readline completer interface to make ZMQ request."""
  3. # Copyright (c) IPython Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. try:
  6. from queue import Empty # Py 3
  7. except ImportError:
  8. from Queue import Empty # Py 2
  9. from traitlets.config import Configurable
  10. from traitlets import Float
  11. class ZMQCompleter(Configurable):
  12. """Client-side completion machinery.
  13. How it works: self.complete will be called multiple times, with
  14. state=0,1,2,... When state=0 it should compute ALL the completion matches,
  15. and then return them for each value of state."""
  16. timeout = Float(5.0, config=True, help='timeout before completion abort')
  17. def __init__(self, shell, client, config=None):
  18. super(ZMQCompleter,self).__init__(config=config)
  19. self.shell = shell
  20. self.client = client
  21. self.matches = []
  22. def complete_request(self, code, cursor_pos):
  23. # send completion request to kernel
  24. # Give the kernel up to 5s to respond
  25. msg_id = self.client.complete(
  26. code=code,
  27. cursor_pos=cursor_pos,
  28. )
  29. msg = self.client.shell_channel.get_msg(timeout=self.timeout)
  30. if msg['parent_header']['msg_id'] == msg_id:
  31. return msg['content']
  32. return {'matches': [], 'cursor_start': 0, 'cursor_end': 0,
  33. 'metadata': {}, 'status': 'ok'}