"Fossies" - the Fresh Open Source Software Archive 
Member "polysh-polysh-0.13/polysh/control_commands_helpers.py" (11 May 2020, 4496 Bytes) of package /linux/privat/polysh-polysh-0.13.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "control_commands_helpers.py" see the
Fossies "Dox" file reference documentation.
1 """Polysh - Helpers for Control Commands
2
3 Copyright (c) 2006 Guillaume Chazarain <guichaz@gmail.com>
4 Copyright (c) 2018 InnoGames GmbH
5 """
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import os
20 from fnmatch import fnmatch
21 import readline
22 from typing import Iterator, List, Set, Callable
23
24 from polysh.host_syntax import expand_syntax
25 from polysh.console import console_output
26 from polysh import dispatchers
27 from polysh import remote_dispatcher
28
29
30 def toggle_shells(command: str, enable: bool) -> None:
31 """Enable or disable the specified shells. If the command would have
32 no effect, it changes all other shells to the inverse enable value."""
33 selection = list(selected_shells(command))
34 if command and command != '*' and selection:
35 for i in selection:
36 if i.state != remote_dispatcher.STATE_DEAD and i.enabled != enable:
37 break
38 else:
39 toggle_shells('*', not enable)
40
41 for i in selection:
42 if i.state != remote_dispatcher.STATE_DEAD:
43 i.set_enabled(enable)
44
45
46 def selected_shells(
47 command: str
48 ) -> Iterator[remote_dispatcher.RemoteDispatcher]:
49 """Iterator over the shells with names matching the patterns.
50 An empty patterns matches all the shells"""
51 if not command or command == '*':
52 for i in dispatchers.all_instances():
53 yield i
54 return
55 selected = set() # type: Set[remote_dispatcher.RemoteDispatcher]
56 instance_found = False
57 for pattern in command.split():
58 found = False
59 for expanded_pattern in expand_syntax(pattern):
60 for i in dispatchers.all_instances():
61 instance_found = True
62 if (
63 fnmatch(i.display_name, expanded_pattern) or
64 fnmatch(str(i.last_printed_line), expanded_pattern)
65 ):
66 found = True
67 if i not in selected:
68 selected.add(i)
69 yield i
70 if instance_found and not found:
71 console_output('{} not found\n'.format(pattern).encode())
72
73
74 def complete_shells(
75 line: str, text: str,
76 predicate: Callable = lambda i: True) -> List[str]:
77 """Return the shell names to include in the completion"""
78 res = [i.display_name + ' ' for i in dispatchers.all_instances() if
79 i.display_name.startswith(text) and
80 predicate(i) and
81 ' ' + i.display_name + ' ' not in line]
82 return res
83
84
85 def expand_local_path(path: str) -> str:
86 return os.path.expanduser(os.path.expandvars(path) or '~')
87
88
89 def list_control_commands() -> List[str]:
90 from polysh import control_commands
91 return [c[3:] for c in dir(control_commands) if c.startswith('do_')]
92
93
94 def get_control_command(name: str) -> Callable:
95 from polysh import control_commands
96 func = getattr(control_commands, 'do_' + name)
97 return func
98
99
100 def complete_control_command(line: str, text: str) -> List[str]:
101 from polysh import control_commands
102 if readline.get_begidx() == 0:
103 # Completing control command name
104 cmds = list_control_commands()
105 prefix = text[1:]
106 matches = [':' + cmd + ' ' for cmd in cmds if cmd.startswith(prefix)]
107 else:
108 # Completing control command parameters
109 cmd = line.split()[0][1:]
110
111 def def_compl(line: str) -> List:
112 return []
113 compl_func = getattr(control_commands, 'complete_' + cmd, def_compl)
114 matches = compl_func(line, text)
115 return matches
116
117
118 def handle_control_command(line: str) -> None:
119 if not line:
120 return
121 cmd_name = line.split()[0]
122 try:
123 cmd_func = get_control_command(cmd_name)
124 except AttributeError:
125 console_output(
126 'Unknown control command: {}\n'.format(cmd_name).encode())
127 else:
128 parameters = line[len(cmd_name) + 1:]
129 cmd_func(parameters)