-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPyramidBuilder.py
More file actions
214 lines (191 loc) · 11.1 KB
/
Copy pathPyramidBuilder.py
File metadata and controls
214 lines (191 loc) · 11.1 KB
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
# Importing Required Modules :-
import PySimpleGUI as sg
import os
from html.parser import HTMLParser
from bs4 import BeautifulSoup
import re
# ---------------------------------------------------------------------------------------------------------------------------------
# NEEDS OF BUILD FUNCTION
# ---------------------------------------------------------------------------------------------------------------------------------
def Code_Beautifier(document): # Organises the text of HTML Document.
soup=BeautifulSoup(document, 'html.parser')
_return = soup.prettify()
return _return
def Link_Handler(IN): # Automatically handles link address with relative address (Converts them to Individual File Addresses).
match = re.findall(r'<a href="(.*?)"', IN)
OUT=IN
for link in match:
new_link=(get_file_name(link))[1]
OUT = OUT.replace(link, "/"+new_link)
return OUT
def get_file_name(Path): # Returns a tuple with File name without extension as first element and file name with extension as second from the full path of the file.
_return_with_extension = ""
for i in range(1, len(Path) + 1):
if Path[-i] == "/":
break
else:
_return_with_extension = Path[-i] + _return_with_extension
_return = ""
for j in range(0, len(_return_with_extension)):
if _return_with_extension[j] == ".":
break
else:
_return += _return_with_extension[j]
final = (_return, _return_with_extension)
return final
def func_creator(FileName, Code): # Returns the function as string, needed by pyramid module for HTML page.
unhandled_beautified_code = Code_Beautifier(Code)
completely_handled_code = Link_Handler(unhandled_beautified_code)
_return = "def " + FileName + "(request):" + "\n" + \
" " + "return Response(\"\"\"" + completely_handled_code + "\"\"\")" + "\n" + "\n"
return _return
def route_adder(IN): # Returns two lines needed by pyramid for creating a view and adding path/route of file.
_return = ""
for i in range(0, len(IN)):
File_Details = get_file_name(IN[i])
Individual_Files = " config.add_route(\'" + File_Details[0] + "\',\'/" + File_Details[
1] + "\')\n" + " config.add_view(" + File_Details[0] + ", route_name = \'" + File_Details[
0] + "\')\n"
_return += Individual_Files
return _return
def Home_Page(Path, Port): # Returns the line of code needed for automatically opening an HTML page usually homepage/inital starting point.
file_details = get_file_name(Path)
_return ="webbrowser.open"+ "(\'http://localhost:" + Port + "/" + file_details[1] + "\', new=1)"
return _return
def build(Input_List, Output_Directory, port, On_Start_File): # Main Script for build process that automatically creates a folder with the output file/ build :-
fixed_imports = """import webbrowser\nfrom wsgiref.simple_server import make_server\nfrom pyramid.config import Configurator\nfrom pyramid.response import Response"""
Routes = route_adder(Input_List)
final_lines = """if __name__ == '__main__':\n with Configurator() as config:\n""" + Routes + """ app = config.make_wsgi_app()\n """ + Home_Page(On_Start_File, port) + """\n """ + """server = make_server('0.0.0.0', """ + port + """, app)\n server.serve_forever()"""
os.mkdir(Output_Directory + "/" + "Build") # Creating a directing for storing finally built file.
mf = open(Output_Directory + "/" + "Build" + "/" + "AppFile.py", "a") # Creates and opens a file for final code.
mf.write("# coding: ANSI") # Some opening error removal by defining encoding.
mf.write("\n")
mf.write(fixed_imports) # Adds lines of code containg module imports needed for pyramid program to run.
mf.write("\n")
mf.write("\n")
for loc in Input_List: # loc is the path/location to each HTML file chosen.
f = open(loc, "r")
data_read = f.read() # Reads the data HTML file chosen.
Name_of_File = get_file_name(loc)
write_material = func_creator(Name_of_File[0], data_read)
mf.write(write_material) # Adds the functions by func_creator.
mf.write("\n")
f.close()
mf.write(final_lines) # Finally adds line for adding routes, views, automate the process of opening a HTML page as homepage.
mf.close()
# ---------------------------------------------------------------------------------------------------------------------------------
# GUI AND BASIC FUNCTIONS NEEDED
# ---------------------------------------------------------------------------------------------------------------------------------
def folder_checks(Location): # Performs all checks to ensure proper file handling.
Existence_Check = os.access(Location, os.F_OK)
if Existence_Check:
_return = ("""\n Checking Folder Existence... \n Does Folder exist ? True""", 'CL')
Write_Check = os.access(Location, os.R_OK)
if Write_Check:
_return = ("""\n Checking Folder Existence... \n Does Folder exist ? True \n \n Checking Write Access... \n Do we have Write Permission ? True""", 'CL')
else:
_return = ("""\n Checking Folder Existence... \n Does Folder exist ? True \n Checking Write Access... \n Do we have Write Permission ? False \n Task Terminated.""", 'TD')
else:
_return = ("""\n Checking Folder Existence... \n Does Folder exist ? False \n Task Terminated.""", 'TD')
return _return
def input_checks(Location): # Preforms all checks to ensure proper folder handling.
Existence_Check = os.access(Location, os.F_OK)
if Existence_Check:
_return = ("""\n Checking File Existence... \n Does file exist ? True""", 'CL')
Read_Check = os.access(Location, os.R_OK)
if Read_Check:
_return = ("""\n Checking File Existence... \n Does file exist ? True \n \n Checking Read Access... \n Do we have Read Permission ? True""", 'CL')
else:
_return = ("""\n Checking File Existence... \n Does file exist ? True \n Checking Read Access... \n Do we have Read Permission ? False \n Task Terminated.""", 'TD')
else:
_return = ("""\n Checking File Existence... \n Does file exist ? False \n Task Terminated.""", 'TD')
return _return
# ---------------------------------------------------------------------------------------------------------------------------------
# GRAPHICAL USER INTERFACE
# ---------------------------------------------------------------------------------------------------------------------------------
sg.theme("Reds")
layout1 = [ # Layout 1 for Choosing HTML Files.
[sg.Text('Input Files Chooser :-')],
[sg.Input(key='-IN-'), sg.FileBrowse()],
[sg.Button('Next File'), sg.Button('Submit All'), sg.Button('QUIT')],
[sg.Text(size=(34, 7), key='-Progress-')]
]
layout2 = [ # Layout 2 for choosing Output Folder & BUILD button.
[sg.Text('Output Folder Chooser :-')],
[sg.Input(key='-IN-'), sg.FolderBrowse()],
[sg.Button('BUILD'), sg.Button('QUIT')],
[sg.Text(size=(34, 7), key='-Progress-')]
]
window1 = sg.Window('HTML File Chooser', layout1)
OutputList = ()
while True:
event1, values1 = window1.read()
if event1 == sg.WIN_CLOSED or event1 == 'QUIT':
break
elif event1 == 'Next File':
if values1['-IN-'] == '':
window1['-Progress-'].update('\n Please Enter a path to the file.')
else:
toshow1 = input_checks(values1['-IN-'])
if toshow1[1] == 'CL':
OutputList += (values1['-IN-'],)
window1['-Progress-'].update(toshow1[0])
window1['-IN-'].update('')
elif event1 == 'Submit All':
if values1['-IN-'] == '':
window1.close()
window2 = sg.Window('Output Folder:-', layout2)
while True:
event2, values2 = window2.read()
if event2 == sg.WIN_CLOSED or event2 == 'QUIT':
break
elif event2 == 'BUILD':
if values2['-IN-'] == '':
window1['-Progress-'].update('\n Please Enter a path to the file.')
else:
toShow2 = folder_checks(values2['-IN-'])
if toShow2[1] == 'CL':
window2['-Progress-'].update(toShow2[0])
Port_Number = sg.popup_get_text('Port Checker', 'Enter the Port Number to set the server on :-')
Start_Page = sg.popup_get_file("Choose the file you want to automatically open as Homepage: -", title="File Chooser:-")
try: # MAIN BUILD EXECUTION :-
build(OutputList, values2['-IN-'], Port_Number, Start_Page)
sg.popup("Success", "Your Build of the App was Successful.")
except:
sg.popup("Error", "Unknown Error Occurred.\nThe Build was Unsuccessful.")
else:
window2['-Progress-'].update(toShow2[0])
elif event2 == 'QUIT':
window2.close()
else:
toshow1 = input_checks(values1['-IN-'])
if toshow1[1] == 'CL':
OutputList += (values1['-IN-'],)
window1['-Progress-'].update(toshow1[0])
window1['-IN-'].update('')
window1.close()
window2 = sg.Window('Output Folder:-', layout2)
while True:
event2, values2 = window2.read()
if event2 == sg.WIN_CLOSED or event2 == 'QUIT':
break
elif event2 == 'BUILD':
if values2['-IN-'] == '':
window1['-Progress-'].update('\n Please Enter a path to the file.')
else:
toShow2 = folder_checks(values2['-IN-'])
if toShow2[1] == 'CL':
window2['-Progress-'].update(toShow2[0])
Port_Number = sg.popup_get_text('Port Checker', 'Enter the Port Number to set the server on :-')
Start_Page = sg.popup_get_file("Choose the file you want to automatically open as Homepage: -", title="File Chooser:-")
build(OutputList, values2['-IN-'], Port_Number, Start_Page)
sg.popup("Success", "Your Build of the App was Successful.")
try: # MAIN BUILD EXECUTION :-
build(OutputList, values2['-IN-'], Port_Number, Start_Page)
sg.popup("Success", "Your Build of the App was Successful.")
except:
sg.popup("Error", "Unknown Error Occurred.\nThe Build was Unsuccessful.")
else:
window2['-Progress-'].update(toShow2[0])
elif event2 == 'QUIT':
window2.close()