-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
373 lines (358 loc) · 15 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from time import sleep
import webbrowser
import os
import subprocess
import re
import comp
import sys
import shutil
import importlib
def main():
for i in "--------------- W E B G E N ---------------":
print(i, end="")
sleep(0.04)
print("\nenter a command or 'help' for a list of commands\n")
cmd = ""
while cmd != "exit":
try:
cmd = input(">> ")
if cmd == "help":
print("\tf(folder,filename) -> opens or creates a new webfile\n\tgf(folder,filename) -> creates an importable global file\n\tfdir -> open file directory\n\tkill(folder) -> delete a project (folder)\n\texit -> exit WebGen\n\texportf(folder, destination) -> exports all webpages to an destination\n\trestart -> restart program\n")
elif re.search("f(.+,.+)", cmd):
exec(cmd)
print("\n\n--------------- W E B G E N ---------------")
elif re.search("gf(.+)", cmd):
exec(cmd)
print("\n\n--------------- W E B G E N ---------------")
elif cmd == "fdir":
url = "file:///"+str(os.getcwd())
webbrowser.get().open(url, new=1)
elif re.search("exportf(.+,.+)", cmd):
exec(cmd)
elif re.search("kill(.+)", cmd):
exec(cmd)
elif cmd == "restart":
os.execv(sys.executable, ['python'] + sys.argv)
elif cmd == "exit":
continue
else:
print('Unrecognized command. Type "help" for list of commands.')
except Exception as exc:
print('[!!!] {err}'.format(err=exc))
if re.search("exportf(.+,.+)", cmd):
print('When referencing the destination path, make sure to prepend with the letter r like so: r"THIS\IS\A\PATH"')
def f(folder, filename):
if not re.search(".+///",filename):
path = "files/"+folder+"/"+filename+"/"
g = False
else:
path = "files/"+folder+"/"
filename = filename[:-3]
g = True
if os.path.exists(path+"/gf.txt"):
gftxt = open(path+"gf.txt",'r')
if filename+".py" not in gftxt.read().split("\n"):
gftxt.close()
gftxt = open(path+"gf.txt",'a')
gftxt.write(filename+".py\n")
gftxt.close()
else:
gftxt = open(path+"gf.txt",'a')
gftxt.write(filename+".py\n")
gftxt.close()
if not os.path.exists(path):
os.makedirs(path)
print("creating new file...")
if not os.path.exists(path+filename+".py"):
ftxt = open(path+filename+".py", 'a')
if g == False:
ftxt.write("import modules as mx\nimport sys\nimport os\nhomedir = os.getcwd() + r'/files/"+folder+"/'\nsys.path.append(homedir)\ndata = mx.Data()\nbody = mx.Body()\n\n### OBJECTS ###\n\n### CONTENT ###\n\n### PROPERTIES ###")
else:
ftxt.write("import modules as mx\n\n### OBJECTS ###\n\n### CONTENT ###\n\n### PROPERTIES ###")
else:
ftxt = open(path+filename+".py", 'a')
if g == False:
fhtml = open(path+"index.html", 'a')
fhtml.close()
ftxt.close()
for i in range(int((43 - (len(filename)+2)) / 2)):
print("-", end="")
sleep(0.02)
print(" ", end="")
sleep(0.02)
for i in filename:
print(i.upper(), end="")
sleep(0.02)
print(" ", end="")
sleep(0.02)
for i in range(int((43 - (len(filename)+2)) / 2)):
print("-", end="")
sleep(0.02)
print("\n")
fedit(folder,filename,g)
def kill(folder):
print('Are you sure you want to delete '+folder+'? Type [y] or [n].')
sure = input('>> ')
if sure == 'y':
print('Please double confirm [y] or [n]. By confirming you may be deleting multiple pages.')
sure = input('>> ')
if sure == 'y':
shutil.rmtree('files/'+folder)
print("Project terminated.")
elif sure == 'n':
print('Project saved.')
else:
print('Informal response. Project saved.')
elif sure == 'n':
print('Project saved.')
else:
print('Informal response. Project saved.')
def gf(folder, filename):
f(folder, filename+"///")
return
def exportf(folder, destination):
init_modules = {module: sys.modules[module] for module in sys.modules}
home = destination
os.makedirs(home+"/"+folder)
for filename in os.listdir("files/"+folder+"/"):
if "." in filename:
continue
if not os.path.exists("files/"+folder+"/"+filename+"/index.html"):
continue
currdir = os.path.dirname(os.path.realpath(__file__))
path = currdir + "/files/"+folder+"/"+filename+"/"
newhome = home + "/" + folder + "/"
os.makedirs(newhome+"/"+filename)
comp.main(folder,filename)
if os.path.exists(path+"/index.php"):
f = open(path+"/index.php",'r')
fr = open(newhome+"/"+filename+"/index.php",'w')
else:
f = open(path+"/index.html",'r')
fr = open(newhome+"/"+filename+"/index.html",'w')
fr.write(f.read())
fr.close()
f.close()
print('copied '+filename)
if os.path.exists(path+"/images"):
os.makedirs(newhome+"/"+filename+"/images")
for file_name in os.listdir(path+"/images"):
source = path+"/images/" + file_name
destination = newhome+"/"+filename+"/images/" + file_name
shutil.copy(source, destination)
print('copied', file_name)
sys.modules = init_modules
shutil.copy('JQuery.js', home+"/"+folder)
shutil.copy('JQuery-UI.js', home+"/"+folder)
prevdir = os.getcwd()
os.chdir(home)
shutil.make_archive(folder, 'zip', folder)
shutil.rmtree(home+"/"+folder)
print('zipped', folder+'.zip')
os.chdir(prevdir)
def fedit(folder,filename,g):
if g == True:
path = "files/"+folder+"/"
else:
path = "files/"+folder+"/"+filename+"/"
cmd = ""
modules, descriptions = getModules()
while cmd != "exit":
try:
cmd = input(">> ")
if cmd == "help":
print("\tlm -> list modules\n\tmod m -> print the properties of mod m\n\tlgf -> list global files\n\te -> open file editor\n\tcss - > open a CSS document\n\tjs -> open a JavaScript document\n\tphp -> open a PHP document (for global files only)\n\tfdir -> open file directory\n\timages -> open images directory\n\tr -> refresh\n\texport(directory_name) -> export compiled file\n\tkill -> delete file\n\texit -> exit file\n\trestart -> restart program\n")
elif cmd == "lm":
for m in descriptions:
print("\t"+m)
elif re.search("mod .+", cmd):
props = modules[cmd[4:]]
for p in props:
print("\t"+p)
elif cmd == "lgf":
p = "files/"+folder+"/"
if os.path.exists(p+"gf.txt"):
gftxt = open(p+"gf.txt",'r')
print([x for x in gftxt.read().split("\n") if x != ''])
gftxt.close()
elif cmd == "r":
if g == True:
print("[!!!] global file cannot be executed.")
else:
c(folder, filename)
elif cmd == "css":
css(folder, filename, g)
elif cmd == "js":
js(folder, filename, g)
elif cmd == "php":
php(folder, filename, g)
elif cmd == "fdir":
url = "file:///"+str(os.getcwd())+"/"+path
webbrowser.open(url, new=0)
elif cmd == "e":
if(g == False):
if sys.platform == "win32":
try:
subprocess.Popen([sys.exec_prefix+r"\Lib\idlelib\idle.bat", path+filename+".py"])
except Exception as exc:
print(exc)
print('opening notepad.exe as subtitute...')
subprocess.Popen(["notepad.exe", path+filename+".py"])
elif sys.platform == "darwin":
try:
subprocess.call(['open', '-a', 'IDLE 3', path+filename+".py"])
except Exception as exc:
print(exc)
print('opening TextEdit as subtitute...')
subprocess.call(['open', '-a', 'TextEdit', path+filename+".py"])
else:
if sys.platform == "win32":
try:
subprocess.Popen([sys.exec_prefix+r"\Lib\idlelib\idle.bat", "files/"+folder+"/"+filename+".py"])
except Exception as exc:
print(exc)
print('opening notepad.exe as subtitute...')
subprocess.Popen(["notepad.exe", "files/"+folder+"/"+filename+".py"])
elif sys.platform == "darwin":
try:
subprocess.call(['open', '-a', 'IDLE 3', "files/"+folder+"/"+filename+".py"])
except Exception as exc:
print(exc)
print('opening TextEdit as subtitute...')
subprocess.call(['open', '-a', 'TextEdit', "files/"+folder+"/"+filename+".py"])
elif cmd == "images":
if g == False:
if not os.path.exists("files/"+folder+"/"+filename+"/images"):
os.makedirs("files/"+folder+"/"+filename+"/images")
url = "file:///"+str(os.getcwd())+"/files/"+folder+"/"+filename+"/images"
webbrowser.open(url, new=0)
elif cmd == "kill":
print('Are you sure you want to delete '+filename+'? Type [y] or [n].')
sure = input('>> ')
if sure == 'y':
if g == False:
shutil.rmtree(path)
if len(os.listdir("files/"+folder+"/")) == 0 or len(os.listdir("files/"+folder+"/")) == 1 and os.listdir("files/"+folder+"/")[0] == '.DS_Store':
shutil.rmtree("files/"+folder+"/")
elif g == True and os.path.exists("files/"+folder+"/"+filename+".py"):
os.remove("files/"+folder+"/"+filename+".py")
if os.path.exists("files/"+folder+"/"+filename+".css"):
os.remove("files/"+folder+"/"+filename+".css")
if os.path.exists("files/"+folder+"/"+filename+".js"):
os.remove("files/"+folder+"/"+filename+".js")
print('Files terminated.')
cmd = "exit"
elif sure == 'n':
print('Files saved.')
else:
print('Informal response. Files saved.')
elif cmd == "restart":
os.execv(sys.executable, ['python'] + sys.argv)
elif re.search("export(.+)", cmd):
dest = cmd.replace('export("','').replace('")','')
export(dest,folder,filename)
elif cmd == "exit":
continue
else:
print('Unrecognized command. Type "help" for list of commands.')
except Exception as exc:
print('[!!!] {err}'.format(err=exc))
def export(home,folder,filename):
currdir = os.path.dirname(os.path.realpath(__file__))
path = currdir + "/files/"+folder+"/"+filename+"/"
os.makedirs(home+"/"+filename)
comp.main(folder,filename)
if os.path.exists(path+"/index.php"):
f = open(path+"/index.php",'r')
fr = open(home+"/"+filename+"/index.php",'w')
else:
f = open(path+"/index.html",'r')
fr = open(home+"/"+filename+"/index.html",'w')
fr.write(f.read())
fr.close()
f.close()
print('copied index')
if os.path.exists(path+"/images"):
os.makedirs(home+"/"+filename+"/images")
for file_name in os.listdir(path+"/images"):
source = path+"/images/" + file_name
destination = home+"/"+filename+"/images/" + file_name
shutil.copy(source, destination)
print('copied', file_name)
prevdir = os.getcwd()
os.chdir(home)
shutil.make_archive(filename, 'zip', filename)
shutil.rmtree(home+"/"+filename)
print('zipped', filename+'.zip')
os.chdir(prevdir)
def css(folder, filename, g):
if g == True:
path = "files/"+folder+"/"
else:
path = "files/"+folder+"/"+filename+"/"
fcss = open(path+filename+".css", 'a')
fcss.close()
if sys.platform == "win32":
subprocess.Popen(["notepad.exe", path+filename+".css"])
elif sys.platform == "darwin":
subprocess.call(['open', '-a', 'TextEdit', path+filename+".css"])
def js(folder, filename, g):
if g == True:
path = "files/"+folder+"/"
else:
path = "files/"+folder+"/"+filename+"/"
if not os.path.exists(path+"JQuery.js"):
copyrootf("JQuery.js", folder)
copyrootf("JQuery-UI.js", folder)
fjs = open(path+filename+".js", 'a')
fjs.close()
if sys.platform == "win32":
subprocess.Popen(["notepad.exe", path+filename+".js"])
elif sys.platform == "darwin":
subprocess.call(['open', '-a', 'TextEdit', path+filename+".js"])
def php(folder, filename, g):
if g == True:
path = "files/"+folder+"/"
fcss = open(path+filename+".php", 'a')
fcss.close()
if sys.platform == "win32":
subprocess.Popen(["notepad.exe", path+filename+".php"])
elif sys.platform == "darwin":
subprocess.call(['open', '-a', 'TextEdit', path+filename+".php"])
else:
print('[!!!] to add php to a file, create a global file and link it with "import"')
def copyrootf(filename, p):
p = "files/"+p+"/"
f = open(filename,'r')
fr = open(p+filename,'w')
fr.write(f.read())
fr.close()
f.close()
def getModules():
modules = {}
descriptions = []
mf = open("modules.py","r")
mfl = mf.read().split("\n")
for l in mfl:
if "# " in l:
descriptions.append(l[2:])
if "class" in l:
if "def __init__(self):" in mfl[mfl.index(l) + 1]:
mod = l[5:].replace(':','').replace(' ','')
props = []
ind = mfl.index(l) + 2
while "self" in mfl[ind]:
bounds = (mfl[ind].index("."), mfl[ind].index("="))
props.append(mfl[ind][bounds[0]:bounds[1] - 1])
ind += 1
modules[mod] = props
mf.close()
return modules, descriptions
def c(folder, filename):
path = "files/"+folder+"/"+filename+"/"
importlib.reload(comp)
comp.main(folder,filename)
url = "file:///"+str(os.getcwd())+"/"+path+"index.html"
webbrowser.get().open(url, new=0)
return
main()