-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatpy.py
96 lines (90 loc) · 2.39 KB
/
catpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import sys
import subprocess
if len(sys.argv) != 2:
print("Error: has de proveir un fitxer a la comanda.")
print("Ús: catpy <nom_fitxer>")
else:
file_path = sys.argv[1]
referencies=[
[" i_també ", " and "],
[" també ", " and "],
[" com_a ", " as "],
[" anomenat ", " as "],
[" anomenat_com ", " as "],
[" anomenat_com_a ", " as "],
["afirma ", "assert "],
["atura", "break"],
["atur-et", "break"],
["trenca", "break"],
["frena", "break"],
["classe ", "class "],
["continua", "continue"],
["funció ", "def "],
["eliminar ", "del "],
["destruir ", "del "],
["del_contrari_si ", "elif "],
["en_cas_contrari_si ", "elif "],
["del_contrari", "else"],
["excepte ", "except "],
["Fals", "False"],
["fals", "False"],
["finalment", "finally"],
["per ", "for "],
["per_a ", "for "],
["des_de ", "from "],
["global ", "global "],
["si ", "if "],
["en_cas_que ", "if "],
["importa ", "import "],
[" dins ", " in "],
[" dins_de ", " in "],
[" en ", " in "],
["és ", "is "],
["lambda", "lambda"],
["Res", "None"],
["no_local ", "nonlocal "],
[" no_és ", " not "],
[" o_bé ", " or "],
[" o_per_el_contrari ", " or "],
["ignora", "pass"],
["pasa", "pass"],
["eleva ", "raise "],
["puja ", "raise "],
["retorna ", "return "],
["retornar ", "return "],
["Veritable", "True"],
["veritable", "True"],
["Cert", "True"],
["cert", "True"],
["Certament", "True"],
["certament", "True"],
["prova ", "try "],
["intenta ", "try "],
["mentres ", "while "],
[" amb ", " with "],
["rendiment ", "yield "],
["imprimir(", "print("],
["imprimeix(", "print("],
["fuet(", "print("],
["lallargaria_de(", "len("],
["allargaria(", "len("],
["allargada(", "len("],
["concatena(", "append("],
["enganxa(", "append("],
["afegeix(", "append("]
]
try:
with open(file_path, 'r') as file:
content = file.read()
for ref in referencies:
content = content.replace(ref[0], ref[1])
new_file_path = file_path.replace('.cat', '_executable.py')
with open(new_file_path, 'w') as new_file: new_file.write(content)
try:
result = subprocess.run(['python3', new_file_path], capture_output=True, text=True)
print(result.stdout if result.stderr == '' else result.stderr)
except:
print(f"error {result.stderr}")
os.remove(new_file_path)
except FileNotFoundError: print(f"Error: Fitxer '{file_path}' no trovat.")