Get rid of overflows and 'L' suffixes with python-1.5
[polysh.git] / gsh / control_commands_helpers.py
blobd311ca5a5f04e65471019518ce0ea6a84417ac4c
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, 2007, 2008 Guillaume Chazarain <guichaz@gmail.com>
19 import os
20 from fnmatch import fnmatch
21 import readline
23 from gsh.host_syntax import expand_syntax
24 from gsh.console import console_output
25 from gsh import dispatchers
26 from gsh 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 for i in selected_shells(command):
32 if i.state != remote_dispatcher.STATE_DEAD and i.enabled != enable:
33 break
34 else:
35 toggle_shells('*', not enable)
37 for i in selected_shells(command):
38 if i.state != remote_dispatcher.STATE_DEAD:
39 i.set_enabled(enable)
41 def selected_shells(command):
42 """Iterator over the shells with names matching the patterns.
43 An empty patterns matches all the shells"""
44 selected = set()
45 instance_found = False
46 for pattern in (command or '*').split():
47 found = False
48 for expanded_pattern in expand_syntax(pattern):
49 for i in dispatchers.all_instances():
50 instance_found = True
51 if fnmatch(i.display_name, expanded_pattern):
52 found = True
53 if i not in selected:
54 selected.add(i)
55 yield i
56 if instance_found and not found:
57 console_output('%s not found\n' % pattern)
59 def complete_shells(line, text, predicate=lambda i: True):
60 """Return the shell names to include in the completion"""
61 res = [i.display_name + ' ' for i in dispatchers.all_instances() if \
62 i.display_name.startswith(text) and \
63 predicate(i) and \
64 ' ' + i.display_name + ' ' not in line]
65 return res
67 def expand_local_path(path):
68 return os.path.expanduser(os.path.expandvars(path) or '~')
70 def list_control_commands():
71 import gsh.control_commands
72 return [c[3:] for c in dir(gsh.control_commands) if c.startswith('do_')]
74 def get_control_command(name):
75 import gsh.control_commands
76 func = getattr(gsh.control_commands, 'do_' + name)
77 return func
79 def complete_control_command(line, text):
80 import gsh.control_commands
81 if readline.get_begidx() == 0:
82 # Completing control command name
83 cmds = list_control_commands()
84 prefix = text[1:]
85 matches = [':' + cmd + ' ' for cmd in cmds if cmd.startswith(prefix)]
86 else:
87 # Completing control command parameters
88 cmd = line.split()[0][1:]
89 def_compl = lambda line: []
90 compl_func = getattr(gsh.control_commands, 'complete_' + cmd, def_compl)
91 matches = compl_func(line, text)
92 return matches
94 def handle_control_command(line):
95 if not line:
96 return
97 cmd_name = line.split()[0]
98 try:
99 cmd_func = get_control_command(cmd_name)
100 except AttributeError:
101 console_output('Unknown control command: %s\n' % cmd_name)
102 else:
103 parameters = line[len(cmd_name) + 1:]
104 cmd_func(parameters)