36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
# tool/core/cmd/stat.py
|
|
import struct
|
|
from core.utils import console, console_err
|
|
from core.protocol import COMMANDS, ERRORS
|
|
|
|
class stat:
|
|
def __init__(self, bus):
|
|
self.bus = bus
|
|
|
|
def get(self, path: str):
|
|
path_bytes = path.encode('utf-8')
|
|
payload = struct.pack('B', len(path_bytes)) + path_bytes
|
|
self.bus.send_request(COMMANDS['stat'], payload)
|
|
|
|
# 1 Byte Type + 4 Byte Size = 5
|
|
data = self.bus.receive_response(length=5)
|
|
|
|
if not data or data.get('type') == 'error':
|
|
return None
|
|
|
|
payload = data['data']
|
|
result = {
|
|
'is_directory': payload[0] == 1,
|
|
'size': struct.unpack('<I', payload[1:5])[0]
|
|
}
|
|
return result
|
|
|
|
def print(self, result, path: str):
|
|
if not result:
|
|
return
|
|
|
|
t_name = "📁 Verzeichnis" if result['is_directory'] else "📄 Datei"
|
|
console.print(f"[info_title]Stat[/info_title] für [info]{path}[/info]:")
|
|
console.print(f" • Typ: [info]{t_name}[/info]")
|
|
if not result['is_directory']:
|
|
console.print(f" • Grösse: [info]{result['size']/1024:.2f} KB[/info] ({result['size']} Bytes)") |