/
georgiy
/
work
Обзор
Документация
Войти
/
georgiy
/
work
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
task2/shell/shell.py
103 строки
3 KB
Georgiy Yankovskiy
task2
17 ноя 2024, 23:50
17 ноя 2024, 23:50
95703e6
Код
Авторство
О чём код?
from cmd import Cmd class Shell(Cmd): intro = None use_rawinput = False prompt = '' first = True def __init__(self, stdin=None, stdout=None): super(Shell, self).__init__(completekey='tab', stdin=stdin, stdout=stdout) def cmdloop(self, intro=None): """Repeatedly issue a prompt, accept input, parse an initial prefix off the received input, and dispatch to action methods, passing them the remainder of the line as argument. """ self.preloop() if self.use_rawinput and self.completekey: try: import readline self.old_completer = readline.get_completer() readline.set_completer(self.complete) readline.parse_and_bind(self.completekey+": complete") except ImportError: pass try: # if intro is not None: # self.intro = intro # if self.intro: # self.stdout.write(str(self.intro)+"\n") stop = None while not stop: if self.cmdqueue: line = self.cmdqueue.pop(0) else: if self.use_rawinput: try: line = input(self.prompt) except EOFError: line = 'EOF' else: # Hot fix # if self.first: # self.first = False # else: self.stdout.write(self.prompt) self.stdout.write('\x00\r\n') self.stdout.flush() line = self.stdin.readline() if not len(line): line = 'EOF' else: line = (line.rstrip('\r\n')) line = self.precmd(line) stop = self.onecmd(line) stop = self.postcmd(stop, line) self.postloop() finally: if self.use_rawinput and self.completekey: try: import readline readline.set_completer(self.old_completer) except ImportError: pass def print(self, value): if self.stdout and not self.stdout.closed: print(value) self.stdout.write(value) self.stdout.flush() def printline(self, value): self.print(value + '\n\r') def do_greet(self, arg): if arg: self.printline('Hey {0}! Nice to see you!'.format(arg)) else: self.printline('Hello there!') def do_exit(self, arg): self.printline('Disconnecting...') return True def precmd(self, line): return line def emptyline(self): self.print('\n\r') ## Overwritten def default(self, line): self.stdout.write('*** Unknown syntax: %s\n\r' % line) if __name__ == '__main__': Shell().cmdloop()