def __call__(self): from pycopia import tty fd = sys.stdin.fileno() mode = tty.tcgetattr(fd) try: while 1: print self.term try: c = sys.stdin.read(1) self.term.write(c) except EOFError: break finally: tty.tcsetattr(fd, tty.TCSANOW, mode)
def interact(self, msg=None, escape=None, cmd_interp=None): import select from pycopia import tty if escape is None: escape = chr(29) # ^] assert escape < " ", "escape key must be control character" self.cmd_interp = cmd_interp if self.cmd_interp: self.cmd_interp.set_session(self) from pycopia.CLI import CommandQuit print msg or "\nEntering interactive mode." print "Type ^%s to stop interacting." % (chr(ord(escape) | 0x40)) # save tty state and set to raw mode stdin_fd = sys.stdin.fileno() fo_fd = self.fileno() ttystate = tty.tcgetattr(stdin_fd) tty.setraw(stdin_fd) try: self._fo.restart(1) except AttributeError: pass while 1: try: rfd, wfd, xfd = select.select([fo_fd, stdin_fd], [], []) except select.error, errno: if errno[0] == EINTR: continue if fo_fd in rfd: try: text = self._fo.read(1) except (OSError, EOFError), err: tty.tcsetattr(stdin_fd, tty.TCSAFLUSH, ttystate) print '*** EOF ***' print err break if text: sys.stdout.write(text) sys.stdout.flush() else: break
def interact(self, msg=None, escape=None, cmd_interp=None): import select from pycopia import tty if escape is None: escape = chr(29) # ^] assert escape < " ", "escape key must be control character" self.cmd_interp = cmd_interp if self.cmd_interp: self.cmd_interp.set_session(self) from pycopia.CLI import CommandQuit print(msg or "\nEntering interactive mode.") print("Type ^%s to stop interacting." % (chr(ord(escape) | 0x40))) # save tty state and set to raw mode stdin_fd = sys.stdin.fileno() fo_fd = self.fileno() ttystate = tty.tcgetattr(stdin_fd) tty.setraw(stdin_fd) try: self._fo.restart(1) except AttributeError: pass while 1: try: rfd, wfd, xfd = select.select([fo_fd, stdin_fd], [], []) except select.error as err: if err.errno == EINTR: continue if fo_fd in rfd: try: text = self._fo.read(1) except (OSError, EOFError) as err: tty.tcsetattr(stdin_fd, tty.TCSAFLUSH, ttystate) print('*** EOF ***') print(err) break if text: sys.stdout.write(text) sys.stdout.flush() else: break if stdin_fd in rfd: char = sys.stdin.read(1) if char == escape: tty.tcsetattr(stdin_fd, tty.TCSAFLUSH, ttystate) if self.cmd_interp: try: self.cmd_interp.interact() tty.setraw(stdin_fd) except CommandQuit: break except: extype, exvalue, tb = sys.exc_info() sys.stderr.write("%s: %s\n" % (extype, exvalue)) sys.stderr.flush() tty.setraw(stdin_fd) else: break else: try: self.write(char) except: tty.tcsetattr(stdin_fd, tty.TCSAFLUSH, ttystate) extype, exvalue, tb = sys.exc_info() sys.stderr.write("%s: %s\n" % (extype, exvalue)) sys.stderr.flush() tty.setraw(stdin_fd) try: self._fo.restart(0) except AttributeError: pass
def interact(self, msg=None, escape=None, cmd_interp=None): import select from pycopia import tty if escape is None: escape = chr(29) # ^] assert escape < " ", "escape key must be control character" self.cmd_interp = cmd_interp if self.cmd_interp: self.cmd_interp.set_session(self) from pycopia.CLI import CommandQuit print (msg or "\nEntering interactive mode.") print ("Type ^%s to stop interacting." % (chr(ord(escape) | 0x40))) # save tty state and set to raw mode stdin_fd = sys.stdin.fileno() fo_fd = self.fileno() ttystate = tty.tcgetattr(stdin_fd) tty.setraw(stdin_fd) try: self._fo.restart(1) except AttributeError: pass while 1: try: rfd, wfd, xfd = select.select([fo_fd, stdin_fd], [], []) except select.error as err: if err.errno == EINTR: continue if fo_fd in rfd: try: text = self._fo.read(1) except (OSError, EOFError) as err: tty.tcsetattr(stdin_fd, tty.TCSAFLUSH, ttystate) print ('*** EOF ***') print (err) break if text: sys.stdout.write(text) sys.stdout.flush() else: break if stdin_fd in rfd: char = sys.stdin.read(1) if char == escape: tty.tcsetattr(stdin_fd, tty.TCSAFLUSH, ttystate) if self.cmd_interp: try: self.cmd_interp.interact() tty.setraw(stdin_fd) except CommandQuit: break except: extype, exvalue, tb = sys.exc_info() sys.stderr.write("%s: %s\n" % (extype, exvalue)) sys.stderr.flush() tty.setraw(stdin_fd) else: break else: try: self.write(char) except: tty.tcsetattr(stdin_fd, tty.TCSAFLUSH, ttystate) extype, exvalue, tb = sys.exc_info() sys.stderr.write("%s: %s\n" % (extype, exvalue)) sys.stderr.flush() tty.setraw(stdin_fd) try: self._fo.restart(0) except AttributeError: pass
try: text = self._fo.read(1) except (OSError, EOFError), err: tty.tcsetattr(stdin_fd, tty.TCSAFLUSH, ttystate) print '*** EOF ***' print err break if text: sys.stdout.write(text) sys.stdout.flush() else: break if stdin_fd in rfd: char = sys.stdin.read(1) if char == escape: tty.tcsetattr(stdin_fd, tty.TCSAFLUSH, ttystate) if self.cmd_interp: try: self.cmd_interp.interact() tty.setraw(stdin_fd) except CommandQuit: break except: extype, exvalue, tb = sys.exc_info() sys.stderr.write("%s: %s\n" % (extype, exvalue)) sys.stderr.flush() tty.setraw(stdin_fd) else: break else: try: