""" App Interface Module - Provides high-level file operations for applications """ import threading from typing import Dict, List, Optional class AppInterface: """ Provides a high-level interface for applications to interact with the virtual SSD. Simulates the interface that applications would use to save and read files. """ def __init__(self, virtual_os): self.virtual_os = virtual_os self.lock = threading.RLock() print("AppInterface initialized") def save(self, filename: str, data: bytes) -> bool: """ Save data to a file. This is the main entry point for applications. """ with self.lock: if not filename: print("Error: Filename cannot be empty") return False if not isinstance(data, bytes): print("Error: Data must be bytes") return False print(f"App saving file '{filename}' ({len(data)} bytes)") return self.virtual_os.write_file(filename, data) def load(self, filename: str) -> Optional[bytes]: """ Load data from a file. """ with self.lock: if not filename: print("Error: Filename cannot be empty") return None print(f"App loading file '{filename}'") return self.virtual_os.read_file(filename) def delete(self, filename: str) -> bool: """ Delete a file. """ with self.lock: if not filename: print("Error: Filename cannot be empty") return False print(f"App deleting file '{filename}'") return self.virtual_os.delete_file(filename) def list_files(self) -> Dict[str, Dict]: """ List all files in the system. """ with self.lock: return self.virtual_os.list_files() def get_file_info(self, filename: str) -> Optional[Dict]: """ Get detailed information about a file. """ with self.lock: return self.virtual_os.get_file_info(filename) def get_system_info(self) -> Dict: """ Get system information including capacity and usage. """ with self.lock: return self.virtual_os.get_os_stats() def sync(self): """ Force synchronization of all buffered data to storage. """ with self.lock: print("App requesting sync...") self.virtual_os.flush_buffers() def save_text_file(self, filename: str, text: str, encoding: str = 'utf-8') -> bool: """ Convenience method to save text data. """ try: data = text.encode(encoding) return self.save(filename, data) except Exception as e: print(f"Error encoding text: {e}") return False def load_text_file(self, filename: str, encoding: str = 'utf-8') -> Optional[str]: """ Convenience method to load text data. """ try: data = self.load(filename) if data is None: return None return data.decode(encoding) except Exception as e: print(f"Error decoding text: {e}") return None def get_capacity_info(self) -> Dict: """ Get capacity information in a user-friendly format. """ with self.lock: stats = self.get_system_info() fs_stats = stats.get("file_system", {}) total_gb = fs_stats.get("total_bytes", 0) / (1024**3) used_gb = fs_stats.get("used_bytes", 0) / (1024**3) free_gb = fs_stats.get("free_bytes", 0) / (1024**3) return { "total_gb": round(total_gb, 2), "used_gb": round(used_gb, 2), "free_gb": round(free_gb, 2), "usage_percent": round(fs_stats.get("usage_percent", 0), 2), "num_files": fs_stats.get("num_files", 0) } def shutdown(self): """ Shutdown the application interface. """ print("AppInterface shutdown complete") def __del__(self): try: self.shutdown() except: pass