def run_interactive_gtp_session(engine):
"""Run a GTP engine session on stdin and stdout, using readline.
engine -- Gtp_engine_protocol object
This enables readline tab-expansion, and command history in
~/.gomill-gtp-history (if readline is available).
Returns either when EOF is seen on stdin, or when the engine signals end of
session.
If stdin isn't a terminal, this is equivalent to run_gtp_session.
If a write fails with 'broken pipe', this raises ControllerDisconnected.
Note that this will propagate KeyboardInterrupt if the user presses ^C;
normally you'll want to handle this to avoid an ugly traceback.
"""
# readline doesn't do anything if stdin isn't a tty, but it's simplest to
# just not import it in that case.
try:
use_readline = os.isatty(sys.stdin.fileno())
if use_readline:
import readline
except Exception:
use_readline = False
if not use_readline:
run_gtp_session(engine, sys.stdin, sys.stdout)
return
def write(s):
sys.stdout.write(s)
sys.stdout.flush()
history_pathname = os.path.expanduser("~/.gomill-gtp-history")
readline.parse_and_bind("tab: complete")
old_completer = readline.get_completer()
old_delims = readline.get_completer_delims()
readline.set_completer(make_readline_completer(engine))
readline.set_completer_delims("")
try:
readline.read_history_file(history_pathname)
except EnvironmentError:
pass
_run_gtp_session(engine, raw_input, write)
try:
readline.write_history_file(history_pathname)
except EnvironmentError:
pass
readline.set_completer(old_completer)
readline.set_completer_delims(old_delims)
评论列表
文章目录