Factor out logging
[polysh.git] / gsh / control_commands_helpers.py
blob3813563e869bd47db8e60e4633c7dfd0281006fe
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
27 def toggle_shells(command, enable):
28 """Enable or disable the specified shells"""
29 for i in selected_shells(command):
30 if i.active:
31 i.set_enabled(enable)
33 def selected_shells(command):
34 """Iterator over the shells with names matching the patterns.
35 An empty patterns matches all the shells"""
36 selected = set()
37 instance_found = False
38 for pattern in (command or '*').split():
39 found = False
40 for expanded_pattern in expand_syntax(pattern):
41 for i in dispatchers.all_instances():
42 instance_found = True
43 if fnmatch(i.display_name, expanded_pattern):
44 found = True
45 if i not in selected:
46 selected.add(i)
47 yield i
48 if instance_found and not found:
49 console_output('%s not found\n' % pattern)
51 def complete_shells(line, text, predicate=lambda i: True):
52 """Return the shell names to include in the completion"""
53 res = [i.display_name + ' ' for i in dispatchers.all_instances() if \
54 i.display_name.startswith(text) and \
55 predicate(i) and \
56 ' ' + i.display_name + ' ' not in line]
57 return res
59 def expand_local_path(path):
60 return os.path.expanduser(os.path.expandvars(path) or '~')
62 def list_control_commands():
63 import gsh.control_commands
64 return [c[3:] for c in dir(gsh.control_commands) if c.startswith('do_')]
66 def get_control_command(name):
67 import gsh.control_commands
68 func = getattr(gsh.control_commands, 'do_' + name)
69 return func
71 def complete_control_command(line, text):
72 import gsh.control_commands
73 if readline.get_begidx() == 0:
74 # Completing control command name
75 cmds = list_control_commands()
76 prefix = text[1:]
77 matches = [':' + cmd + ' ' for cmd in cmds if cmd.startswith(prefix)]
78 else:
79 # Completing control command parameters
80 cmd = line.split()[0][1:]
81 def_compl = lambda line: []
82 compl_func = getattr(gsh.control_commands, 'complete_' + cmd, def_compl)
83 matches = compl_func(line, text)
84 return matches
86 def handle_control_command(line):
87 cmd_name = line.split()[0]
88 try:
89 cmd_func = get_control_command(cmd_name)
90 except AttributeError:
91 console_output('Unknown control command: %s\n' % cmd_name)
92 else:
93 parameters = line[len(cmd_name) + 1:]
94 cmd_func(parameters)