Browse Source

Use exception to message config errors

pull/8/head
Richard Neumann 3 years ago
parent
commit
d5b61b1580
  1. 7
      rcon/config.py
  2. 10
      rcon/exceptions.py
  3. 10
      rcon/rconclt.py
  4. 10
      rcon/rconshell.py

7
rcon/config.py

@ -7,9 +7,10 @@ from getpass import getpass
from logging import getLogger from logging import getLogger
from os import getenv, name from os import getenv, name
from pathlib import Path from pathlib import Path
from sys import exit # pylint: disable=W0622
from typing import Iterable, NamedTuple, Optional, Union from typing import Iterable, NamedTuple, Optional, Union
from rcon.exceptions import ConfigReadError
__all__ = ['CONFIG_FILES', 'LOG_FORMAT', 'SERVERS', 'Config', 'from_args'] __all__ = ['CONFIG_FILES', 'LOG_FORMAT', 'SERVERS', 'Config', 'from_args']
@ -92,7 +93,7 @@ def from_args(args: Namespace) -> Config:
host, port, passwd = SERVERS[args.server] host, port, passwd = SERVERS[args.server]
except KeyError: except KeyError:
LOGGER.error('No such server: %s.', args.server) LOGGER.error('No such server: %s.', args.server)
exit(2) raise ConfigReadError(2)
if passwd is None: if passwd is None:
try: try:
@ -100,6 +101,6 @@ def from_args(args: Namespace) -> Config:
except (KeyboardInterrupt, EOFError): except (KeyboardInterrupt, EOFError):
print() print()
LOGGER.error('Aborted by user.') LOGGER.error('Aborted by user.')
exit(1) raise ConfigReadError(1)
return Config(host, port, passwd) return Config(host, port, passwd)

10
rcon/exceptions.py

@ -1,6 +1,14 @@
"""RCON exceptions.""" """RCON exceptions."""
__all__ = ['RequestIdMismatch', 'WrongPassword'] __all__ = ['ConfigReadError', 'RequestIdMismatch', 'WrongPassword']
class ConfigReadError(Exception):
"""Indicates an error while reading the configuration."""
def __init__(self, exit_code: int):
super().__init__()
self.exit_code = exit_code
class RequestIdMismatch(Exception): class RequestIdMismatch(Exception):

10
rcon/rconclt.py

@ -7,6 +7,7 @@ from pathlib import Path
from rcon.client import Client from rcon.client import Client
from rcon.config import CONFIG_FILES, LOG_FORMAT, from_args from rcon.config import CONFIG_FILES, LOG_FORMAT, from_args
from rcon.errorhandler import ErrorHandler from rcon.errorhandler import ErrorHandler
from rcon.exceptions import ConfigReadError
__all__ = ['main'] __all__ = ['main']
@ -39,12 +40,16 @@ def get_args() -> Namespace:
return parser.parse_args() return parser.parse_args()
def main() -> None: def main() -> int:
"""Runs the RCON client.""" """Runs the RCON client."""
args = get_args() args = get_args()
basicConfig(format=LOG_FORMAT, level=DEBUG if args.debug else INFO) basicConfig(format=LOG_FORMAT, level=DEBUG if args.debug else INFO)
host, port, passwd = from_args(args)
try:
host, port, passwd = from_args(args)
except ConfigReadError as cre:
return cre.exit_code
with ErrorHandler(LOGGER): with ErrorHandler(LOGGER):
with Client(host, port, timeout=args.timeout) as client: with Client(host, port, timeout=args.timeout) as client:
@ -52,3 +57,4 @@ def main() -> None:
text = client.run(args.command, *args.argument) text = client.run(args.command, *args.argument)
print(text, flush=True) print(text, flush=True)
return 0

10
rcon/rconshell.py

@ -7,6 +7,7 @@ from pathlib import Path
from rcon.config import CONFIG_FILES, LOG_FORMAT, from_args from rcon.config import CONFIG_FILES, LOG_FORMAT, from_args
from rcon.console import PROMPT, rconcmd from rcon.console import PROMPT, rconcmd
from rcon.errorhandler import ErrorHandler from rcon.errorhandler import ErrorHandler
from rcon.exceptions import ConfigReadError
from rcon.readline import CommandHistory from rcon.readline import CommandHistory
@ -32,17 +33,22 @@ def get_args() -> Namespace:
return parser.parse_args() return parser.parse_args()
def main() -> None: def main() -> 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)
if args.server: if args.server:
host, port, passwd = from_args(args) try:
host, port, passwd = from_args(args)
except ConfigReadError as cre:
return cre.exit_code
else: else:
host = port = passwd = None host = port = passwd = None
with ErrorHandler(LOGGER): with ErrorHandler(LOGGER):
with CommandHistory(LOGGER): with CommandHistory(LOGGER):
rconcmd(host, port, passwd, prompt=args.prompt) rconcmd(host, port, passwd, prompt=args.prompt)
return 0

Loading…
Cancel
Save