refactoring

This commit is contained in:
Devaev Maxim 2020-05-25 01:49:39 +03:00
parent 499cbb0cc5
commit d5dca5a8b4
3 changed files with 17 additions and 17 deletions

View File

@ -28,8 +28,8 @@ from typing import Dict
# ===== # =====
class IpmiPasswdError(Exception): class IpmiPasswdError(Exception):
def __init__(self, path: str, number: int, msg: str) -> None: def __init__(self, path: str, lineno: int, msg: str) -> None:
super().__init__(f"Syntax error at {path}:{number}: {msg}") super().__init__(f"Syntax error at {path}:{lineno}: {msg}")
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True)
@ -57,30 +57,30 @@ class IpmiAuthManager:
def __parse_passwd_file(self, lines: List[str]) -> Dict[str, IpmiUserCredentials]: def __parse_passwd_file(self, lines: List[str]) -> Dict[str, IpmiUserCredentials]:
credentials: Dict[str, IpmiUserCredentials] = {} credentials: Dict[str, IpmiUserCredentials] = {}
for (number, line) in enumerate(lines): for (lineno, line) in enumerate(lines):
if len(line.strip()) == 0 or line.lstrip().startswith("#"): if len(line.strip()) == 0 or line.lstrip().startswith("#"):
continue continue
if " -> " not in line: if " -> " not in line:
raise IpmiPasswdError(self.__path, number, "Missing ' -> ' operator") raise IpmiPasswdError(self.__path, lineno, "Missing ' -> ' operator")
(left, right) = map(str.lstrip, line.split(" -> ", 1)) (left, right) = map(str.lstrip, line.split(" -> ", 1))
for (name, pair) in [("left", left), ("right", right)]: for (name, pair) in [("left", left), ("right", right)]:
if ":" not in pair: if ":" not in pair:
raise IpmiPasswdError(self.__path, number, f"Missing ':' operator in {name} credentials") raise IpmiPasswdError(self.__path, lineno, f"Missing ':' operator in {name} credentials")
(ipmi_user, ipmi_passwd) = left.split(":") (ipmi_user, ipmi_passwd) = left.split(":")
ipmi_user = ipmi_user.strip() ipmi_user = ipmi_user.strip()
if len(ipmi_user) == 0: if len(ipmi_user) == 0:
raise IpmiPasswdError(self.__path, number, "Empty IPMI user (left)") raise IpmiPasswdError(self.__path, lineno, "Empty IPMI user (left)")
(kvmd_user, kvmd_passwd) = right.split(":") (kvmd_user, kvmd_passwd) = right.split(":")
kvmd_user = kvmd_user.strip() kvmd_user = kvmd_user.strip()
if len(kvmd_user) == 0: if len(kvmd_user) == 0:
raise IpmiPasswdError(self.__path, number, "Empty KVMD user (left)") raise IpmiPasswdError(self.__path, lineno, "Empty KVMD user (left)")
if ipmi_user in credentials: if ipmi_user in credentials:
raise IpmiPasswdError(self.__path, number, f"Found duplicating user {ipmi_user!r} (left)") raise IpmiPasswdError(self.__path, lineno, f"Found duplicating user {ipmi_user!r} (left)")
credentials[ipmi_user] = IpmiUserCredentials( credentials[ipmi_user] = IpmiUserCredentials(
ipmi_user=ipmi_user, ipmi_user=ipmi_user,

View File

@ -32,8 +32,8 @@ from ...logging import get_logger
# ===== # =====
class VncAuthError(Exception): class VncAuthError(Exception):
def __init__(self, path: str, number: int, msg: str) -> None: def __init__(self, path: str, lineno: int, msg: str) -> None:
super().__init__(f"Syntax error at {path}:{number}: {msg}") super().__init__(f"Syntax error at {path}:{lineno}: {msg}")
# ===== # =====
@ -68,24 +68,24 @@ class VncAuthManager:
lines = (await vc_file.read()).split("\n") lines = (await vc_file.read()).split("\n")
credentials: Dict[str, VncAuthKvmdCredentials] = {} credentials: Dict[str, VncAuthKvmdCredentials] = {}
for (number, line) in enumerate(lines): for (lineno, line) in enumerate(lines):
if len(line.strip()) == 0 or line.lstrip().startswith("#"): if len(line.strip()) == 0 or line.lstrip().startswith("#"):
continue continue
if " -> " not in line: if " -> " not in line:
raise VncAuthError(self.__path, number, "Missing ' -> ' operator") raise VncAuthError(self.__path, lineno, "Missing ' -> ' operator")
(vnc_passwd, kvmd_userpass) = map(str.lstrip, line.split(" -> ", 1)) (vnc_passwd, kvmd_userpass) = map(str.lstrip, line.split(" -> ", 1))
if ":" not in kvmd_userpass: if ":" not in kvmd_userpass:
raise VncAuthError(self.__path, number, "Missing ':' operator in KVMD credentials (right part)") raise VncAuthError(self.__path, lineno, "Missing ':' operator in KVMD credentials (right part)")
(kvmd_user, kvmd_passwd) = kvmd_userpass.split(":") (kvmd_user, kvmd_passwd) = kvmd_userpass.split(":")
kvmd_user = kvmd_user.strip() kvmd_user = kvmd_user.strip()
if len(kvmd_user) == 0: if len(kvmd_user) == 0:
raise VncAuthError(self.__path, number, "Empty KVMD user (right part)") raise VncAuthError(self.__path, lineno, "Empty KVMD user (right part)")
if vnc_passwd in credentials: if vnc_passwd in credentials:
raise VncAuthError(self.__path, number, "Duplicating VNC password (left part)") raise VncAuthError(self.__path, lineno, "Duplicating VNC password (left part)")
credentials[vnc_passwd] = VncAuthKvmdCredentials(kvmd_user, kvmd_passwd) credentials[vnc_passwd] = VncAuthKvmdCredentials(kvmd_user, kvmd_passwd)
return credentials return credentials

View File

@ -107,7 +107,7 @@ def _read_keyboard_layout(path: str) -> Dict[int, At1Key]: # Keysym to evdev (a
lines = list(map(str.strip, layout_file.read().split("\n"))) lines = list(map(str.strip, layout_file.read().split("\n")))
layout: Dict[int, At1Key] = {} layout: Dict[int, At1Key] = {}
for (number, line) in enumerate(lines): for (lineno, line) in enumerate(lines):
if len(line) == 0 or line.startswith(("#", "map ", "include ")): if len(line) == 0 or line.startswith(("#", "map ", "include ")):
continue continue
@ -122,5 +122,5 @@ def _read_keyboard_layout(path: str) -> Dict[int, At1Key]: # Keysym to evdev (a
ctrl=("ctrl" in parts[2:]), ctrl=("ctrl" in parts[2:]),
) )
except ValueError as err: except ValueError as err:
logger.error("Syntax error at %s:%d: %s", path, number, err) logger.error("Syntax error at %s:%d: %s", path, lineno, err)
return layout return layout