Completed
Push — master ( 6682c5...b1f7aa )
by Mohammad Mahdi
01:07
created

show_items()   A

Complexity

Conditions 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
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
import datetime
13
from functools import reduce
14
15
meta_input = ""
16
17
18
def show_items(list):
19
    for i, item in enumerate(list):  # print errors
20
        print(str(i + 1) + "-" + item)  # print pass
21
22
23
def print_logo():
24
    """ print qpage logo by characters
25
26
    """
27
    if "logo.txt" in os.listdir():
28
        with open("logo.txt", "r") as logo_file:
29
            for line in logo_file:
30
                print(line.rstrip())
31
    else:
32
        pass
33
34
35
def convert_bytes(num):
36
    """ convert num to idiomatic byte unit
37
38
    :param num: the input number.
39
    """
40
    for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
41
        if num < 1024.0:
42
            return "%3.1f %s" % (num, x)
43
        num /= 1024.0
44
45
46
def file_size():
47
    """ Print the size of output file
48
49
    """
50
    list_of_files = os.listdir(OUT_DIR)
51
    response = 0
52
    for file in list_of_files:
53
        file_info = os.stat(os.path.join(OUT_DIR, file))
54
        response += file_info.st_size
55
    print_line(70, "*")
56
    print("Used SPACE --> " + convert_bytes(response))
57
    print_line(70, "*")
58
59
60
def download_badge(address):
61
    """ Download badge for website
62
63
    :param address: the address that should get badge
64
    """
65
    r = requests.get(address, stream=True)
66
    with open(os.path.join(IMAGE_DIR, "badge.svg"), 'wb') as f:
67
        shutil.copyfileobj(r.raw, f)
68
69
70
def random_badge_color():
71
    """return a random color for badge
72
73
    """
74
    random_index = random.randint(0, len(BADGE_COLOR_LIST) - 1)
75
    return BADGE_COLOR_LIST[random_index]
76
77
78
def system_details():
79
    """ Show detail of system that code is runnig on
80
81
    """
82
    return platform.node() + " , " + platform.processor() + " ,  " + platform.platform()
83
84
85
def generation_time(time_1=None):
86
    """ Calculate the generation time
87
88
    :param time_1: time that passed but not counted in generation time
89
    :return :the amount of time that passed .
90
    """
91
    if time_1 is None:
92
        return time.perf_counter()
93
    else:
94
        return time.perf_counter() - time_1
95
96
97
def find_global_ip():
98
    """ Find the ip for use in API
99
100
    :return: return the IP.
101
    """
102
    try:
103
        response = requests.get(IP_FINDER_API)
104
        return response.text[:-1]
105
    except Exception as e:
106
        error_log(e)
107
        return "0.0.0.0"
108
109
110
def create_badge(subject="qpage", status=VERSION, color="blue", random=False):
111
    if random:
112
        color = random_badge_color()
113
    else:
114
        if color not in BADGE_COLOR_LIST:
115
            color = "orange"
116
    badge_adr = ADV_BADGE_STATIC + subject + "-" + status + "-" + color + '.svg'
117
    if internet():
118
        download_badge(badge_adr)
119
        return os.path.join(IMAGE_DIR, "badge.svg")
120
    else:
121
        return badge_adr
122
123
124
def is_sample_downloaded():
125
    """ Check the sample site material is downloaded
126
127
    :return : list of the materials
128
    """
129
    download_list = []
130
    if "profile.png" not in os.listdir(IMAGE_DIR):
131
        download_list.append(0)
132
    if "font.TTF" not in os.listdir(FONT_DIR):
133
        download_list.append(1)
134
    if "resume.pdf" not in os.listdir(DOC_DIR) and "resume.txt" not in os.listdir(DOC_DIR):
135
        download_list.extend([2, 3])
136
    if "icon.ico" not in os.listdir(IMAGE_DIR):
137
        download_list.append(4)
138
    return download_list
139
140
141
def download_lorem():
142
    """ Download the lorem file
143
144
    """
145
    if internet():
146
        urllib.request.urlretrieve("http://www.qpage.ir/sample/Latin-Lipsum.txt", "Latin-Lipsum.txt")
147
    else:
148
        print("Error In Download Lorem")
149
150
151
def read_lorem(char=100):
152
    """ find and read lorem
153
154
    :param char: the amount of char that needed
155
    :return : the lorme string
156
    """
157
    try:
158
        if "Latin-Lipsum.txt" not in os.listdir(WORK_DIR):
159
            download_lorem()
160
        lorem_file = open("Latin-Lipsum.txt", "r")
161
        lorem_text = lorem_file.read()
162
        lorem_file.close()
163
        return " ".join(lorem_text.split(" ")[:char])
164
    except Exception as e:
165
        error_log(e)
166
        return None
167
168
169
def sample_site_download(item_list):
170
    """Download sample material for make a fake site
171
172
    :param item_list: Download items form item_list
173
    """
174
    try:
175
        if internet():
176
            for i in item_list:
177
                print("Downloading " + SAMPLE_DICT_MESSAGE[i] + " . . . [" + str(i + 1) + "/5]")
178
                print_line(70)
179
                urllib.request.urlretrieve(list(SAMPLE_DICT_ADDR.values())[i],
180
                                           os.path.join(IMAGE_DIR, list(SAMPLE_DICT_ADDR.keys())[i]))
181
            print("Done! All Material Downloaded")
182
            print_line(70)
183
        else:
184
            print("Error In Internet Connection!")
185
            print_line(70)
186
    except Exception as e:
187
        error_log(e)
188
        print("Error in downloading sample files check your internet conection")
189
        print_line(70)
190
191
192
def logger(status=False, perf_time=None):
193
    """Show the build log of the app
194
195
    :param status: show status of app.
196
    :param perf_time : show the time passed for generate files
197
    """
198
    if "log" not in os.listdir():
199
        os.mkdir("log")
200
    file = open(reduce(os.path.join, [os.getcwd(), "log", "build_log.txt"]), "a")
201
    if not status:
202
        file.write("Failed  " + str(datetime.datetime.now()) + "\n")
203
    else:
204
        file.write("Success " + str(datetime.datetime.now()) + "\n")
205
        file.write("Generation Time: " + str(perf_time) + "\n")
206
    file.close()
207
208
209
def error_log(msg):
210
    """Show the errorlog of the app
211
212
        :param msg: error message
213
    """
214
    if "log" not in os.listdir():
215
        os.mkdir("log")
216
    file = open(reduce(os.path.join, [os.getcwd(), "log", "error_log.txt"]), "a")
217
    file.write(str(datetime.datetime.now()) + " --> " + str(msg) + "\n")
218
    file.close()
219
220
221
def print_line(number, char="-"):
222
    """ Print a Line
223
224
    :param number: the amount char that in lien
225
    :param char  : the char that used to draw line
226
    """
227
    line = ""
228
    i = 0
229
    while (i < number):
230
        line += char
231
        i += 1
232
    print(line)
233
234
235
def name_standard(name):
236
    """ return the Standard VERSION of the input word
237
238
    :param name: the name that should be standard
239
    :return name: the standard form of word
240
    """
241
    reponse_name = name[0].upper() + name[1:].lower()
242
    return reponse_name
243
244
245
def address_print():
246
    """Print the working directory
247
248
    """
249
    print_line(70, "*")
250
    print("Where --> " + WORK_DIR)
251
    print_line(70, "*")
252
253
254
def create_folder():
255
    """This Function Create Empty Folder At Begin
256
257
    """
258
    folder_flag = 0
259
    list_of_folders = os.listdir(WORK_DIR)
260
    for i in ["doc", "image", "output", "font"]:
261
        if i not in list_of_folders:
262
            os.mkdir(i)
263
            folder_flag += 1
264
            if i == "doc":
265
                file = open(os.path.join(DOC_DIR, "index.txt"), "w")
266
                if read_lorem() is None:
267
                    file.write("This is For First Page . . .")
268
                else:
269
                    file.write(read_lorem())
270
                file.close()
271
    return bool(folder_flag)
272
273
274
def page_name_update():
275
    """This Function Update Page Names
276
277
    """
278
    for i in os.listdir(DOC_DIR):
279
        if i.find(".txt") != -1 and i[:-4].upper() != "INDEX":
280
            ACTUAL_NAME.append(i[:-4])
281
            PAGE_NAME.append(i[:-4])
282
283
284
def menu_maker():
285
    """Top Menu Maker In each html page
286
287
    """
288
    result = "<center>"
289
    for i, item in enumerate(PAGE_NAME):
290
        if item == "Home":
291
            targets_blank = ""
292
        else:
293
            targets_blank = 'target="blank"'
294
            # Hyper Link To Each Page In HTML File
295
        result += '\t<a href="' \
296
                  + ACTUAL_NAME[i] + '.html"' + targets_blank + '>' + name_standard(item) + "</a>\n"
297
        result += "&nbsp\n"
298
    result += "</center>"
299
    result = result + "\t\t" + BREAK_LINE  # Add Break line to End Of The Menu
300
    return result  # Return All Of The Menu
301
302
303
def menu_writer():  #
304
    """Write menu_maker output in html and close file after
305
306
    """
307
    message = menu_maker()
308
    PAGE_NAME_length = len(PAGE_NAME)
309
    for i in range(PAGE_NAME_length):
310
        file = open(os.path.join(OUT_DIR, ACTUAL_NAME[i] + ".html"), "a")
311
        file.write(message)
312
        file.close()
313
314
315
def print_meta():
316
    """Add meta to html files
317
318
    :return static_meta: The meta that created
319
    """
320
    global meta_input
321
    meta_input = input("Please Enter Your Name : ")
322
    static_meta = '<meta name="description" content="Welcome to HOMEPAGE of ' + meta_input + '"/>\n'
323
    static_meta += '<meta property="og:title" content="' + meta_input + '"/>\n'
324
    static_meta += '<meta property="og:site_name" content="' + meta_input + '"/>\n'
325
    static_meta += '<meta property="og:image" content="favicon.ico" />\n'
326
    if len(meta_input) < 4:
327
        warnings.append("[Warning] Your input for name is too short!!")
328
    return static_meta
329
330
331
def html_init(name):
332
    """Create Initial Form Of each Html Page Like Title And HTML  And Body Tag.
333
334
    :param name: the name of html file.
335
    """
336
337
    html_name = os.path.join(OUT_DIR, name + ".html")
338
    file = open(html_name, "w")
339
    file.write("<html>\n")
340
    file.write("\t<head>\n")
341
    if name == "index":
342
        file.write("\t\t<title>Welcome To My HOMEPAGE</title>\n")
343
    else:
344
        file.write("\t\t<title>" + name_standard(name) + "</title>\n")
345
    file.write('<link rel="stylesheet" href="styles.css" type="text/css"/>\n')
346
    css_link = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'
347
    file.write('<link rel="stylesheet" href= ' + css_link + ' type="text/style"/>\n')
348
349
    if name == 'index':  # Add meta only for index page
350
        file.write(print_meta())
351
352
    file.write("\t</head>\n")
353
    file.write('\t<body class="body_tag">\n')
354
    file.close()
355
356
357
def html_end(name):
358
    """Create End Of The Html and close file
359
360
    :param name: The name of html file.
361
    """
362
    html_name = os.path.join(OUT_DIR, name + ".html")
363
    file = open(html_name, "a")
364
    file.write("\t</body>\n")
365
    file.write("</html>")
366
    file.close()
367
368
369
def close_files():
370
    """Close all the files.
371
372
    """
373
    for i in files:
374
        if i.closed == False:
375
            i.close()
376
377
378
def LSM_translate(line, center):
379
    # TODO : write a document for this function
380
    """ ????
381
382
    :param line: the input line.
383
    :param center: put it in center
384
385
    :return : return a list contain text header end and header begin
386
    """
387
    line.strip()
388
    text = line
389
    header_start = '<h4 class="color_tag">'
390
    header_end = "</h4>"
391
    if line.find("[L]") != -1:
392
        header_start = '<h2 class="color_tag">'
393
        header_end = "</h2>"
394
        text = line[3:]
395
    elif line.find("[S]") != -1:
396
        header_start = '<h5 class="color_tag">'
397
        header_end = "</h5>"
398
        text = line[3:]
399
    elif line.find("[M]") != -1:
400
        text = line[3:]
401
    if center:  # Centuries Text If Condition Is True For Manual Centering
402
        header_start = "<center>" + header_start
403
        header_end += "</center>"
404
    if text.find("[center]") != -1:  # Find Center Tag In Each Line
405
        header_start = "<center>" + header_start
406
        header_end += "</center>"
407
        text = text[:text.find("[center]")]
408
    return [text, header_end, header_start]
409
410
411
def print_text(text_file, file, center=False, close=False):  # Write Text Part Of Each Page
412
    """Write the text part of each page
413
414
    :param text_file: Text tha should be written.
415
    :param file     : The file that text will be written inside.
416
    :param center   : put the text in center.
417
    :param close    : close file after done editing
418
419
    :type close : bool
420
    :type center: bool
421
422
    """
423
424
    text_code = ""
425
    for line in text_file:
426
        if len(line) == 1:
427
            text_code = SPACE
428
        else:
429
            text_header = LSM_translate(line, center)
430
            text = text_header[0]
431
            header_end = text_header[1]
432
            header_start = text_header[2]
433
            text_code = header_start + text + header_end + "\n"
434
        file.write(text_code)
435
    if close:
436
        file.close()
437
438
439
def print_image(file, image_format="jpg", close=False):
440
    """Write Image Part OF The Page.
441
442
    :param file: The file that images will be added.
443
    :param close : close file after done editing.
444
    :param image_format: the format of image
445
446
    :type close : bool
447
    """
448
    for i, item in enumerate(SIZE_BOX):
449
        print(i, "-", item)
450
    image_size = int(input("Please Enter Profile Image Size : "))  # Choose Profile Image Size
451
    image_size_string = SIZE_BOX[2]  # Getting Html String From SIZE_BOX list default mode (Medium)
452
    if 0 <= image_size < len(SIZE_BOX):
453
        image_size_string = SIZE_BOX[image_size]
454
    image_code = '<center><img src="image.' + image_format + '"' + ', width=' + image_size_string + ' alt="profile image"></img></center>\n'
455
    file.write(image_code)
456
    if close:
457
        file.close()
458
459
460
def print_download(file, name, link, center=False, close=False):
461
    """ Create Download Link in page
462
463
    :param file: The file that contain html of page.
464
    :param name: The name of the link
465
    :param link: The place that name is Linked
466
    :param center: put the text in center
467
    :param close : close file after done editing
468
469
    :type center: bool
470
    :type close : bool
471
472
    """
473
    link_code = "<a href=" + '"' + link + '"' + TARGET_BLANK + '>' + name + "</a>"
474
    if center:
475
        link_code = "<center>" + link_code + "</center>"
476
    file.write(link_code + "\n")
477
    file.write(BREAK_LINE)
478
    if close:
479
        file.close()
480
481
482
def print_adv(file, close=True):
483
    """ Print the advertisement.
484
485
    :param file  : The file that should ad to it.
486
    :param close : Close file after add ad
487
    """
488
    file.write(BREAK_LINE)
489
    file.write(
490
        '<center>' + "<p>" + "Generated " + today_time + " By" + "</p>" + '<a href=' + '"' + HOMEPAGE + '"' + TARGET_BLANK + '>' + '<img src="' + create_badge(
491
            random=True) + '"alt="Qpage">' + '</a> </center>')
492
    if close:
493
        file.close()
494
495
496
def build_index(file):
497
    """ Find and build index page
498
499
    :param file: The index file.
500
    """
501
    image_name = ""
502
    img_format = "jpg"
503
    file_of_images = os.listdir(IMAGE_DIR)
504
    for i in file_of_images:
505
        for form in IMFORMAT_BOX:
506
            if i.find("." + form) != -1:
507
                image_name = os.path.join(IMAGE_DIR, i)
508
                img_format = form
509
                global IMAGE_COUNTER
510
                IMAGE_COUNTER = 1
511
                break
512
    shutil.copyfile(image_name, os.path.join(OUT_DIR, "image." + img_format))
513
    print_image(file, img_format)
514
515
516
def build_resume(file):
517
    """ Find and build resume page.
518
519
    :param file: The resume file.
520
    """
521
    resume_name = ""
522
    file_of_docs = os.listdir(DOC_DIR)
523
    for i in file_of_docs:
524
        if i.find(".pdf") != -1:
525
            resume_name = os.path.join(DOC_DIR, i)
526
            global PDF_COUNTER
527
            PDF_COUNTER = 1
528
            break
529
    shutil.copyfile(resume_name, os.path.join(OUT_DIR, "Resume.pdf"))
530
    print_download(file, "Download Full Version", "Resume.pdf", center=True)
531
532
533
def contain(name):
534
    """main function That Open Each Page HTML File and call other function to write data in it
535
536
    :param name: the name of the file that should be written
537
    """
538
    #
539
    file = open(os.path.join(OUT_DIR, name + ".html"), "a")
540
    text_file = open(os.path.join(DOC_DIR, name + ".txt"), "r")
541
    files.append(file)
542
    files.append(text_file)
543
544
    if name.upper() == "INDEX":
545
        build_index(file)
546
    elif name.upper() == "RESUME":
547
        build_resume(file)
548
549
    print_text(text_file, file)
550
    if name.upper() == "INDEX":
551
        print_adv(file)
552
553
554
def clear_folder(path):
555
    """This Function Get Path Of Foldr And Delete Its Contains
556
557
    :param path: the path that gonna be deleted.
558
    """
559
560
    if os.path.exists(path):
561
        list_of_files = os.listdir(path)
562
        for file in list_of_files:
563
            os.remove(os.path.join(path, file))
564
    else:
565
        os.mkdir(path)
566
567
568
def print_warning():
569
    """ Print Warnings!
570
571
    """
572
    print(str(len(warnings)) + " Warning , 0 Error")
573
    show_items(warnings)
574
575
576
def get_color_code():
577
    """Ask for selecting color of text and background
578
579
    :return list: background and text color
580
    """
581
    for i, item in enumerate(COLOR_BOX):
582
        print(i, "-", item)
583
    back_color_code = int(input("Please enter your background color : "))
584
    if back_color_code not in range(7):
585
        back_color_code = 0
586
    text_color_code = int(input("Please enter your text color : "))
587
    if text_color_code not in range(7):
588
        text_color_code = 1
589
    return [back_color_code, text_color_code]
590
591
592
def color_code_map():
593
    """ Check and insert colors that is chosen.
594
595
    :return list: background and text color
596
    """
597
    [back_color_code, text_color_code] = get_color_code()
598
    if text_color_code == back_color_code:
599
        warnings.append(WARNING_DICT["color_warning"] + " Your text color and background color are same!!")
600
    background_color = COLOR_BOX[back_color_code]  # convert code to color string in COLOR_BOX
601
    text_color = COLOR_BOX[text_color_code]  # convert code to color string in COLOR_BOX
602
    return [background_color, text_color]
603
604
605
def css_font(font_folder):
606
    """ Search and file all fonts.
607
608
    :param font_folder: the folder to search.
609
    :return list : font_flag and the current format
610
    """
611
    font_flag = 0  # 0 If there is no font file in font_folder
612
    current_FONT_FORMAT = None
613
    for i in font_folder:
614
        for j in FONT_FORMAT:  # search for other font format in font box
615
            if i.lower().find(j) != -1:  # If there is a font in font folder
616
                shutil.copyfile(os.path.join(FONT_DIR, i),
617
                                os.path.join(OUT_DIR, "qpage" + j))  # copy font file to output folder
618
                font_flag = 1  # Turn Flag On
619
                current_FONT_FORMAT = j  # font format of current selected font for css editing
620
    return [font_flag, current_FONT_FORMAT]
621
622
623
def font_creator(css_file, font_section):
624
    """ Ask and Select font.
625
626
    :param css_file: the file that font css will be added to.
627
    :param font_section: the font section of css file
628
629
    :return font_section: the font section of css after edit
630
    """
631
    font_folder = os.listdir(FONT_DIR)
632
    details = css_font(font_folder)
633
    current_FONT_FORMAT = details[1]
634
    font_flag = details[0]
635
636
    if font_flag == 1:  # check flag if it is 1
637
        css_file.write(
638
            "@font-face{\nfont-family:qpagefont;\nsrc:url(qpage"
639
            + current_FONT_FORMAT
640
            + ");\n}\n")  # Write font-face in html
641
642
        font_section = "font-family:qpagefont;\n"  # Update Font Section For Body Tag
643
        for i, item in enumerate(FONTSTYLE_BOX):
644
            print(i, "-", item)
645
        font_style = int(input(" Please choose your font style "))
646
        if font_style < len(FONTSTYLE_BOX):
647
            font_style = FONTSTYLE_BOX[font_style]
648
        else:
649
            font_style = "normal"
650
        font_section = font_section + "font-style:" + font_style + ";\n"
651
    else:
652
        warnings.append(WARNING_DICT["font_warning"] + " There is no specific font set for this website!!")
653
    return font_section
654
655
656
def css_creator():
657
    """Ask For background and text color in and make css """
658
    font_section = 'font-family : Georgia , serif;\n'
659
    colors = color_code_map()
660
    background_color = colors[0]
661
    text_color = colors[1]
662
663
    css_file = open(os.path.join(OUT_DIR, "styles.css"), "w")  # open css file
664
    font_section = font_creator(css_file, font_section)
665
666
    css_file.write(
667
        ".body_tag{\n"
668
        + "background-color:"
669
        + background_color
670
        + ";\n"
671
        + font_section
672
        + CSS_MARGIN
673
        + CSS_ANIMATION_1
674
        + "}\n")  # write body tag
675
676
    css_file.write(".color_tag{\n" + "color:" + text_color + ";\n}")  # write color_tag in css
677
    css_file.write(CSS_ANIMATION_2)
678
    css_file.close()  # close css file
679
680
681
def preview():
682
    """Preview website in browser """
683
    # TODO: not working on unix
684
685
    webbrowser.open(os.path.join(OUT_DIR, "index.html"))
686
687
688
def error_finder():
689
    """ Check and find error that display it"""
690
    error_vector = []
691
    pass_vector = []
692
    PDF_COUNTER = 0
693
    # image_list = os.listdir(IMAGE_DIR)
694
    doc_list = os.listdir(DOC_DIR)
695
    if IMAGE_COUNTER == 1:
696
        pass_vector.append("[Pass] Your profile image in OK!!")
697
    else:
698
        error_vector.append(ERROR_DICT["image_error"] + " Your profile image is not in correct format")
699
    if len(doc_list) == 0:
700
        error_vector.append(ERROR_DICT["empty_error"] + "There is no file in doc folder ( index.txt and .pdf file in "
701
                                                        "necessary)")
702
    else:
703
        if "index.txt" in doc_list:
704
            pass_vector.append("[Pass] index.txt file OK!")
705
        else:
706
            error_vector.append(ERROR_DICT["firstpage_error"] + " index.txt is not in doc folder!")
707
        if PDF_COUNTER == 0:
708
            error_vector.append(ERROR_DICT["resume_error"] + "[Error] Where Is Your Resume File? It should be in doc "
709
                                                             "folder")
710
        else:
711
            pass_vector.append("[Pass] Your Resume File is OK!!")
712
    return [error_vector, pass_vector]
713
714
715
def icon_creator():
716
    """ Find .ico file and use it as favicon of website."""
717
    icon_flag = 0
718
    for file in os.listdir(IMAGE_DIR):
719
        if file.endswith('ico'):
720
            shutil.copy(os.path.join(IMAGE_DIR, file), OUT_DIR)
721
            os.rename(os.path.join(OUT_DIR, file), os.path.join(OUT_DIR, 'favicon.ico'))
722
            icon_flag = 1
723
            break
724
    if "favicon.ico" in os.listdir(WORK_DIR) and icon_flag == 0:
725
        shutil.copy(os.path.join(WORK_DIR, "favicon.ico"), OUT_DIR)
726
        warnings.append(WARNING_DICT["icon_warning"] + " There is no icon for this website")
727
728
729
def robot_maker():
730
    """ Create Robots.txt for pages """
731
    robots = open(os.path.join(OUT_DIR, "robots.txt"), "w")
732
    robots.write("User-agent: *\n")
733
    robots.write("Disallow: ")
734
    robots.close()
735
736
737
def internet(host="8.8.8.8", port=53, timeout=3):
738
    """ Check Internet Connections.
739
740
    :param  host: the host that check connection to
741
    :param  port: port that check connection with
742
    :param  timeout: times that check the connnection
743
744
    :return bool: True if Connection is Stable
745
    """
746
    try:
747
        socket.setdefaulttimeout(timeout)
748
        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
749
        return True
750
    except Exception as ex:
751
        error_log(ex)
752
        return False
753
754
755
def server():
756
    """Get Server response."""
757
    # global meta_input
758
    headers = {'content-type': 'application/json', "NAME": meta_input, "VERSION": VERSION, "SYSTEM": system_details(),
759
               "IP": find_global_ip()}
760
    response = requests.get(SERVER_API, headers=headers)
761
    if response.status_code == 200:
762
        print("Installed Saved!")
763
764
765
def version_control():
766
    """ Check and update VERSIONs. """
767
768
    try:
769
        # print("Check for new VERSION . . .")
770
        # print_line(70)
771
        VERSION_pattern = r"last_VERSION:(.+)"
772
        if internet():
773
            response = requests.get("http://www.qpage.ir/releases.html")
774
            body = response.text
775
            last_VERSION = float(re.findall(VERSION_pattern, body)[0][:-3])
776
            if last_VERSION > float(VERSION):
777
                print_line(70)
778
                print("**New VERSION Of Qpage Is Available Now (VERSION " + str(last_VERSION) + ")**")
779
                print("Download Link -->" + "https://github.com/sepandhaghighi/qpage/archive/v" + str(
780
                    last_VERSION) + ".zip")
781
                print_line(70)
782
            else:
783
                # TODO : fix VERSION control else
784
                pass
785
                # print("Already Updated!!!")
786
                # print_line(70)
787
    except Exception as e:
788
        error_log(e)
789
        pass
790
791
792
def enter_to_exit():
793
    """Quit Project by pressing a key.
794
795
    """
796
797
    print_line(70, "*")
798
    response = input("Enter [R] for restart Qpage and any other key to exit : ")
799
    if response.upper() != "R":
800
        sys.exit()
801
802
803
def wait_func(iteration):
804
    """Wait for-in range Iteration.
805
806
    :param iteration: the amount of wait.
807
    """
808
809
    for _ in range(iteration):
810
        time.sleep(1)
811
        print(".")
812