Skip to content

Commit be60ea5

Browse files
committed
new file: scripts/_STASH/cc-restore-typesV0.py
modified: scripts/cc-restore-types.py
1 parent 7ce5b57 commit be60ea5

2 files changed

Lines changed: 129 additions & 8 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import os
4+
5+
6+
def extract_comments(lines):
7+
comments_map = {}
8+
in_block_comment = False
9+
comment_buffer = []
10+
11+
for line in lines:
12+
stripped = line.strip()
13+
14+
# Gestione commenti multiriga (JSDoc e /* */)
15+
if not in_block_comment and stripped.startswith("/*"):
16+
in_block_comment = True
17+
comment_buffer.append(line)
18+
if "*/" in stripped: # Commento aperto e chiuso sulla stessa riga
19+
in_block_comment = False
20+
continue
21+
22+
if in_block_comment:
23+
comment_buffer.append(line)
24+
if "*/" in stripped:
25+
in_block_comment = False
26+
continue
27+
28+
# Gestione commenti singola riga (solo se occupano tutta la riga)
29+
if stripped.startswith("//"):
30+
comment_buffer.append(line)
31+
continue
32+
33+
# Se abbiamo un buffer pieno e troviamo una riga di codice valida (l'ancora)
34+
if comment_buffer and stripped:
35+
anchor = stripped
36+
# Ignoriamo ancore troppo generiche (es. solo una graffa) per evitare falsi positivi
37+
if len(anchor) > 2:
38+
if anchor not in comments_map:
39+
comments_map[anchor] = []
40+
# Salviamo il blocco di commenti associato a questa riga di codice
41+
comments_map[anchor].append("".join(comment_buffer))
42+
43+
comment_buffer = [] # Reset del buffer
44+
45+
return comments_map
46+
47+
48+
def restore(orig_file, stripped_file, output_file):
49+
if not os.path.exists(orig_file) or not os.path.exists(stripped_file):
50+
print("❌ Errore: Uno dei file di input non esiste.")
51+
sys.exit(1)
52+
53+
with open(orig_file, "r", encoding="utf-8") as f:
54+
orig_lines = f.readlines()
55+
56+
with open(stripped_file, "r", encoding="utf-8") as f:
57+
stripped_lines = f.readlines()
58+
59+
print(f"[*] Analisi del file originale in corso...")
60+
comments_map = extract_comments(orig_lines)
61+
total_blocks = sum(len(v) for v in comments_map.values())
62+
print(f"[*] Trovati {total_blocks} blocchi di commenti/JSDoc mappati ad ancore.")
63+
64+
output_lines = []
65+
restored_count = 0
66+
67+
for line in stripped_lines:
68+
stripped = line.strip()
69+
70+
# Se la riga corrente del nuovo file corrisponde a un'ancora mappata
71+
if stripped in comments_map and comments_map[stripped]:
72+
# Pop() garantisce l'ordine cronologico se ci sono ancore identiche
73+
comment_block = comments_map[stripped].pop(0)
74+
output_lines.append(comment_block)
75+
restored_count += 1
76+
77+
output_lines.append(line)
78+
79+
with open(output_file, "w", encoding="utf-8") as f:
80+
f.writelines(output_lines)
81+
82+
print(f"[+] Operazione Completata!")
83+
print(f"[+] Ripristinati {restored_count} su {total_blocks} blocchi di commenti/tipi.")
84+
print(f"[+] Nuovo file salvato in: {output_file}")
85+
86+
87+
if __name__ == "__main__":
88+
if len(sys.argv) != 4:
89+
print("Uso: python3 cc-restore-types.py <file_vecchio_con_commenti.js> <file_nuovo_tagliato.js> <output_ripristinato.js>")
90+
print("Esempio: python3 cc-restore-types.py script_v1.js script_v2.js script_v2_commentato.js")
91+
sys.exit(1)
92+
93+
restore(sys.argv[1], sys.argv[2], sys.argv[3])

scripts/cc-restore-types.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33
import os
44

55

6+
# --- Colori ANSI per CLI ---
7+
class C:
8+
RESET = "\033[0m"
9+
BOLD = "\033[1m"
10+
CYAN = "\033[96m"
11+
GREEN = "\033[92m"
12+
YELLOW = "\033[93m"
13+
RED = "\033[91m"
14+
MAGENTA = "\033[95m"
15+
16+
17+
def print_usage():
18+
print(f"\n{C.BOLD}{C.MAGENTA}🌽 CodeCorn Type Restorer{C.RESET}")
19+
print(f"Ripristina i commenti e i JSDoc da uno script originale a uno sforbiciato.\n")
20+
print(f"{C.BOLD}Uso:{C.RESET}")
21+
print(f" {C.CYAN}python3 cc-restore-types.py{C.RESET} {C.YELLOW}<originale.js> <sforbiciato.js> <output.js>{C.RESET}\n")
22+
print(f"{C.BOLD}Opzioni:{C.RESET}")
23+
print(f" {C.GREEN}-h, --help{C.RESET} Mostra questo messaggio di aiuto ed esce.\n")
24+
print(f"{C.BOLD}Esempio:{C.RESET}")
25+
print(f" {C.CYAN}python3 cc-restore-types.py{C.RESET} {C.YELLOW}v1_full.js v2_min.js v2_restored.js{C.RESET}\n")
26+
27+
628
def extract_comments(lines):
729
comments_map = {}
830
in_block_comment = False
@@ -47,7 +69,7 @@ def extract_comments(lines):
4769

4870
def restore(orig_file, stripped_file, output_file):
4971
if not os.path.exists(orig_file) or not os.path.exists(stripped_file):
50-
print("❌ Errore: Uno dei file di input non esiste.")
72+
print(f"\n{C.BOLD}{C.RED}❌ Errore:{C.RESET} {C.RED}Uno dei file di input non esiste.{C.RESET}\n")
5173
sys.exit(1)
5274

5375
with open(orig_file, "r", encoding="utf-8") as f:
@@ -56,10 +78,10 @@ def restore(orig_file, stripped_file, output_file):
5678
with open(stripped_file, "r", encoding="utf-8") as f:
5779
stripped_lines = f.readlines()
5880

59-
print(f"[*] Analisi del file originale in corso...")
81+
print(f"\n{C.CYAN}[*] Analisi del file originale in corso...{C.RESET}")
6082
comments_map = extract_comments(orig_lines)
6183
total_blocks = sum(len(v) for v in comments_map.values())
62-
print(f"[*] Trovati {total_blocks} blocchi di commenti/JSDoc mappati ad ancore.")
84+
print(f"{C.CYAN}[*] Trovati {C.BOLD}{total_blocks}{C.RESET}{C.CYAN} blocchi di commenti/JSDoc mappati ad ancore.{C.RESET}")
6385

6486
output_lines = []
6587
restored_count = 0
@@ -79,15 +101,21 @@ def restore(orig_file, stripped_file, output_file):
79101
with open(output_file, "w", encoding="utf-8") as f:
80102
f.writelines(output_lines)
81103

82-
print(f"[+] Operazione Completata!")
83-
print(f"[+] Ripristinati {restored_count} su {total_blocks} blocchi di commenti/tipi.")
84-
print(f"[+] Nuovo file salvato in: {output_file}")
104+
print(f"\n{C.GREEN}{C.BOLD}[+] Operazione Completata!{C.RESET}")
105+
print(f"{C.GREEN}[+] Ripristinati {C.BOLD}{restored_count}{C.RESET}{C.GREEN} su {C.BOLD}{total_blocks}{C.RESET}{C.GREEN} blocchi di commenti/tipi.{C.RESET}")
106+
print(f"{C.GREEN}[+] Nuovo file salvato in: {C.BOLD}{C.YELLOW}{output_file}{C.RESET}\n")
85107

86108

87109
if __name__ == "__main__":
110+
# Gestione argomenti vuoti o flag di help
111+
if len(sys.argv) == 1 or sys.argv[1] in ("-h", "--help"):
112+
print_usage()
113+
sys.exit(0)
114+
115+
# Controllo numero argomenti
88116
if len(sys.argv) != 4:
89-
print("Uso: python3 cc-restore-types.py <file_vecchio_con_commenti.js> <file_nuovo_tagliato.js> <output_ripristinato.js>")
90-
print("Esempio: python3 cc-restore-types.py script_v1.js script_v2.js script_v2_commentato.js")
117+
print(f"\n{C.BOLD}{C.RED}❌ Errore:{C.RESET} {C.RED}Numero di argomenti non valido.{C.RESET}")
118+
print_usage()
91119
sys.exit(1)
92120

93121
restore(sys.argv[1], sys.argv[2], sys.argv[3])

0 commit comments

Comments
 (0)