Browse Source

Fixed readline history for Windows systems.

pull/2/head 1.1.2
Richard Neumann 4 years ago
parent
commit
9a99ea5434
  1. 21
      rcon/rconshell.py
  2. 33
      rcon/readline.py

21
rcon/rconshell.py

@ -1,16 +1,15 @@
"""An interactive RCON shell.""" """An interactive RCON shell."""
from argparse import ArgumentParser, Namespace from argparse import ArgumentParser, Namespace
from contextlib import suppress
from logging import INFO, basicConfig, getLogger from logging import INFO, basicConfig, getLogger
from pathlib import Path from pathlib import Path
from readline import read_history_file, write_history_file
from socket import timeout from socket import timeout
from sys import exit # pylint: disable=W0622 from sys import exit # pylint: disable=W0622
from rcon.errorhandler import ErrorHandler from rcon.errorhandler import ErrorHandler
from rcon.exceptions import RequestIdMismatch from rcon.exceptions import RequestIdMismatch
from rcon.rconclt import get_credentials from rcon.rconclt import get_credentials
from rcon.readline import CommandHistory
from rcon.config import CONFIG_FILE, LOG_FORMAT from rcon.config import CONFIG_FILE, LOG_FORMAT
from rcon.console import PROMPT, rconcmd from rcon.console import PROMPT, rconcmd
@ -23,7 +22,6 @@ ERRORS = (
(RequestIdMismatch, 'Unexpected request ID mismatch.', 5) (RequestIdMismatch, 'Unexpected request ID mismatch.', 5)
) )
LOGGER = getLogger('rconshell') LOGGER = getLogger('rconshell')
HIST_FILE = Path.home().joinpath('.rconshell_history')
def get_args() -> Namespace: def get_args() -> Namespace:
@ -38,24 +36,25 @@ def get_args() -> Namespace:
return parser.parse_args() return parser.parse_args()
def main(): def run() -> int:
"""Runs the RCON shell.""" """Runs the RCON shell."""
args = get_args() args = get_args()
basicConfig(level=INFO, format=LOG_FORMAT) basicConfig(level=INFO, format=LOG_FORMAT)
with suppress(FileNotFoundError, PermissionError):
read_history_file(HIST_FILE)
if args.server: if args.server:
host, port, passwd = get_credentials(args) host, port, passwd = get_credentials(args)
else: else:
host = port = passwd = None host = port = passwd = None
with ErrorHandler(ERRORS, LOGGER): with ErrorHandler(ERRORS, LOGGER):
exit_code = rconcmd(host, port, passwd, prompt=args.prompt) return rconcmd(host, port, passwd, prompt=args.prompt)
def main():
"""Wraps the run function."""
with suppress(PermissionError): with CommandHistory():
write_history_file(HIST_FILE) returncode = run()
exit(exit_code) exit(returncode)

33
rcon/readline.py

@ -0,0 +1,33 @@
"""Wrapper for readline support."""
from os import name
from pathlib import Path
__all__ = ['CommandHistory']
HIST_FILE = Path.home().joinpath('.rconshell_history')
if name == 'posix':
from readline import read_history_file, write_history_file
class CommandHistory:
"""Context manager for the command line history."""
def __enter__(self):
read_history_file()
return self
def __exit__(self, *_):
write_history_file(HIST_FILE)
else:
class CommandHistory:
"""Context manager for the command line history."""
def __enter__(self):
return self
def __exit__(self, *_):
pass
Loading…
Cancel
Save