style: ruff format and the lint rules of the driver standard
Mechanical: ruff format, and ruff check --fix with the rule set every NAPALM driver uses (E, F, I, UP, B at line length 100, Python 3.10). What ruff cannot fix comes in the next commit.
This commit is contained in:
1 parent
7f2ae6ac58
commit
a212bc8715
14 files changed
+322
-239
No files matched your search
@@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
@@ -99,14 +98,16 @@ class OpenWrtPackageMixin:
|
||||
name = token[: vm.start()] if vm else token
|
||||
version = vm.group(1) if vm else ""
|
||||
origin = ""
|
||||
packages.append({
|
||||
"name": name,
|
||||
"version": version,
|
||||
"installed": True,
|
||||
"description": "",
|
||||
"size": 0,
|
||||
"source": origin,
|
||||
})
|
||||
packages.append(
|
||||
{
|
||||
"name": name,
|
||||
"version": version,
|
||||
"installed": True,
|
||||
"description": "",
|
||||
"size": 0,
|
||||
"source": origin,
|
||||
}
|
||||
)
|
||||
return sorted(packages, key=lambda p: p["name"].lower())
|
||||
|
||||
def _pm_type(self) -> str:
|
||||
@@ -121,6 +122,7 @@ class OpenWrtPackageMixin:
|
||||
index is populated (OpenWrt stores it in RAM and loses it on reboot).
|
||||
"""
|
||||
import shlex
|
||||
|
||||
safe_q = shlex.quote(query)
|
||||
if self._pm_type() == "apk":
|
||||
# Refresh index (no-ops if already current, safe to run every time)
|
||||
@@ -137,14 +139,16 @@ class OpenWrtPackageMixin:
|
||||
name, version, description = m.group(1), m.group(2), (m.group(3) or "")
|
||||
else:
|
||||
name, version, description = line, "", ""
|
||||
packages.append({
|
||||
"name": name,
|
||||
"version": version,
|
||||
"installed": name in installed,
|
||||
"description": description,
|
||||
"size": 0,
|
||||
"source": "",
|
||||
})
|
||||
packages.append(
|
||||
{
|
||||
"name": name,
|
||||
"version": version,
|
||||
"installed": name in installed,
|
||||
"description": description,
|
||||
"size": 0,
|
||||
"source": "",
|
||||
}
|
||||
)
|
||||
else:
|
||||
# opkg lists live in /var/opkg-lists/ (RAM) — cleared on reboot
|
||||
self._send_command("opkg update 2>/dev/null || true")
|
||||
@@ -159,14 +163,16 @@ class OpenWrtPackageMixin:
|
||||
name = parts[0].strip()
|
||||
version = parts[1].strip() if len(parts) > 1 else ""
|
||||
description = parts[2].strip() if len(parts) > 2 else ""
|
||||
packages.append({
|
||||
"name": name,
|
||||
"version": version,
|
||||
"installed": name in installed,
|
||||
"description": description,
|
||||
"size": 0,
|
||||
"source": "",
|
||||
})
|
||||
packages.append(
|
||||
{
|
||||
"name": name,
|
||||
"version": version,
|
||||
"installed": name in installed,
|
||||
"description": description,
|
||||
"size": 0,
|
||||
"source": "",
|
||||
}
|
||||
)
|
||||
return packages
|
||||
|
||||
@staticmethod
|
||||
@@ -175,27 +181,29 @@ class OpenWrtPackageMixin:
|
||||
# Strip CSI sequences (\x1b[...X), OSC, charset designations, and
|
||||
# 2-byte DEC private sequences like ESC 7 (cursor save) / ESC 8 (restore)
|
||||
cleaned = re.sub(
|
||||
r'\x1b(?:\[[0-9;?]*[a-zA-Z]|\][^\x07]*\x07|[()][0-9A-Za-z]|[\x30-\x7e])',
|
||||
'', raw,
|
||||
r"\x1b(?:\[[0-9;?]*[a-zA-Z]|\][^\x07]*\x07|[()][0-9A-Za-z]|[\x30-\x7e])",
|
||||
"",
|
||||
raw,
|
||||
)
|
||||
# After stripping cursor-save/restore sequences, apk progress updates
|
||||
# end up concatenated on a single line. Strip those inline patterns.
|
||||
cleaned = re.sub(r'\s*\d{1,3}%\s*#*', ' ', cleaned)
|
||||
cleaned = re.sub(r"\s*\d{1,3}%\s*#*", " ", cleaned)
|
||||
lines = []
|
||||
for segment in cleaned.split('\n'):
|
||||
for segment in cleaned.split("\n"):
|
||||
# \r overwrites the line; keep only the portion after the last \r
|
||||
part = segment.split('\r')[-1].strip()
|
||||
part = segment.split("\r")[-1].strip()
|
||||
if not part:
|
||||
continue
|
||||
# Drop pure progress-bar lines (only #, spaces, digits, %)
|
||||
if re.match(r'^[#\s\d%]*$', part):
|
||||
if re.match(r"^[#\s\d%]*$", part):
|
||||
continue
|
||||
lines.append(part)
|
||||
return '\n'.join(lines)
|
||||
return "\n".join(lines)
|
||||
|
||||
def install_package(self, name: str) -> dict[str, Any]:
|
||||
"""Install a package by name. Returns ``{"success": bool, "output": str}``."""
|
||||
import shlex
|
||||
|
||||
safe_name = shlex.quote(name)
|
||||
if self._pm_type() == "apk":
|
||||
raw = self._send_command(f"apk add {safe_name} 2>&1")
|
||||
@@ -209,6 +217,7 @@ class OpenWrtPackageMixin:
|
||||
def uninstall_package(self, name: str) -> dict[str, Any]:
|
||||
"""Remove a package by name. Returns ``{"success": bool, "output": str}``."""
|
||||
import shlex
|
||||
|
||||
safe_name = shlex.quote(name)
|
||||
if self._pm_type() == "apk":
|
||||
raw = self._send_command(f"apk del {safe_name} 2>&1")
|
||||
|
||||
Reference in new issue
Block a user