mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 01:00:29 +08:00
refactoring
This commit is contained in:
parent
002031baf1
commit
f652eca9c2
@ -117,8 +117,8 @@ def _parse_ps2_key(key: str) -> _Ps2Key:
|
||||
|
||||
def _read_keymap_csv(path: str) -> list[_KeyMapping]:
|
||||
keymap: list[_KeyMapping] = []
|
||||
with open(path) as keymap_file:
|
||||
for row in csv.DictReader(keymap_file):
|
||||
with open(path) as file:
|
||||
for row in csv.DictReader(file):
|
||||
if len(row) >= 6:
|
||||
keymap.append(_KeyMapping(
|
||||
web_name=row["web_name"],
|
||||
|
||||
@ -59,8 +59,8 @@ def _get_htpasswd_for_write(config: Section) -> Generator[passlib.apache.Htpassw
|
||||
try:
|
||||
try:
|
||||
st = os.stat(path)
|
||||
with open(path, "rb") as htpasswd_file:
|
||||
os.write(tmp_fd, htpasswd_file.read())
|
||||
with open(path, "rb") as file:
|
||||
os.write(tmp_fd, file.read())
|
||||
os.fchown(tmp_fd, st.st_uid, st.st_gid)
|
||||
os.fchmod(tmp_fd, st.st_mode)
|
||||
finally:
|
||||
|
||||
@ -40,8 +40,8 @@ class IpmiUserCredentials:
|
||||
class IpmiAuthManager:
|
||||
def __init__(self, path: str) -> None:
|
||||
self.__path = path
|
||||
with open(path) as passwd_file:
|
||||
self.__credentials = self.__parse_passwd_file(passwd_file.read().split("\n"))
|
||||
with open(path) as file:
|
||||
self.__credentials = self.__parse_passwd_file(file.read().split("\n"))
|
||||
|
||||
def __contains__(self, ipmi_user: str) -> bool:
|
||||
return (ipmi_user in self.__credentials)
|
||||
|
||||
@ -77,8 +77,8 @@ class AuthManager:
|
||||
assert self.__internal_service
|
||||
|
||||
if self.__totp_secret_path:
|
||||
with open(self.__totp_secret_path) as secret_file:
|
||||
secret = secret_file.read().strip()
|
||||
with open(self.__totp_secret_path) as file:
|
||||
secret = file.read().strip()
|
||||
if secret:
|
||||
code = passwd[-6:]
|
||||
if not pyotp.TOTP(secret).verify(code):
|
||||
|
||||
@ -80,14 +80,14 @@ def _write(path: str, value: (str | int), optional: bool=False) -> None:
|
||||
logger.info("WRITE --- [SKIPPED] %s", path)
|
||||
return
|
||||
logger.info("WRITE --- %s", path)
|
||||
with open(path, "w") as param_file:
|
||||
param_file.write(str(value))
|
||||
with open(path, "w") as file:
|
||||
file.write(str(value))
|
||||
|
||||
|
||||
def _write_bytes(path: str, data: bytes) -> None:
|
||||
get_logger().info("WRITE --- %s", path)
|
||||
with open(path, "wb") as param_file:
|
||||
param_file.write(data)
|
||||
with open(path, "wb") as file:
|
||||
file.write(data)
|
||||
|
||||
|
||||
def _check_config(config: Section) -> None:
|
||||
|
||||
@ -49,22 +49,22 @@ class _GadgetControl:
|
||||
def __udc_stopped(self) -> Generator[None, None, None]:
|
||||
udc = usb.find_udc(self.__udc)
|
||||
udc_path = usb.get_gadget_path(self.__gadget, usb.G_UDC)
|
||||
with open(udc_path) as udc_file:
|
||||
enabled = bool(udc_file.read().strip())
|
||||
with open(udc_path) as file:
|
||||
enabled = bool(file.read().strip())
|
||||
if enabled:
|
||||
with open(udc_path, "w") as udc_file:
|
||||
udc_file.write("\n")
|
||||
with open(udc_path, "w") as file:
|
||||
file.write("\n")
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
time.sleep(self.__init_delay)
|
||||
with open(udc_path, "w") as udc_file:
|
||||
udc_file.write(udc)
|
||||
with open(udc_path, "w") as file:
|
||||
file.write(udc)
|
||||
|
||||
def __read_metas(self) -> Generator[dict, None, None]:
|
||||
for meta_name in sorted(os.listdir(self.__meta_path)):
|
||||
with open(os.path.join(self.__meta_path, meta_name)) as meta_file:
|
||||
yield json.loads(meta_file.read())
|
||||
with open(os.path.join(self.__meta_path, meta_name)) as file:
|
||||
yield json.loads(file.read())
|
||||
|
||||
def enable_function(self, func: str) -> None:
|
||||
with self.__udc_stopped():
|
||||
|
||||
@ -39,14 +39,14 @@ def _get_param_path(gadget: str, instance: int, param: str) -> str:
|
||||
|
||||
|
||||
def _get_param(gadget: str, instance: int, param: str) -> str:
|
||||
with open(_get_param_path(gadget, instance, param)) as param_file:
|
||||
return param_file.read().strip()
|
||||
with open(_get_param_path(gadget, instance, param)) as file:
|
||||
return file.read().strip()
|
||||
|
||||
|
||||
def _set_param(gadget: str, instance: int, param: str, value: str) -> None:
|
||||
try:
|
||||
with open(_get_param_path(gadget, instance, param), "w") as param_file:
|
||||
param_file.write(value + "\n")
|
||||
with open(_get_param_path(gadget, instance, param), "w") as file:
|
||||
file.write(value + "\n")
|
||||
except OSError as err:
|
||||
if err.errno == errno.EBUSY:
|
||||
raise SystemExit(f"Can't change {param!r} value because device is locked: {err}")
|
||||
|
||||
@ -174,8 +174,8 @@ class _Service: # pylint: disable=too-many-instance-attributes
|
||||
real_driver = "rndis"
|
||||
path = usb.get_gadget_path(self.__gadget, usb.G_FUNCTIONS, f"{real_driver}.usb0/ifname")
|
||||
logger.info("Using OTG gadget %r ...", self.__gadget)
|
||||
with open(path) as iface_file:
|
||||
iface = iface_file.read().strip()
|
||||
with open(path) as file:
|
||||
iface = file.read().strip()
|
||||
logger.info("Using OTG Ethernet interface %r ...", iface)
|
||||
assert iface
|
||||
return iface
|
||||
|
||||
@ -44,13 +44,13 @@ def _join_rtc(rtc: int, key: str) -> str:
|
||||
|
||||
|
||||
def _read_int(rtc: int, key: str) -> int:
|
||||
with open(_join_rtc(rtc, key)) as value_file:
|
||||
return int(value_file.read().strip() or "0")
|
||||
with open(_join_rtc(rtc, key)) as file:
|
||||
return int(file.read().strip() or "0")
|
||||
|
||||
|
||||
def _write_int(rtc: int, key: str, value: int) -> None:
|
||||
with open(_join_rtc(rtc, key), "w") as value_file:
|
||||
value_file.write(str(value))
|
||||
with open(_join_rtc(rtc, key), "w") as file:
|
||||
file.write(str(value))
|
||||
|
||||
|
||||
def _reset_alarm(rtc: int, timeout: int) -> None:
|
||||
|
||||
@ -110,8 +110,8 @@ def _read_keyboard_layout(path: str) -> dict[int, list[At1Key]]: # Keysym to ev
|
||||
logger = get_logger(0)
|
||||
logger.info("Reading keyboard layout %s ...", path)
|
||||
|
||||
with open(path) as layout_file:
|
||||
lines = list(map(str.strip, layout_file.read().split("\n")))
|
||||
with open(path) as file:
|
||||
lines = list(map(str.strip, file.read().split("\n")))
|
||||
|
||||
layout: dict[int, list[At1Key]] = {}
|
||||
for (lineno, line) in enumerate(lines):
|
||||
|
||||
@ -426,8 +426,8 @@ class Plugin(BaseAuthService):
|
||||
assert user == user.strip()
|
||||
assert user
|
||||
try:
|
||||
with io.StringIO(_FREERADUIS_DICT) as dct_file:
|
||||
dct = pyrad.dictionary.Dictionary(dct_file)
|
||||
with io.StringIO(_FREERADUIS_DICT) as file:
|
||||
dct = pyrad.dictionary.Dictionary(file)
|
||||
client = pyrad.client.Client(
|
||||
server=self.__host,
|
||||
authport=self.__port,
|
||||
|
||||
@ -172,8 +172,8 @@ class BaseDeviceProcess(multiprocessing.Process): # pylint: disable=too-many-in
|
||||
return get_logger()
|
||||
|
||||
def __is_udc_configured(self) -> bool:
|
||||
with open(self.__udc_state_path) as udc_state_file:
|
||||
return (udc_state_file.read().strip().lower() == "configured")
|
||||
with open(self.__udc_state_path) as file:
|
||||
return (file.read().strip().lower() == "configured")
|
||||
|
||||
def __write_report(self, report: bytes) -> bool:
|
||||
assert report
|
||||
|
||||
@ -75,13 +75,13 @@ class Drive:
|
||||
# =====
|
||||
|
||||
def __get_param(self, param: str) -> str:
|
||||
with open(os.path.join(self.__lun_path, param)) as param_file:
|
||||
return param_file.read().strip()
|
||||
with open(os.path.join(self.__lun_path, param)) as file:
|
||||
return file.read().strip()
|
||||
|
||||
def __set_param(self, param: str, value: str) -> None:
|
||||
try:
|
||||
with open(os.path.join(self.__lun_path, param), "w") as param_file:
|
||||
param_file.write(value + "\n")
|
||||
with open(os.path.join(self.__lun_path, param), "w") as file:
|
||||
file.write(value + "\n")
|
||||
except OSError as err:
|
||||
if err.errno == errno.EBUSY:
|
||||
raise MsdDriveLockedError()
|
||||
|
||||
@ -129,12 +129,12 @@ class Plugin(BaseUserGpioDriver):
|
||||
self.__set_udc_enabled(True)
|
||||
|
||||
def __set_udc_enabled(self, enabled: bool) -> None:
|
||||
with open(self.__udc_path, "w") as udc_file:
|
||||
udc_file.write(self.__udc if enabled else "\n")
|
||||
with open(self.__udc_path, "w") as file:
|
||||
file.write(self.__udc if enabled else "\n")
|
||||
|
||||
def __is_udc_enabled(self) -> bool:
|
||||
with open(self.__udc_path) as udc_file:
|
||||
return bool(udc_file.read().strip())
|
||||
with open(self.__udc_path) as file:
|
||||
return bool(file.read().strip())
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"GPIO({self._instance_name})"
|
||||
|
||||
@ -35,9 +35,9 @@ from .. import tools
|
||||
|
||||
# =====
|
||||
def load_yaml_file(path: str) -> Any:
|
||||
with open(path) as yaml_file:
|
||||
with open(path) as file:
|
||||
try:
|
||||
return yaml.load(yaml_file, _YamlLoader)
|
||||
return yaml.load(file, _YamlLoader)
|
||||
except Exception as err:
|
||||
# Reraise internal exception as standard ValueError and show the incorrect file
|
||||
raise ValueError(f"Invalid YAML in the file {path!r}:\n{tools.efmt(err)}") from None
|
||||
@ -45,9 +45,9 @@ def load_yaml_file(path: str) -> Any:
|
||||
|
||||
# =====
|
||||
class _YamlLoader(yaml.SafeLoader):
|
||||
def __init__(self, yaml_file: IO) -> None:
|
||||
super().__init__(yaml_file)
|
||||
self.__root = os.path.dirname(yaml_file.name)
|
||||
def __init__(self, file: IO) -> None:
|
||||
super().__init__(file)
|
||||
self.__root = os.path.dirname(file.name)
|
||||
|
||||
def include(self, node: yaml.nodes.Node) -> Any:
|
||||
incs: list[str]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user