| | |
| | """ |
| | Script per processare file raw.in e raw.out, separando descrizione e codice. |
| | Genera tre file: description.txt, input.py e output.py |
| | """ |
| | import os |
| | import sys |
| | import argparse |
| | import re |
| |
|
| | def clear_text(text): |
| | """ |
| | Pulisce il testo dalle sequenze di escape preservando la formattazione originale. |
| | """ |
| | |
| | temp_newline = "TEMP_NEWLINE_PLACEHOLDER" |
| | temp_tab = "TEMP_TAB_PLACEHOLDER" |
| | |
| | |
| | text = text.replace("\\n", temp_newline) |
| | text = text.replace("\\t", temp_tab) |
| | |
| | |
| | text = text.replace("\\", "") |
| | |
| | |
| | text = text.replace(temp_newline, "\n") |
| | text = text.replace(temp_tab, "\t") |
| | |
| | |
| | text = text.rstrip('%') |
| | |
| | |
| | text = re.sub(r'\*(.*?)\*', r'\1', text) |
| | |
| | return text |
| |
|
| | def process_input_file(file_path): |
| | """ |
| | Processa il file di input contenente descrizione e codice. |
| | Restituisce una tupla (descrizione, codice). |
| | """ |
| | try: |
| | with open(file_path, 'r', encoding='utf-8') as file: |
| | content = file.read() |
| | |
| | |
| | parts = content.split("_BREAK_", 1) |
| | |
| | if len(parts) == 2: |
| | description = parts[0].strip() |
| | code = parts[1].strip() |
| | |
| | |
| | cleaned_code = clear_text(code) |
| | |
| | return (description, cleaned_code) |
| | else: |
| | |
| | print("Avviso: Nessun separatore _BREAK_ trovato. Considerando tutto il file come codice.") |
| | return ("", clear_text(content)) |
| | |
| | except Exception as e: |
| | print(f"Errore nella lettura o elaborazione del file di input {file_path}: {e}") |
| | return (None, None) |
| |
|
| | def process_output_file(file_path): |
| | """ |
| | Processa il file di output contenente solo codice. |
| | Restituisce il codice pulito. |
| | """ |
| | try: |
| | with open(file_path, 'r', encoding='utf-8') as file: |
| | content = file.read() |
| | |
| | |
| | cleaned_code = clear_text(content) |
| | |
| | return cleaned_code |
| | |
| | except Exception as e: |
| | print(f"Errore nella lettura o elaborazione del file di output {file_path}: {e}") |
| | return None |
| |
|
| | def save_files(description, input_code, output_code, output_folder): |
| | """ |
| | Salva i tre file di output nella cartella specificata. |
| | """ |
| | os.makedirs(output_folder, exist_ok=True) |
| | |
| | |
| | description_path = os.path.join(output_folder, "description.txt") |
| | with open(description_path, 'w', encoding='utf-8') as f: |
| | f.write(description) |
| | |
| | |
| | input_path = os.path.join(output_folder, "input.py") |
| | with open(input_path, 'w', encoding='utf-8') as f: |
| | f.write(input_code) |
| | |
| | |
| | output_path = os.path.join(output_folder, "output.py") |
| | with open(output_path, 'w', encoding='utf-8') as f: |
| | f.write(output_code) |
| | |
| | return description_path, input_path, output_path |
| |
|
| | def main(): |
| | |
| | parser = argparse.ArgumentParser(description="Processa file raw.in e raw.out per generare descrizione, input e output") |
| | parser.add_argument("raw_in", help="Percorso al file raw.in con descrizione e codice") |
| | parser.add_argument("raw_out", help="Percorso al file raw.out con solo codice") |
| | parser.add_argument("output_folder", help="Cartella dove salvare i file generati") |
| | parser.add_argument("--debug", action="store_true", help="Mostra dettagli di debug durante l'elaborazione") |
| | |
| | args = parser.parse_args() |
| | |
| | |
| | description, input_code = process_input_file(args.raw_in) |
| | if description is None or input_code is None: |
| | print(f"Impossibile processare il file di input: {args.raw_in}") |
| | return 1 |
| | |
| | |
| | output_code = process_output_file(args.raw_out) |
| | if output_code is None: |
| | print(f"Impossibile processare il file di output: {args.raw_out}") |
| | return 1 |
| | |
| | |
| | if args.debug: |
| | print("=== DESCRIZIONE ===") |
| | print(description) |
| | print("\n=== CODICE INPUT ===") |
| | print(input_code) |
| | print("\n=== CODICE OUTPUT ===") |
| | print(output_code) |
| | |
| | |
| | try: |
| | desc_path, in_path, out_path = save_files(description, input_code, output_code, args.output_folder) |
| | print(f"File generati con successo:") |
| | print(f" - Descrizione: {desc_path}") |
| | print(f" - Input: {in_path}") |
| | print(f" - Output: {out_path}") |
| | return 0 |
| | except Exception as e: |
| | print(f"Errore nel salvare i file: {e}") |
| | return 1 |
| |
|
| | if __name__ == "__main__": |
| | sys.exit(main()) |
| |
|