本文整理汇总了Python中termios.tcgetattr函数的典型用法代码示例。如果您正苦于以下问题:Python tcgetattr函数的具体用法?Python tcgetattr怎么用?Python tcgetattr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了tcgetattr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __enter__
def __enter__(self):
# prepare standard file descriptors for raw manipulation
self.was_blocking = os.get_blocking(0)
os.set_blocking(0, False)
try:
self.terminal_attr_stdin = termios.tcgetattr(0)
self.terminal_attr_stdout = termios.tcgetattr(1)
self.terminal_attr_stderr = termios.tcgetattr(2)
tty.setraw(0)
tty.setraw(1)
tty.setraw(2)
except termios.error: # probably redirected
self.terminal_attr_stdin = None
# redirect standard file descriptors to new PTY
master, slave = pty.openpty()
os.set_blocking(master, False)
self.real_stdin = os.dup(0)
self.real_stdout = os.dup(1)
self.real_stderr = os.dup(2)
os.close(0)
os.close(1)
os.close(2)
os.dup2(slave, 0)
os.dup2(slave, 1)
os.dup2(slave, 2)
os.close(slave)
self.terminal_pipe = master
# start REPL in separate thread
threading.Thread(target=repl, args=(self,), daemon=True).start()
return self
示例2: getch
def getch():
"Returns a single character"
if getch.platform is None:
try:
# Window's python?
import msvcrt
getch.platform = 'windows'
except ImportError:
# Fallback...
try:
import tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
getch.platform = 'unix'
except termios.error:
getch.platform = 'dumb'
if getch.platform == 'windows':
import msvcrt
return msvcrt.getch()
elif getch.platform == 'unix':
import tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
else:
return sys.stdin.read(1).strip().lower()
示例3: setup
def setup(self):
self.old = termios.tcgetattr(self.fd)
new = termios.tcgetattr(self.fd)
new[3] = new[3] & ~termios.ICANON & ~termios.ECHO & ~termios.ISIG
new[6][termios.VMIN] = 1
new[6][termios.VTIME] = 0
termios.tcsetattr(self.fd, termios.TCSANOW, new)
示例4: run
def run(self):
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 not self.stopped():
try:
c = sys.stdin.read(16)
for fn in self.registered_fns[c]:
fn()
# print "Called registered func %s for %s" % (fn,repr(c))
#print 'Got char %s' % c
if c == 'q':
self.stop()
except IOError: pass
#time.sleep(.1)
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
print 'Keyboard handler stopped.'
示例5: _make_eof_intr
def _make_eof_intr():
"""Set constants _EOF and _INTR.
This avoids doing potentially costly operations on module load.
"""
global _EOF, _INTR
if (_EOF is not None) and (_INTR is not None):
return
# inherit EOF and INTR definitions from controlling process.
try:
from termios import VEOF, VINTR
try:
fd = sys.__stdin__.fileno()
except ValueError:
# ValueError: I/O operation on closed file
fd = sys.__stdout__.fileno()
intr = ord(termios.tcgetattr(fd)[6][VINTR])
eof = ord(termios.tcgetattr(fd)[6][VEOF])
except (ImportError, OSError, IOError, ValueError, termios.error):
# unless the controlling process is also not a terminal,
# such as cron(1), or when stdin and stdout are both closed.
# Fall-back to using CEOF and CINTR. There
try:
from termios import CEOF, CINTR
(intr, eof) = (CINTR, CEOF)
except ImportError:
# ^C, ^D
(intr, eof) = (3, 4)
_INTR = _byte(intr)
_EOF = _byte(eof)
示例6: warning
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 = ""
sys.stderr.write("\n\x1b[92;01m%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 = (u"%s" % c).lower()
sys.stderr.write(" %s\x1b[39;49;00m\n\n" % c)
if answer:
for it in answer:
if c in it:
return c
return answer.pop(0)
示例7: input_loop
def input_loop(url_prefix):
global plex_keys
while True:
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(3)
break
except IOError:
time.sleep(0.1)
pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
if c == 'q':
clear()
exit(0)
elif c == '*':
print "\n"
change_defaults_i()
del sys.argv[1:]
main()
elif c in plex_keys:
urllib2.urlopen(url_prefix + plex_keys[c]).read()
示例8: wait_key
def wait_key(message = ''):
''' Wait for a key press on the console and return it. '''
if message != '':
print (message)
result = None
if os.name == 'nt':
import msvcrt
result = msvcrt.getch()
else:
import termios
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)
try:
result = sys.stdin.read(1)
except IOError:
pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
return result
示例9: interact
def interact(self):
os.write(self.fd, self.readbuffer)
old = termios.tcgetattr(self.fd)
new = termios.tcgetattr(self.fd)
new[3] = new[3] & ~termios.ECHO
try:
tty.setraw(self.fd)
while True:
try:
rd, wd, ed = select.select([self.ptyfd, self.fd], [], [])
except select.error as e:
if e.args[0] == 4:
continue
else:
raise
for i in rd:
if i == self.ptyfd:
s = os.read(i, 1024)
os.write(self.fd, s)
elif i == self.fd:
s = os.read(i, 1024)
os.write(self.ptyfd, s)
except OSError as e:
if e.errno == 5:
# 使用 print() 会导致下一个 Python 提示符位置不对
os.write(self.fd, '已结束。\r\n'.encode())
else:
raise
finally:
termios.tcsetattr(self.fd, termios.TCSADRAIN, old)
示例10: __call__
def __call__(self):
import sys, termios, fcntl, os
fd = sys.stdin.fileno()
oldattr = termios.tcgetattr(fd)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
c = 0
try:
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
while True:
try:
c = sys.stdin.read(1)
break
except IOError:
pass
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, oldattr)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
return c
示例11: oldstat
def oldstat(pid, events, t, ann=None, norm=False, guest=False):
evcmd = ",".join(events)
if guest:
cmd = "sudo perf kvm stat"
else:
cmd = "sudo perf stat"
cmd += " -e {events} --log-fd 1 -x, -p {pid}".format(events=evcmd, pid=pid)
pid, fd = pty.fork()
if pid == 0:
osexec(cmd)
# fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("hhhh", 24, 80, 0, 0)) # resise terminal
# disable echo
flags = termios.tcgetattr(fd)
flags[3] &= ~termios.ECHO
termios.tcsetattr(fd, termios.TCSADRAIN, flags)
time.sleep(t)
ctrl_c = termios.tcgetattr(fd)[-1][termios.VINTR] # get Ctrl+C character
os.write(fd, ctrl_c)
os.waitpid(pid, 0)
raw = b""
while True:
try:
chunk = os.read(fd, BUF_SIZE)
except OSError:
break
if not chunk:
break
raw += chunk
return PerfData(raw, ann=ann, norm=norm)
示例12: __del__
def __del__(self):
if self.LOGGING:
# push any remaining output
line = self.serial.readline()
while line != "":
print line,
line = self.serial.readline()
if AUTOTEST:
if self.quiet:
sys = __import__("sys")
termios = __import__("termios")
else:
if self.quiet:
# for some reason, when our destructor is called sys and termios
# are no longer available. So we reimport them.
sys = __import__("sys")
termios = __import__("termios")
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] | termios.ECHO
termios.tcsetattr(fd, termios.TCSADRAIN, new)
self.serial.flushInput()
self.serial.flushOutput()
self.serial.close()
示例13: readKeys
def readKeys():
"""Waits for keyboard input and calls the method that is mapped to it."""
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO # dont echo back
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 True:
try:
key = sys.stdin.read(1)
if key in control_keys.keys():
control_keys[key]()
elif key == 'x':
pibot.stop()
sys.exit(0)
except IOError: pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
示例14: getChar
def getChar():
"""
!! DOES NOT WORK, IF STDIN WAS "CLOSED" WITH EOF IN THE SAME PROCESS !!
Stolen from:
http://python4fun.blogspot.de/2008/06/get-key-press-in-python.html
"""
if (os.name == "nt"):
import msvcrt
return msvcrt.getch()
else:
import termios, sys
TERMIOS = termios
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
new[6][TERMIOS.VMIN] = 1
new[6][TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
c = None
try:
c = os.read(fd, 1)
finally:
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
return c
示例15: getch
def getch():
'''
Get a character from stdin
'''
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 True:
try:
c = sys.stdin.read(1)
if c != "":
break
except IOError: pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
return c