From: Axy Date: Mon, 12 Jan 2026 15:36:37 +0000 (+0100) Subject: Refactor ex4 to separate test setup from crisis handling logic X-Git-Url: https://git.uwuaxy.net/sitemap.xml?a=commitdiff_plain;h=3f67de353cf6fb127dfa1b2401a82dbd57290106;p=axy%2Fft%2Fpython04.git Refactor ex4 to separate test setup from crisis handling logic --- diff --git a/ex4/ft_crisis_response.py b/ex4/ft_crisis_response.py index 9bb19bb..ce0e6cf 100644 --- a/ex4/ft_crisis_response.py +++ b/ex4/ft_crisis_response.py @@ -2,15 +2,6 @@ import os def crisis_handler(filename): - # Setup for permission error test - if filename == "classified_vault.txt": - try: - with open(filename, "w") as f: - f.write("TOP SECRET") - os.chmod(filename, 0o000) # Make unreadable - except OSError as e: - print(f"Setup error for {filename}: {e}") - # Determine log prefix if filename == "standard_archive.txt": print(f"ROUTINE ACCESS: Attempting access to '{filename}'...") @@ -20,9 +11,7 @@ def crisis_handler(filename): try: with open(filename, "r") as f: content = f.read().strip() - print( - f'SUCCESS: Archive recovered - "{content}"' - ) # Note: escaped quote here is intentional + print(f'SUCCESS: Archive recovered - "{content}"') print("STATUS: Normal operations resumed") except FileNotFoundError: print("RESPONSE: Archive not found in storage matrix") @@ -33,27 +22,37 @@ def crisis_handler(filename): except Exception as e: print(f"RESPONSE: Unexpected error: {e}") print("STATUS: Crisis handled") - finally: - # Cleanup for permission error test - if filename == "classified_vault.txt" and os.path.exists(filename): - try: - os.chmod(filename, 0o666) # Restore permissions - os.remove(filename) - except OSError: - pass def main(): print("=== CYBER ARCHIVES - CRISIS RESPONSE SYSTEM ===") - test_files = [ - "lost_archive.txt", - "classified_vault.txt", - "standard_archive.txt", - ] + # Scenario 1: Missing Archive + crisis_handler("lost_archive.txt") + + # Scenario 2: Security Violation (Simulated) + vault_filename = "classified_vault.txt" + try: + # Setup: Create and lock the vault + with open(vault_filename, "w") as f: + f.write("TOP SECRET") + os.chmod(vault_filename, 0o000) # Make unreadable + + crisis_handler(vault_filename) + + except OSError as e: + print(f"Test setup failed: {e}") + finally: + # Cleanup: Unlock and remove + if os.path.exists(vault_filename): + try: + os.chmod(vault_filename, 0o666) + os.remove(vault_filename) + except OSError: + pass - for filename in test_files: - crisis_handler(filename) + # Scenario 3: Standard Access + crisis_handler("standard_archive.txt") print("All crisis scenarios handled successfully. Archives secure.")