File: autocomplete.py

package info (click to toggle)
python-stem 1.2.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,568 kB
  • ctags: 2,036
  • sloc: python: 20,108; makefile: 127; sh: 3
file content (112 lines) | stat: -rw-r--r-- 2,925 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Tab completion for our interpreter prompt.
"""

from stem.interpreter import uses_settings

try:
  # added in python 3.2
  from functools import lru_cache
except ImportError:
  from stem.util.lru_cache import lru_cache


@uses_settings
def _get_commands(controller, config):
  """
  Provides commands recognized by tor.
  """

  commands = config.get('autocomplete', [])

  if controller is None:
    return commands

  # GETINFO commands. Lines are of the form '[option] -- [description]'. This
  # strips '*' from options that accept values.

  results = controller.get_info('info/names', None)

  if results:
    for line in results.splitlines():
      option = line.split(' ', 1)[0].rstrip('*')
      commands.append('GETINFO %s' % option)
  else:
    commands.append('GETINFO ')

  # GETCONF, SETCONF, and RESETCONF commands. Lines are of the form
  # '[option] [type]'.

  results = controller.get_info('config/names', None)

  if results:
    for line in results.splitlines():
      option = line.split(' ', 1)[0]

      commands.append('GETCONF %s' % option)
      commands.append('SETCONF %s' % option)
      commands.append('RESETCONF %s' % option)
  else:
    commands += ['GETCONF ', 'SETCONF ', 'RESETCONF ']

  # SETEVENT, USEFEATURE, and SIGNAL commands. For each of these the GETINFO
  # results are simply a space separated lists of the values they can have.

  options = (
    ('SETEVENTS ', 'events/names'),
    ('USEFEATURE ', 'features/names'),
    ('SIGNAL ', 'signal/names'),
  )

  for prefix, getinfo_cmd in options:
    results = controller.get_info(getinfo_cmd, None)

    if results:
      commands += [prefix + value for value in results.split()]
    else:
      commands.append(prefix)

  # Adds /help commands.

  usage_info = config.get('help.usage', {})

  for cmd in usage_info.keys():
    commands.append('/help ' + cmd)

  return commands


class Autocompleter(object):
  def __init__(self, controller):
    self._commands = _get_commands(controller)

  @lru_cache()
  def matches(self, text):
    """
    Provides autocompletion matches for the given text.

    :param str text: text to check for autocompletion matches with

    :returns: **list** with possible matches
    """

    lowercase_text = text.lower()
    return [cmd for cmd in self._commands if cmd.lower().startswith(lowercase_text)]

  def complete(self, text, state):
    """
    Provides case insensetive autocompletion options, acting as a functor for
    the readlines set_completer function.

    :param str text: text to check for autocompletion matches with
    :param int state: index of result to be provided, readline fetches matches
      until this function provides None

    :returns: **str** with the autocompletion match, **None** if eithe none
      exists or state is higher than our number of matches
    """

    try:
      return self.matches(text)[state]
    except IndexError:
      return None