mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 00:51:53 +08:00
refactoring
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user