Rename gsh to polysh.
[polysh.git] / polysh / control_commands_helpers.py
blob819cf27798a181e253343af58aaa89560462435d
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006 Guillaume Chazarain <guichaz@gmail.com>
19 import os
20 from fnmatch import fnmatch
21 import readline
23 from polysh.host_syntax import expand_syntax
24 from polysh.console import console_output
25 from polysh import dispatchers
26 from polysh import remote_dispatcher
28 def toggle_shells(command, enable):
29 """Enable or disable the specified shells. If the command would have
30 no effect, it changes all other shells to the inverse enable value."""
31 selection = list(selected_shells(command))
32 if command and command != '*' and selection:
33 for i in selection:
34 if i.state != remote_dispatcher.STATE_DEAD and i.enabled != enable:
35 break
36 else:
37 toggle_shells('*', not enable)
39 for i in selection:
40 if i.state != remote_dispatcher.STATE_DEAD:
41 i.set_enabled(enable)
43 def selected_shells(command):
44 """Iterator over the shells with names matching the patterns.
45 An empty patterns matches all the shells"""
46 if not command or command == '*':
47 for i in dispatchers.all_instances():
48 yield i
49 return
50 selected = set()
51 instance_found = False
52 for pattern in command.split():
53 found = False
54 for expanded_pattern in expand_syntax(pattern):
55 for i in dispatchers.all_instances():
56 instance_found = True
57 if fnmatch(i.display_name, expanded_pattern):
58 found = True
59 if i not in selected:
60 selected.add(i)
61 yield i
62 if instance_found and not found:
63 console_output('%s not found\n' % pattern)
65 def complete_shells(line, text, predicate=lambda i: True):
66 """Return the shell names to include in the completion"""
67 res = [i.display_name + ' ' for i in dispatchers.all_instances() if \
68 i.display_name.startswith(text) and \
69 predicate(i) and \
70 ' ' + i.display_name + ' ' not in line]
71 return res
73 def expand_local_path(path):
74 return os.path.expanduser(os.path.expandvars(path) or '~')
76 def list_control_commands():
77 from polysh import control_commands
78 return [c[3:] for c in dir(control_commands) if c.startswith('do_')]
80 def get_control_command(name):
81 from polysh import control_commands
82 func = getattr(control_commands, 'do_' + name)
83 return func
85 def complete_control_command(line, text):
86 from polysh import control_commands
87 if readline.get_begidx() == 0:
88 # Completing control command name
89 cmds = list_control_commands()
90 prefix = text[1:]
91 matches = [':' + cmd + ' ' for cmd in cmds if cmd.startswith(prefix)]
92 else:
93 # Completing control command parameters
94 cmd = line.split()[0][1:]
95 def_compl = lambda line: []
96 compl_func = getattr(control_commands, 'complete_' + cmd, def_compl)
97 matches = compl_func(line, text)
98 return matches
100 def handle_control_command(line):
101 if not line:
102 return
103 cmd_name = line.split()[0]
104 try:
105 cmd_func = get_control_command(cmd_name)
106 except AttributeError:
107 console_output('Unknown control command: %s\n' % cmd_name)
108 else:
109 parameters = line[len(cmd_name) + 1:]
110 cmd_func(parameters)