added python tool inital version
This commit is contained in:
35
buzzer_tool/core/commands/info.py
Normal file
35
buzzer_tool/core/commands/info.py
Normal file
@@ -0,0 +1,35 @@
|
||||
# core/commands/info.py
|
||||
from core.connection import BuzzerError
|
||||
|
||||
def execute(conn) -> dict:
|
||||
"""Holt die Systeminformationen und gibt sie als strukturiertes Dictionary zurück."""
|
||||
lines = conn.send_command("info")
|
||||
if not lines:
|
||||
raise BuzzerError("Keine Antwort auf 'info' empfangen.")
|
||||
|
||||
parts = lines[0].split(';')
|
||||
if len(parts) != 5:
|
||||
raise BuzzerError(f"Unerwartetes Info-Format: {lines[0]}")
|
||||
|
||||
protocol_version = int(parts[0])
|
||||
if protocol_version != 1:
|
||||
raise BuzzerError(f"Inkompatibles Protokoll: Gerät nutzt v{protocol_version}, Host erwartet v1.")
|
||||
|
||||
app_version = parts[1]
|
||||
f_frsize = int(parts[2])
|
||||
f_blocks = int(parts[3])
|
||||
f_bfree = int(parts[4])
|
||||
|
||||
total_kb = (f_blocks * f_frsize) / 1024
|
||||
free_kb = (f_bfree * f_frsize) / 1024
|
||||
used_kb = total_kb - free_kb
|
||||
percent_used = (used_kb / total_kb) * 100 if total_kb > 0 else 0
|
||||
|
||||
return {
|
||||
"protocol_version": protocol_version,
|
||||
"app_version": app_version,
|
||||
"total_kb": total_kb,
|
||||
"free_kb": free_kb,
|
||||
"used_kb": used_kb,
|
||||
"percent_used": percent_used
|
||||
}
|
||||
84
buzzer_tool/core/commands/ls.py
Normal file
84
buzzer_tool/core/commands/ls.py
Normal file
@@ -0,0 +1,84 @@
|
||||
# core/commands/ls.py
|
||||
from core.connection import BuzzerError
|
||||
|
||||
def get_file_tree(conn, target_path="/", recursive=False) -> list:
|
||||
"""
|
||||
Liest das Dateisystem aus und gibt eine hierarchische Baumstruktur zurück.
|
||||
"""
|
||||
if not target_path.endswith('/'):
|
||||
target_path += '/'
|
||||
|
||||
cmd_path = target_path.rstrip('/') if target_path != '/' else '/'
|
||||
|
||||
try:
|
||||
lines = conn.send_command(f"ls {cmd_path}")
|
||||
except BuzzerError as e:
|
||||
return [{"type": "E", "name": f"Fehler beim Lesen: {e}", "path": target_path}]
|
||||
|
||||
nodes = []
|
||||
if not lines:
|
||||
return nodes
|
||||
|
||||
for line in lines:
|
||||
parts = line.split(',', 2)
|
||||
if len(parts) != 3:
|
||||
continue
|
||||
|
||||
entry_type, entry_size, entry_name = parts
|
||||
node = {
|
||||
"type": entry_type,
|
||||
"name": entry_name,
|
||||
"path": f"{target_path}{entry_name}"
|
||||
}
|
||||
|
||||
if entry_type == 'D':
|
||||
if recursive:
|
||||
# Rekursiver Aufruf auf dem Host für Unterverzeichnisse
|
||||
node["children"] = get_file_tree(conn, f"{target_path}{entry_name}/", recursive=True)
|
||||
else:
|
||||
node["children"] = []
|
||||
elif entry_type == 'F':
|
||||
node["size"] = int(entry_size)
|
||||
|
||||
nodes.append(node)
|
||||
|
||||
return nodes
|
||||
|
||||
def print_tree(nodes, prefix="", path=""):
|
||||
"""
|
||||
Gibt die Baumstruktur optisch formatiert auf der Konsole aus.
|
||||
"""
|
||||
if path:
|
||||
if path == "/":
|
||||
display_path = "💾 "+"/ (Root)"
|
||||
else:
|
||||
display_path = "📁 " + path
|
||||
print(f"{prefix}{display_path}")
|
||||
for i, node in enumerate(nodes):
|
||||
is_last = (i == len(nodes) - 1)
|
||||
connector = " └─" if is_last else " ├─"
|
||||
|
||||
if node["type"] == 'D':
|
||||
print(f"{prefix}{connector}📁 {node['name']}")
|
||||
extension = " " if is_last else " │ "
|
||||
if "children" in node and node["children"]:
|
||||
print_tree(node["children"], prefix + extension)
|
||||
elif node["type"] == 'F':
|
||||
size_kb = node["size"] / 1024
|
||||
# \033[90m macht den Text dunkelgrau, \033[0m setzt die Farbe zurück
|
||||
print(f"{prefix}{connector}📄 {node['name']} \033[90m({size_kb:.1f} KB)\033[0m")
|
||||
elif node["type"] == 'E':
|
||||
print(f"{prefix}{connector}❌ \033[31m{node['name']}\033[0m")
|
||||
|
||||
def get_flat_file_list(nodes) -> list:
|
||||
"""
|
||||
Wandelt die Baumstruktur in eine flache Liste von Dateipfaden um.
|
||||
Wird von 'rm -r' benötigt, um nacheinander alle Dateien zu löschen.
|
||||
"""
|
||||
flat_list = []
|
||||
for node in nodes:
|
||||
if node["type"] == 'F':
|
||||
flat_list.append(node)
|
||||
elif node["type"] == 'D' and "children" in node:
|
||||
flat_list.extend(get_flat_file_list(node["children"]))
|
||||
return flat_list
|
||||
0
buzzer_tool/core/commands/xy
Normal file
0
buzzer_tool/core/commands/xy
Normal file
Reference in New Issue
Block a user