OLD | NEW |
1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # | 3 # |
4 # This file is part of logilab-common. | 4 # This file is part of logilab-common. |
5 # | 5 # |
6 # logilab-common is free software: you can redistribute it and/or modify it unde
r | 6 # logilab-common is free software: you can redistribute it and/or modify it unde
r |
7 # the terms of the GNU Lesser General Public License as published by the Free | 7 # the terms of the GNU Lesser General Public License as published by the Free |
8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y | 8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y |
9 # later version. | 9 # later version. |
10 # | 10 # |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 def __init__(self): | 27 def __init__(self): |
28 # quit and help are builtins | 28 # quit and help are builtins |
29 # CMD_MAP keys are commands, values are topics | 29 # CMD_MAP keys are commands, values are topics |
30 self.CMD_MAP['pionce'] = _("Sommeil") | 30 self.CMD_MAP['pionce'] = _("Sommeil") |
31 self.CMD_MAP['ronfle'] = _("Sommeil") | 31 self.CMD_MAP['ronfle'] = _("Sommeil") |
32 CLIHelper.__init__(self) | 32 CLIHelper.__init__(self) |
33 | 33 |
34 help_do_pionce = ("pionce", "pionce duree", _("met ton corps en veille")
) | 34 help_do_pionce = ("pionce", "pionce duree", _("met ton corps en veille")
) |
35 def do_pionce(self): | 35 def do_pionce(self): |
36 print 'nap is good' | 36 print('nap is good') |
37 | 37 |
38 help_do_ronfle = ("ronfle", "ronfle volume", _("met les autres en veille
")) | 38 help_do_ronfle = ("ronfle", "ronfle volume", _("met les autres en veille
")) |
39 def do_ronfle(self): | 39 def do_ronfle(self): |
40 print 'fuuuuuuuuuuuu rhhhhhrhrhrrh' | 40 print('fuuuuuuuuuuuu rhhhhhrhrhrrh') |
41 | 41 |
42 cl = BookShell() | 42 cl = BookShell() |
43 """ | 43 """ |
44 | 44 |
| 45 from __future__ import print_function |
| 46 |
45 __docformat__ = "restructuredtext en" | 47 __docformat__ = "restructuredtext en" |
46 | 48 |
47 from six.moves import builtins, input | 49 from six.moves import builtins, input |
48 | 50 |
49 if not hasattr(builtins, '_'): | 51 if not hasattr(builtins, '_'): |
50 builtins._ = str | 52 builtins._ = str |
51 | 53 |
52 | 54 |
53 def init_readline(complete_method, histfile=None): | 55 def init_readline(complete_method, histfile=None): |
54 """Init the readline library if available.""" | 56 """Init the readline library if available.""" |
55 try: | 57 try: |
56 import readline | 58 import readline |
57 readline.parse_and_bind("tab: complete") | 59 readline.parse_and_bind("tab: complete") |
58 readline.set_completer(complete_method) | 60 readline.set_completer(complete_method) |
59 string = readline.get_completer_delims().replace(':', '') | 61 string = readline.get_completer_delims().replace(':', '') |
60 readline.set_completer_delims(string) | 62 readline.set_completer_delims(string) |
61 if histfile is not None: | 63 if histfile is not None: |
62 try: | 64 try: |
63 readline.read_history_file(histfile) | 65 readline.read_history_file(histfile) |
64 except IOError: | 66 except IOError: |
65 pass | 67 pass |
66 import atexit | 68 import atexit |
67 atexit.register(readline.write_history_file, histfile) | 69 atexit.register(readline.write_history_file, histfile) |
68 except: | 70 except: |
69 print 'readline is not available :-(' | 71 print('readline is not available :-(') |
70 | 72 |
71 | 73 |
72 class Completer : | 74 class Completer : |
73 """Readline completer.""" | 75 """Readline completer.""" |
74 | 76 |
75 def __init__(self, commands): | 77 def __init__(self, commands): |
76 self.list = commands | 78 self.list = commands |
77 | 79 |
78 def complete(self, text, state): | 80 def complete(self, text, state): |
79 """Hook called by readline when <tab> is pressed.""" | 81 """Hook called by readline when <tab> is pressed.""" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 commands = [attr[3:] for attr in dir(self) if attr[:3] == 'do_'] | 152 commands = [attr[3:] for attr in dir(self) if attr[:3] == 'do_'] |
151 for command in commands: | 153 for command in commands: |
152 topic = self.CMD_MAP[command] | 154 topic = self.CMD_MAP[command] |
153 help_method = getattr(self, 'help_do_%s' % command) | 155 help_method = getattr(self, 'help_do_%s' % command) |
154 self._topics.setdefault(topic, []).append(help_method) | 156 self._topics.setdefault(topic, []).append(help_method) |
155 self.commands[self.CMD_PREFIX + command] = command | 157 self.commands[self.CMD_PREFIX + command] = command |
156 self._command_help[command] = help_method | 158 self._command_help[command] = help_method |
157 return self.commands.keys() | 159 return self.commands.keys() |
158 | 160 |
159 def _print_help(self, cmd, syntax, explanation): | 161 def _print_help(self, cmd, syntax, explanation): |
160 print _('Command %s') % cmd | 162 print(_('Command %s') % cmd) |
161 print _('Syntax: %s') % syntax | 163 print(_('Syntax: %s') % syntax) |
162 print '\t', explanation | 164 print('\t', explanation) |
163 print | 165 print() |
164 | 166 |
165 | 167 |
166 # predefined commands ##################################################### | 168 # predefined commands ##################################################### |
167 | 169 |
168 def do_help(self, command=None) : | 170 def do_help(self, command=None) : |
169 """base input of the help system""" | 171 """base input of the help system""" |
170 if command in self._command_help: | 172 if command in self._command_help: |
171 self._print_help(*self._command_help[command]) | 173 self._print_help(*self._command_help[command]) |
172 elif command is None or command not in self._topics: | 174 elif command is None or command not in self._topics: |
173 print _("Use help <topic> or help <command>.") | 175 print(_("Use help <topic> or help <command>.")) |
174 print _("Available topics are:") | 176 print(_("Available topics are:")) |
175 topics = sorted(self._topics.keys()) | 177 topics = sorted(self._topics.keys()) |
176 for topic in topics: | 178 for topic in topics: |
177 print '\t', topic | 179 print('\t', topic) |
178 print | 180 print() |
179 print _("Available commands are:") | 181 print(_("Available commands are:")) |
180 commands = self.commands.keys() | 182 commands = self.commands.keys() |
181 commands.sort() | 183 commands.sort() |
182 for command in commands: | 184 for command in commands: |
183 print '\t', command[len(self.CMD_PREFIX):] | 185 print('\t', command[len(self.CMD_PREFIX):]) |
184 | 186 |
185 else: | 187 else: |
186 print _('Available commands about %s:') % command | 188 print(_('Available commands about %s:') % command) |
187 print | 189 print |
188 for command_help_method in self._topics[command]: | 190 for command_help_method in self._topics[command]: |
189 try: | 191 try: |
190 if callable(command_help_method): | 192 if callable(command_help_method): |
191 self._print_help(*command_help_method()) | 193 self._print_help(*command_help_method()) |
192 else: | 194 else: |
193 self._print_help(*command_help_method) | 195 self._print_help(*command_help_method) |
194 except: | 196 except: |
195 import traceback | 197 import traceback |
196 traceback.print_exc() | 198 traceback.print_exc() |
197 print 'ERROR in help method %s'% ( | 199 print('ERROR in help method %s'% ( |
198 command_help_method.__name__) | 200 command_help_method.__name__)) |
199 | 201 |
200 help_do_help = ("help", "help [topic|command]", | 202 help_do_help = ("help", "help [topic|command]", |
201 _("print help message for the given topic/command or \ | 203 _("print help message for the given topic/command or \ |
202 available topics when no argument")) | 204 available topics when no argument")) |
203 | 205 |
204 def do_quit(self): | 206 def do_quit(self): |
205 """quit the CLI""" | 207 """quit the CLI""" |
206 raise EOFError() | 208 raise EOFError() |
207 | 209 |
208 def help_do_quit(self): | 210 def help_do_quit(self): |
209 return ("quit", "quit", _("quit the application")) | 211 return ("quit", "quit", _("quit the application")) |
OLD | NEW |