|
|
|
|
|
""" |
|
|
Demonstration script showing how to run a single test example. |
|
|
|
|
|
This script shows how to use the run_cli_redact function directly |
|
|
to test a specific CLI example. |
|
|
""" |
|
|
|
|
|
import os |
|
|
import shutil |
|
|
import sys |
|
|
import tempfile |
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
|
|
|
|
|
from test import run_cli_redact |
|
|
|
|
|
|
|
|
def demo_pdf_redaction(): |
|
|
"""Demonstrate how to run a single PDF redaction test.""" |
|
|
print("=== Demo: PDF Redaction with Default Settings ===") |
|
|
|
|
|
|
|
|
script_path = os.path.join( |
|
|
os.path.dirname(os.path.dirname(__file__)), "cli_redact.py" |
|
|
) |
|
|
input_file = os.path.join( |
|
|
os.path.dirname(os.path.dirname(__file__)), |
|
|
"example_data", |
|
|
"example_of_emails_sent_to_a_professor_before_applying.pdf", |
|
|
) |
|
|
output_dir = tempfile.mkdtemp(prefix="demo_output_") |
|
|
|
|
|
print(f"Script: {script_path}") |
|
|
print(f"Input: {input_file}") |
|
|
print(f"Output: {output_dir}") |
|
|
|
|
|
|
|
|
if not os.path.isfile(script_path): |
|
|
print(f"β Script not found: {script_path}") |
|
|
return False |
|
|
|
|
|
if not os.path.isfile(input_file): |
|
|
print(f"β Input file not found: {input_file}") |
|
|
print( |
|
|
"Make sure you have the example data files in the example_data/ directory" |
|
|
) |
|
|
return False |
|
|
|
|
|
try: |
|
|
|
|
|
print("\nRunning PDF redaction with default settings...") |
|
|
result = run_cli_redact( |
|
|
script_path=script_path, input_file=input_file, output_dir=output_dir |
|
|
) |
|
|
|
|
|
if result: |
|
|
print("β
Test completed successfully!") |
|
|
print(f"Check the output directory for results: {output_dir}") |
|
|
else: |
|
|
print("β Test failed!") |
|
|
|
|
|
return result |
|
|
|
|
|
finally: |
|
|
|
|
|
if os.path.exists(output_dir): |
|
|
shutil.rmtree(output_dir) |
|
|
print(f"Cleaned up: {output_dir}") |
|
|
|
|
|
|
|
|
def demo_csv_anonymisation(): |
|
|
"""Demonstrate how to run a CSV anonymisation test.""" |
|
|
print("\n=== Demo: CSV Anonymisation ===") |
|
|
|
|
|
|
|
|
script_path = os.path.join( |
|
|
os.path.dirname(os.path.dirname(__file__)), "cli_redact.py" |
|
|
) |
|
|
input_file = os.path.join( |
|
|
os.path.dirname(os.path.dirname(__file__)), |
|
|
"example_data", |
|
|
"combined_case_notes.csv", |
|
|
) |
|
|
output_dir = tempfile.mkdtemp(prefix="demo_output_") |
|
|
|
|
|
print(f"Script: {script_path}") |
|
|
print(f"Input: {input_file}") |
|
|
print(f"Output: {output_dir}") |
|
|
|
|
|
|
|
|
if not os.path.isfile(script_path): |
|
|
print(f"β Script not found: {script_path}") |
|
|
return False |
|
|
|
|
|
if not os.path.isfile(input_file): |
|
|
print(f"β Input file not found: {input_file}") |
|
|
print( |
|
|
"Make sure you have the example data files in the example_data/ directory" |
|
|
) |
|
|
return False |
|
|
|
|
|
try: |
|
|
|
|
|
print("\nRunning CSV anonymisation...") |
|
|
result = run_cli_redact( |
|
|
script_path=script_path, |
|
|
input_file=input_file, |
|
|
output_dir=output_dir, |
|
|
text_columns=["Case Note", "Client"], |
|
|
anon_strategy="replace_redacted", |
|
|
) |
|
|
|
|
|
if result: |
|
|
print("β
Test completed successfully!") |
|
|
print(f"Check the output directory for results: {output_dir}") |
|
|
else: |
|
|
print("β Test failed!") |
|
|
|
|
|
return result |
|
|
|
|
|
finally: |
|
|
|
|
|
if os.path.exists(output_dir): |
|
|
shutil.rmtree(output_dir) |
|
|
print(f"Cleaned up: {output_dir}") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
print("CLI Redaction Test Demo") |
|
|
print("=" * 50) |
|
|
print("This script demonstrates how to run individual tests.") |
|
|
print("=" * 50) |
|
|
|
|
|
|
|
|
success1 = demo_pdf_redaction() |
|
|
success2 = demo_csv_anonymisation() |
|
|
|
|
|
print("\n" + "=" * 50) |
|
|
print("Demo Summary") |
|
|
print("=" * 50) |
|
|
print(f"PDF Redaction: {'β
PASSED' if success1 else 'β FAILED'}") |
|
|
print(f"CSV Anonymisation: {'β
PASSED' if success2 else 'β FAILED'}") |
|
|
|
|
|
overall_success = success1 and success2 |
|
|
print(f"\nOverall: {'β
PASSED' if overall_success else 'β FAILED'}") |
|
|
|
|
|
sys.exit(0 if overall_success else 1) |
|
|
|