def cleanup(self):
if self.old is not None:
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
python类TCSAFLUSH的实例源码
def __exit__(self, type, value, traceback):
# swith to normal terminal
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)
def run(self):
if os.name != "posix":
raise ValueError("Can only be run on Posix systems")
return
buffer = ""
# While PySerial would be preferable and more machine-independant,
# it does not support echo suppression
with open(self.dbgser_tty_name, 'r+') as dbgser:
# Config the debug serial port
oldattrs = termios.tcgetattr(dbgser)
newattrs = termios.tcgetattr(dbgser)
newattrs[4] = termios.B115200 # ispeed
newattrs[5] = termios.B115200 # ospeed
newattrs[3] = newattrs[3] & ~termios.ICANON & ~termios.ECHO
newattrs[6][termios.VMIN] = 0
newattrs[6][termios.VTIME] = 10
termios.tcsetattr(dbgser, termios.TCSANOW, newattrs)
# As long as we weren't asked to stop, try to capture dbgserial
# output and push each line up the result queue.
try:
while not self.stoprequest.isSet():
ch = dbgser.read(1)
if ch:
if ch == "\n":
# Push the line (sans newline) into our queue
# and clear the buffer for the next line.
self.result_q.put(buffer)
buffer = ""
elif ch != "\r":
buffer += ch
except IOError:
pass
finally:
# Restore previous settings
termios.tcsetattr(dbgser, termios.TCSAFLUSH, oldattrs)
# Flush any partial buffer
if buffer:
self.result_q.put(buffer)
def __exit__(self, type, value, traceback):
if isWindows:
pass
else:
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)
def set_normal_term(self):
''' Resets to normal terminal. On Windows this is a no-op.
'''
if os.name == 'nt':
pass
else:
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)
def cleanup(self):
if self.old is not None:
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
def cleanup(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
def set_normal_term(self):
''' Resets to normal terminal. On Windows this is a no-op.
'''
if os.name == 'nt':
pass
else:
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)
def __enter__(self):
# save the terminal settings
self.fd = sys.stdin.fileno()
self.new_term = termios.tcgetattr(self.fd)
self.old_term = termios.tcgetattr(self.fd)
# new terminal setting unbuffered
self.new_term[3] = (self.new_term[3] & ~termios.ICANON & ~termios.ECHO)
# switch to unbuffered terminal
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.new_term)
return self.query_keyboard
def __exit__(self, type, value, traceback):
# swith to normal terminal
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)
def set_normal_term(self):
''' Resets to normal terminal. On Windows this is a no-op.
'''
if os.name == 'nt':
pass
else:
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)
def cleanup(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
def cleanup(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
def warning(message, answer=None):
"""
Print warning message into srdErr and may can for answer
:param message: message
:param answer: list of supported options. Default is first item.
"""
c = ""
if sys.stderr.isatty():
sys.stderr.write("\n\x1b[92;01m%s " % message)
else:
sys.stderr.write("\n%s " % message)
if answer:
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
try:
while 1:
try:
c = sys.stdin.read(1)
break
except IOError:
pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
c = ("%s" % c).lower()
if sys.stderr.isatty():
sys.stderr.write(" %s\x1b[39;49;00m\n\n" % c)
else:
sys.stderr.write(" %s\n\n" % c)
if answer:
for it in answer:
if c in it:
return c
return answer.pop(0)
def cleanup(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
def cleanup(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
def cleanup(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
def set_normal_term(self):
''' Resets to normal terminal. On Windows this is a no-op.
'''
if os.name == 'nt':
pass
else:
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)
def cleanup(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
def cleanup(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)