1 | import shutil # Library For Work With File In High Level Like Copy |
||
2 | import webbrowser |
||
3 | from params import * |
||
4 | import socket |
||
5 | import requests |
||
6 | import re |
||
7 | import time |
||
8 | import sys |
||
9 | import urllib.request |
||
10 | import platform |
||
11 | import random |
||
12 | meta_input = "" |
||
13 | |||
14 | |||
15 | def convert_bytes(num): |
||
16 | for x in ['bytes', 'KB', 'MB', 'GB', 'TB']: |
||
17 | if num < 1024.0: |
||
18 | return "%3.1f %s" % (num, x) |
||
19 | num /= 1024.0 |
||
20 | |||
21 | def file_size(): |
||
22 | list_of_files=os.listdir(out_dir) |
||
23 | response=0 |
||
24 | for file in list_of_files: |
||
25 | file_info = os.stat(os.path.join(out_dir,file)) |
||
26 | response=response+file_info.st_size |
||
27 | print_line(70,"*") |
||
28 | print("Used Space --> "+convert_bytes(response)) |
||
29 | print_line(70, "*") |
||
30 | |||
31 | def download_badge(address): |
||
32 | r = requests.get(address, stream=True) |
||
33 | with open(os.path.join(image_dir,"badge.svg"), 'wb') as f: |
||
34 | shutil.copyfileobj(r.raw, f) |
||
35 | def random_badge_color(): |
||
36 | random_index=random.randint(0,len(badge_color_list)-1) |
||
37 | return badge_color_list[random_index] |
||
38 | def system_details(): |
||
39 | return platform.node()+" , "+platform.processor()+" , "+platform.platform() |
||
40 | |||
41 | def generation_time(time_1=None): |
||
42 | if time_1==None: |
||
43 | return time.perf_counter() |
||
44 | else: |
||
45 | return time.perf_counter()-time_1 |
||
46 | |||
47 | def find_global_ip(): |
||
48 | try: |
||
49 | response=requests.get(ip_finder_api) |
||
50 | return response.text[:-1] |
||
51 | except: |
||
52 | return "0.0.0.0" |
||
53 | def create_badge(subject="qpage", status=version, color="blue",random=False): |
||
54 | if random==True: |
||
55 | color=random_badge_color() |
||
56 | else: |
||
57 | if color not in badge_color_list: |
||
58 | color = "orange" |
||
59 | badge_adr=adv_badge_static + subject + "-" + status + "-" + color + '.svg' |
||
60 | if internet(): |
||
61 | download_badge(badge_adr) |
||
62 | return os.path.join(image_dir,"badge.svg") |
||
63 | else: |
||
64 | return badge_adr |
||
65 | |||
66 | |||
67 | def is_sample_downloaded(): |
||
68 | download_list = [] |
||
69 | if "profile.png" not in os.listdir(image_dir): |
||
70 | download_list.append(0) |
||
71 | if "font.TTF" not in os.listdir(font_dir): |
||
72 | download_list.append(1) |
||
73 | if "resume.pdf" not in os.listdir(doc_dir) and "resume.txt" not in os.listdir(doc_dir): |
||
74 | download_list.extend([2, 3]) |
||
75 | if "icon.ico" not in os.listdir(image_dir): |
||
76 | download_list.append(4) |
||
77 | return download_list |
||
78 | |||
79 | |||
80 | def download_lorem(): |
||
81 | if internet(): |
||
82 | urllib.request.urlretrieve("http://www.qpage.ir/sample/Latin-Lipsum.txt", "Latin-Lipsum.txt") |
||
83 | else: |
||
84 | print("Error In Download Lorem") |
||
85 | |||
86 | |||
87 | def read_lorem(char=100): |
||
88 | try: |
||
89 | if "Latin-Lipsum.txt" not in os.listdir(work_dir): |
||
90 | download_lorem() |
||
91 | lorem_file = open("Latin-Lipsum.txt", "r") |
||
92 | lorem_text = lorem_file.read() |
||
93 | lorem_file.close() |
||
94 | return " ".join(lorem_text.split(" ")[:char]) |
||
95 | except: |
||
96 | return None |
||
97 | |||
98 | |||
99 | def sample_site_download(item_list): |
||
100 | try: |
||
101 | if internet(): |
||
102 | for i in item_list: |
||
103 | print("Downloading " + sample_dict_message[i] + " . . . [" + str(i + 1) + "/5]") |
||
104 | print_line(70) |
||
105 | urllib.request.urlretrieve(list(sample_dict_addr.values())[i], |
||
106 | os.path.join(image_dir, list(sample_dict_addr.keys())[i])) |
||
107 | print("Done! All Material Downloaded") |
||
108 | print_line(70) |
||
109 | else: |
||
110 | print("Error In Internet Connection!") |
||
111 | print_line(70) |
||
112 | except: |
||
113 | print("Error in downloading sample files check your internet conection") |
||
114 | print_line(70) |
||
115 | |||
116 | |||
117 | def logger(status=False,perf_time=None): |
||
118 | file = open("build_log.txt", "a") |
||
119 | if status == False: |
||
120 | file.write("Faild " + str(datetime.datetime.now()) + "\n") |
||
121 | else: |
||
122 | file.write("Sucess " + str(datetime.datetime.now()) + "\n") |
||
123 | file.write("Generation Time: "+str(perf_time)+"\n") |
||
124 | file.close() |
||
125 | |||
126 | |||
127 | def print_line(number, char="-"): |
||
128 | line = "" |
||
129 | for i in range(number): |
||
130 | line = line + char |
||
131 | print(line) |
||
132 | |||
133 | |||
134 | def name_standard(name): |
||
135 | reponse_name = name[0].upper() + name[1:].lower() |
||
136 | return reponse_name |
||
137 | |||
138 | |||
139 | def address_print(): |
||
140 | print_line(70, "*") |
||
141 | print("Where --> " + work_dir) |
||
142 | print_line(70, "*") |
||
143 | |||
144 | |||
145 | def create_folder(): # This Function Create Empty Folder At Begin |
||
146 | folder_flag = 0 |
||
147 | list_of_folders = os.listdir(work_dir) |
||
148 | for i in ["doc", "image", "output", "font"]: |
||
149 | if i not in list_of_folders: |
||
150 | os.mkdir(i) |
||
151 | folder_flag += 1 |
||
152 | if i == "doc": |
||
153 | file = open(os.path.join(doc_dir, "index.txt"), "w") |
||
154 | if read_lorem() == None: |
||
155 | file.write("This is For First Page . . .") |
||
156 | else: |
||
157 | file.write(read_lorem()) |
||
158 | file.close() |
||
159 | return bool(folder_flag) |
||
160 | |||
161 | |||
162 | def page_name_update(): # This Function Update Page Names |
||
163 | for i in os.listdir(doc_dir): |
||
164 | if i.find(".txt") != -1 and i[:-4].upper() != "INDEX": |
||
165 | actual_name.append(i[:-4]) |
||
166 | page_name.append(i[:-4]) |
||
167 | |||
168 | |||
169 | def menu_maker(): # Top Menu Maker In each html page |
||
170 | result = "<center>" |
||
171 | for i in range(len(page_name)): |
||
172 | if page_name[i] == "Home": |
||
173 | targets_blank = "" |
||
174 | else: |
||
175 | targets_blank = 'target="blank"' |
||
176 | result = result + '\t<a href="' + actual_name[i] + '.html"' + targets_blank + '>' + name_standard(page_name[ |
||
177 | i]) + "</a>\n" # Hyper Link To Each Page In HTML File |
||
178 | result += " \n" |
||
179 | result += "</center>" |
||
180 | result = result + "\t\t" + break_line # Add Break line to End Of The Menu |
||
181 | return result # Return All Of The Menu |
||
182 | |||
183 | |||
184 | def menu_writer(): # Write menu_maker output in html file |
||
185 | message = menu_maker() |
||
186 | for i in range(len(page_name)): |
||
187 | file = open(os.path.join(out_dir, actual_name[i] + ".html"), "a") |
||
188 | file.write(message) |
||
189 | file.close() |
||
190 | |||
191 | |||
192 | def print_meta(): |
||
193 | global meta_input |
||
194 | meta_input = input("Please Enter Your Name : ") |
||
195 | static_meta = '<meta name="description" content="Welcome to homepage of ' + meta_input + '"/>\n' |
||
196 | static_meta = static_meta + '<meta property="og:title" content="' + meta_input + '"/>\n' |
||
197 | static_meta = static_meta + '<meta property="og:site_name" content="' + meta_input + '"/>\n' |
||
198 | static_meta = static_meta + '<meta property="og:image" content="favicon.ico" />\n' |
||
199 | if len(meta_input) < 4: |
||
200 | warnings.append("[Warning] Your input for name is too short!!") |
||
201 | return static_meta |
||
202 | |||
203 | |||
204 | def html_init(name): # Create Initial Form Of each Html Page Like Title And HTML And Body Tag |
||
205 | html_name = os.path.join(out_dir, name + ".html") |
||
206 | file = open(html_name, "w") |
||
207 | file.write("<html>\n") |
||
208 | file.write("\t<head>\n") |
||
209 | if name == "index": |
||
210 | file.write("\t\t<title>Welcome To My Homepage</title>\n") |
||
211 | else: |
||
212 | file.write("\t\t<title>" + name_standard(name) + "</title>\n") |
||
213 | file.write('<link rel="stylesheet" href="styles.css" type="text/css"/>\n') |
||
214 | css_link = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css' |
||
215 | file.write('<link rel="stylesheet" href= ' + css_link + ' type="text/style"/>\n') |
||
216 | |||
217 | if name == 'index': # Add meta only for index page |
||
218 | file.write(print_meta()) |
||
219 | |||
220 | file.write("\t</head>\n") |
||
221 | file.write('\t<body class="body_tag">\n') |
||
222 | file.close() |
||
223 | |||
224 | |||
225 | def html_end(name): # Create End Of The Html file |
||
226 | html_name = os.path.join(out_dir, name + ".html") |
||
227 | file = open(html_name, "a") |
||
228 | file.write("\t</body>\n") |
||
229 | file.write("</html>") |
||
230 | file.close() |
||
231 | |||
232 | |||
233 | def close_files(): |
||
234 | for i in files: |
||
235 | i.close() |
||
236 | |||
237 | |||
238 | def LSM_translate(line, center): |
||
239 | line.strip() |
||
240 | text = line |
||
241 | header_start = '<h4 class="color_tag">' |
||
242 | header_end = "</h4>" |
||
243 | if line.find("[L]") != -1: |
||
244 | header_start = '<h2 class="color_tag">' |
||
245 | header_end = "</h2>" |
||
246 | text = line[3:] |
||
247 | elif line.find("[S]") != -1: |
||
248 | header_start = '<h5 class="color_tag">' |
||
249 | header_end = "</h5>" |
||
250 | text = line[3:] |
||
251 | elif line.find("[M]") != -1: |
||
252 | text = line[3:] |
||
253 | if center: # Centerizes Text If Condition Is True For Manual Centering |
||
254 | header_start = "<center>" + header_start |
||
255 | header_end += "</center>" |
||
256 | if text.find("[center]") != -1: # Find Center Tag In Each Line |
||
257 | header_start = "<center>" + header_start |
||
258 | header_end += "</center>" |
||
259 | text = text[:text.find("[center]")] |
||
260 | return [text, header_end, header_start] |
||
261 | |||
262 | View Code Duplication | ||
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
263 | def print_text(text_file, file, center=False, close=False): # Write Text Part Of Each Page |
||
264 | text_code = "" |
||
265 | for line in text_file: |
||
266 | if len(line) == 1: |
||
267 | text_code = space |
||
268 | else: |
||
269 | text_header = LSM_translate(line, center) |
||
270 | text = text_header[0] |
||
271 | header_end = text_header[1] |
||
272 | header_start = text_header[2] |
||
273 | text_code = header_start + text + header_end + "\n" |
||
274 | file.write(text_code) |
||
275 | if close: |
||
276 | file.close() |
||
277 | |||
278 | |||
279 | def print_image(file, close=False, image_format="jpg"): # Write Image Part OF The Page |
||
280 | View Code Duplication | for i in range(len(size_box)): |
|
0 ignored issues
–
show
|
|||
281 | print(i, "-", size_box[i]) |
||
282 | image_size = int(input("Please Enter Profile Image Size : ")) # Choose Profile Image Size |
||
283 | image_size_string = size_box[2] # Getting Html String From size_box list default mode (Medium) |
||
284 | if 0 <= image_size < len(size_box): |
||
285 | image_size_string = size_box[image_size] |
||
286 | image_code = '<center><img src="image.' + image_format + '"' + ', width=' + image_size_string + ' alt="profile image"></img></center>\n' |
||
287 | file.write(image_code) |
||
288 | if close: |
||
289 | file.close() |
||
290 | |||
291 | |||
292 | def print_download(file, name, link, center=False, close=False): # Create Download Link In Page |
||
293 | link_code = "<a href=" + '"' + link + '"' + target_blank + '>' + name + "</a>" |
||
294 | if center: |
||
295 | link_code = "<center>" + link_code + "</center>" |
||
296 | file.write(link_code + "\n") |
||
297 | file.write(break_line) |
||
298 | if close: |
||
299 | file.close() |
||
300 | |||
301 | |||
302 | def print_adv(file, close=True): |
||
303 | file.write(break_line) |
||
304 | file.write( |
||
305 | '<center>' + "<p>" + "Generated " + today_time + " By" + "</p>" + '<a href=' + '"' + homepage + '"' + target_blank + '>' + '<img src="' + create_badge(random=True) + '"alt="Qpage">'+'</a> </center>') |
||
306 | if close: |
||
307 | file.close() |
||
308 | |||
309 | |||
310 | def build_index(file): |
||
311 | image_name = "" |
||
312 | img_format = "jpg" |
||
313 | file_of_images = os.listdir(image_dir) |
||
314 | for i in range(len(file_of_images)): |
||
315 | for form in imformat_box: |
||
316 | if file_of_images[i].find("." + form) != -1: |
||
317 | image_name = os.path.join(image_dir, file_of_images[i]) |
||
318 | img_format = form |
||
319 | global image_counter |
||
320 | image_counter = 1 |
||
321 | break |
||
322 | shutil.copyfile(image_name, os.path.join(out_dir, "image." + img_format)) |
||
323 | print_image(file, image_format=img_format) |
||
324 | |||
325 | |||
326 | def build_resume(file): |
||
327 | resume_name = "" |
||
328 | file_of_docs = os.listdir(doc_dir) |
||
329 | for i in range(len(file_of_docs)): |
||
330 | if file_of_docs[i].find(".pdf") != -1: |
||
331 | resume_name = os.path.join(doc_dir, file_of_docs[i]) |
||
332 | global pdf_counter |
||
333 | pdf_counter = 1 |
||
334 | break |
||
335 | shutil.copyfile(resume_name, os.path.join(out_dir, "Resume.pdf")) |
||
336 | print_download(file, "Download Full Version", "Resume.pdf", center=True) |
||
337 | |||
338 | |||
339 | def contain(name): # main function That Open Each Page HTML File and call other function to write data in it |
||
340 | file = open(os.path.join(out_dir, name + ".html"), "a") |
||
341 | text_file = open(os.path.join(doc_dir, name + ".txt"), "r") |
||
342 | files.append(file) |
||
343 | files.append(text_file) |
||
344 | |||
345 | if name.upper() == "INDEX": |
||
346 | build_index(file) |
||
347 | elif name.upper() == "RESUME": |
||
348 | build_resume(file) |
||
349 | |||
350 | print_text(text_file, file) |
||
351 | print_adv(file) |
||
352 | |||
353 | |||
354 | def clear_folder(path): # This Function Get Path Of Foldr And Delte Its Contains |
||
355 | if os.path.exists(path): |
||
356 | list_of_files = os.listdir(path) |
||
357 | for file in list_of_files: |
||
358 | os.remove(os.path.join(path, file)) |
||
359 | else: |
||
360 | os.mkdir(path) |
||
361 | |||
362 | |||
363 | def print_warning(): |
||
364 | print(str(len(warnings)) + " Warning , 0 Error") |
||
365 | for i in range(len(warnings)): |
||
366 | print(str(i + 1) + "-" + warnings[i]) |
||
367 | |||
368 | |||
369 | def get_color_code(): |
||
370 | for i in range(len(color_box)): |
||
371 | print(i, "-", color_box[i]) |
||
372 | back_color_code = int(input("Please enter your background color : ")) |
||
373 | if back_color_code not in range(7): |
||
374 | back_color_code = 0 |
||
375 | text_color_code = int(input("Please enter your text color : ")) |
||
376 | if text_color_code not in range(7): |
||
377 | text_color_code = 1 |
||
378 | return [back_color_code, text_color_code] |
||
379 | |||
380 | |||
381 | def color_code_map(): |
||
382 | [back_color_code, text_color_code] = get_color_code() |
||
383 | if text_color_code == back_color_code: |
||
384 | warnings.append(warning_dict["color_warning"]+" Your text color and background color are same!!") |
||
385 | background_color = color_box[back_color_code] # convert code to color string in color_box |
||
386 | text_color = color_box[text_color_code] # convert code to color string in color_box |
||
387 | return [background_color, text_color] |
||
388 | |||
389 | |||
390 | def css_font(font_folder): |
||
391 | font_flag = 0 # 0 If there is no font file in font_folder |
||
392 | current_font_format = None |
||
393 | for i in font_folder: |
||
394 | for j in range(len(font_format)): # search for other font format in font box |
||
395 | if i.lower().find(font_format[j]) != -1: # If there is a font in font folder |
||
396 | shutil.copyfile(os.path.join(font_dir, i), |
||
397 | os.path.join(out_dir, "qpage" + font_format[j])) # copy font file to output folder |
||
398 | font_flag = 1 # Turn Flag On |
||
399 | current_font_format = font_format[j] # font format of current selected font for css editing |
||
400 | return [font_flag, current_font_format] |
||
401 | |||
402 | |||
403 | def font_creator(css_file, font_section): |
||
404 | |||
405 | font_folder = os.listdir(font_dir) |
||
406 | details = css_font(font_folder) |
||
407 | current_font_format = details[1] |
||
408 | font_flag = details[0] |
||
409 | |||
410 | if font_flag == 1: # check flag if it is 1 |
||
411 | css_file.write( |
||
412 | "@font-face{\nfont-family:qpagefont;\nsrc:url(qpage" |
||
413 | + current_font_format |
||
414 | + ");\n}\n") # Write font-face in html |
||
415 | |||
416 | font_section = "font-family:qpagefont;\n" # Update Font Section For Body Tag |
||
417 | for i in range(len(fontstyle_box)): |
||
418 | print(i, "-", fontstyle_box[i]) |
||
419 | font_style = int(input(" Please choose your font style ")) |
||
420 | if font_style < len(fontstyle_box): |
||
421 | font_style = fontstyle_box[font_style] |
||
422 | else: |
||
423 | font_style = "normal" |
||
424 | font_section = font_section + "font-style:" + font_style + ";\n" |
||
425 | else: |
||
426 | warnings.append(warning_dict["font_warning"]+" There is no specific font set for this website!!") |
||
427 | return font_section |
||
428 | |||
429 | |||
430 | def css_creator(): # Ask For background and text color in |
||
431 | font_section = 'font-family : Georgia , serif;\n' |
||
432 | colors = color_code_map() |
||
433 | background_color = colors[0] |
||
434 | text_color = colors[1] |
||
435 | |||
436 | css_file = open(os.path.join(out_dir, "styles.css"), "w") # open css file |
||
437 | font_section = font_creator(css_file, font_section) |
||
438 | |||
439 | css_file.write( |
||
440 | ".body_tag{\n" |
||
441 | + "background-color:" |
||
442 | + background_color |
||
443 | + ";\n" |
||
444 | + font_section |
||
445 | + css_margin |
||
446 | + css_animation_1 |
||
447 | + "}\n") # write body tag |
||
448 | |||
449 | css_file.write(".color_tag{\n" + "color:" + text_color + ";\n}") # write color_tag in css |
||
450 | css_file.write(css_animation_2) |
||
451 | css_file.close() # close css file |
||
452 | |||
453 | |||
454 | def preview(): |
||
455 | webbrowser.open(os.path.join(out_dir, "index.html")) |
||
456 | |||
457 | |||
458 | def error_finder(): |
||
459 | error_vector = [] |
||
460 | pass_vector = [] |
||
461 | pdf_counter = 0 |
||
462 | image_list = os.listdir(image_dir) |
||
463 | doc_list = os.listdir(doc_dir) |
||
464 | if image_counter == 1: |
||
465 | pass_vector.append("[Pass] Your profile image in OK!!") |
||
466 | else: |
||
467 | error_vector.append(error_dict["image_error"]+" Your profile image is not in correct format") |
||
468 | if len(doc_list) == 0: |
||
469 | error_vector.append(error_dict["empty_error"]+" There is no file in doc folder ( index.txt and .pdf file in necessary)") |
||
470 | else: |
||
471 | if "index.txt" in doc_list: |
||
472 | pass_vector.append("[Pass] index.txt file OK!") |
||
473 | else: |
||
474 | error_vector.append(error_dict["firstpage_error"]+" index.txt is not in doc folder!") |
||
475 | if pdf_counter == 0: |
||
476 | error_vector.append(error_dict["resume_error"]+"[Error] Where Is Your Resume File? It should be in doc folder") |
||
477 | else: |
||
478 | pass_vector.append("[Pass] Your Resume File is OK!!") |
||
479 | return [error_vector, pass_vector] |
||
480 | |||
481 | |||
482 | def icon_creator(): |
||
483 | icon_flag = 0 |
||
484 | for file in os.listdir(image_dir): |
||
485 | if file.endswith('ico'): |
||
486 | shutil.copy(os.path.join(image_dir, file), out_dir) |
||
487 | os.rename(os.path.join(out_dir, file), os.path.join(out_dir, 'favicon.ico')) |
||
488 | icon_flag = 1 |
||
489 | break |
||
490 | if "favicon.ico" in os.listdir(work_dir) and icon_flag == 0: |
||
491 | shutil.copy(os.path.join(work_dir, "favicon.ico"), out_dir) |
||
492 | warnings.append(warning_dict["icon_warning"]+" There is no icon for this website") |
||
493 | |||
494 | |||
495 | def robot_maker(): # This Function Create Robots.txt for pages |
||
496 | robots = open(os.path.join(out_dir, "robots.txt"), "w") |
||
497 | robots.write("User-agent: *\n") |
||
498 | robots.write("Disallow: ") |
||
499 | robots.close() |
||
500 | |||
501 | |||
502 | def internet(host="8.8.8.8", port=53, timeout=3): |
||
503 | try: |
||
504 | socket.setdefaulttimeout(timeout) |
||
505 | socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port)) |
||
506 | return True |
||
507 | except Exception as ex: |
||
508 | return False |
||
509 | |||
510 | |||
511 | def server(): |
||
512 | global meta_input |
||
513 | headers = {'content-type': 'application/json', "NAME": meta_input, "Version": version,"SYSTEM":system_details(),"IP":find_global_ip()} |
||
514 | response = requests.get(server_api, headers=headers) |
||
515 | # print(response) |
||
516 | |||
517 | |||
518 | def version_control(): |
||
519 | try: |
||
520 | #print("Check for new version . . .") |
||
521 | #print_line(70) |
||
522 | version_pattern = r"last_version:(.+)" |
||
523 | if internet(): |
||
524 | response = requests.get("http://www.qpage.ir/releases.html") |
||
525 | body = response.text |
||
526 | last_version = float(re.findall(version_pattern, body)[0][:-3]) |
||
527 | if last_version > float(version): |
||
528 | print_line(70) |
||
529 | print("**New Version Of Qpage Is Available Now (Version " + str(last_version) + ")**") |
||
530 | print("Download Link -->" + "https://github.com/sepandhaghighi/qpage/archive/v" + str( |
||
531 | last_version) + ".zip") |
||
532 | print_line(70) |
||
533 | else: |
||
534 | pass |
||
535 | #print("Already Updated!!!") |
||
536 | #print_line(70) |
||
537 | except: |
||
538 | pass |
||
539 | |||
540 | |||
541 | def enter_to_exit(mode=False): |
||
542 | print_line(70, "*") |
||
543 | response = input("Enter [R] for restart Qpage and any other key to exit : ") |
||
544 | if response.upper() != "R": |
||
545 | sys.exit() |
||
546 | |||
547 | |||
548 | def wait_func(iteration): |
||
549 | for i in range(iteration): |
||
550 | time.sleep(1) |
||
551 | print(".") |
||
552 |