-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.py
163 lines (130 loc) · 3.65 KB
/
compiler.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
import datetime
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# Variables
out = None
# Utils ----------------------------------------------------------
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
# Classes ----------------------------------------------------------------
class colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class MainFileManager():
def __init__(self):
self.watch_dirs = ['out', 'src']
self.name = ""
self.command = ""
self.extension = ""
self.comment = ""
def checkExtension(self, name):
self.name = name
if(self.name == ""):
self.name = "main.cs"
try:
self.extension = str(self.name.split('.')[1]).lower()
except IndexError:
print(
'Please restart the program and enter the name of the final ' +
'file as the example, with the extension.'
)
quit()
def language(self):
if(self.extension.lower() in ['sboticsr', 're']):
self.command, self.extension, self.comment = (
'importar', self.extension, '#'
)
else:
self.command, self.extension, self.comment = (
'import', self.extension, '//'
)
def diretory(self):
root = os.listdir('./')
for folder in self.watch_dirs:
if folder not in root:
os.mkdir(f'./{folder}')
class ImporterManager():
def include(self, import_name):
FinalInclude = ""
print(f'{colors.WARNING}importing: {import_name.replace("./src/", "")}{colors.ENDC}')
try:
file_to_import = open(import_name, 'r', encoding='utf-8')
except FileNotFoundError:
print(f'File {import_name} not found')
return
for line in file_to_import.readlines():
if line.find(MainFile.command) > -1:
splittedLine = line.split("(")
if MainFile.command in splittedLine[0].split(" "):
file_imported = line.replace(MainFile.command, '')
file_imported = file_imported[file_imported.find('("')+2:file_imported.find('")')]
file_imported = (
f'./src/{file_imported}.' +
f'{MainFile.extension}'
)
FinalInclude += self.include(file_imported)
else:
FinalInclude += line
else:
FinalInclude += line
file_to_import.close()
return FinalInclude
# Instances ----------------------------------------------------------------
MainFile = MainFileManager()
MainFile.checkExtension(
input(
'Enter the name of the final file ' +
'(with the extension, such as "main.cs"), \n' +
'if no filename is entered, "main.cs" will be used: '
)
)
MainFile.diretory()
MainFile.language()
Importer = ImporterManager()
# Init ----------------------------------------------------------------
clear()
print("Waiting modifications...")
def Process():
global out
clear()
data = datetime.datetime.now().replace(microsecond=0)
out = open(f'./out/{MainFile.name}', 'w', encoding='utf-8')
out.write(Importer.include(f'./src/main.{MainFile.extension}'))
print(f'{colors.OKGREEN}Compiled at: {data.time()}{colors.ENDC}')
out.close()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return None
elif event.event_type == 'modified':
Process()
time.sleep(1)
class Watcher:
DIRECTORY_TO_WATCH = "./src"
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(
event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Error")
self.observer.join()
if __name__ == '__main__':
w = Watcher()
w.run()