diff --git a/rcon/readline.py b/rcon/readline.py index e2a0e6d..9de0f6b 100644 --- a/rcon/readline.py +++ b/rcon/readline.py @@ -4,6 +4,11 @@ from logging import Logger from os import name from pathlib import Path +if name == 'posix': + from readline import read_history_file, write_history_file +else: + read_history_file = write_history_file = lambda _: None + __all__ = ['CommandHistory'] @@ -11,42 +16,30 @@ __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 __init__(self, logger: Logger): - self.logger = logger - - def __enter__(self): - try: - read_history_file(HIST_FILE) - except FileNotFoundError: - self.logger.warning('Could not find history file: %s', - HIST_FILE) - except PermissionError: - self.logger.error('Insufficient permissions to read: %s', - HIST_FILE) - - return self - - def __exit__(self, *_): - try: - write_history_file(HIST_FILE) - except PermissionError: - self.logger.error('Insufficient permissions to write: %s', - HIST_FILE) -else: - class CommandHistory: - """Context manager mockup for non-posix systems.""" - - def __init__(self, logger: Logger): - logger.warning('Command line history unavailable on this system.') - - def __enter__(self): - return self - - def __exit__(self, *_): - return None # Mockup for non-posix systems. +class CommandHistory: + """Context manager for the command line history.""" + + def __init__(self, logger: Logger): + """Sets the logger to use.""" + self.logger = logger + + def __enter__(self): + """Loads the history file.""" + try: + read_history_file(HIST_FILE) + except FileNotFoundError: + self.logger.warning('Could not find history file: %s', + HIST_FILE) + except PermissionError: + self.logger.error('Insufficient permissions to read: %s', + HIST_FILE) + + return self + + def __exit__(self, *_): + """Writes to the history file.""" + try: + write_history_file(HIST_FILE) + except PermissionError: + self.logger.error('Insufficient permissions to write: %s', + HIST_FILE)