Completed
Push — master ( cd091a...aaafb1 )
by Sepand
01:52
created

main_handler_2()   B

Complexity

Conditions 3

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 12
Bugs 3 Features 4
Metric Value
cc 3
c 12
b 3
f 4
dl 0
loc 31
rs 8.8571
1
from qpage import *
2
import sys
3
import gc
4
5
6
def error_handler():
7
    """ Close files and check errors and start again main
8
        call:
9
        -close_files
10
        -error_finder
11
        -Show_items
12
        -enter_to_exit
13
        -main_handler
14
    """
15
    close_files()  # Close all of the open files
16
    vector_2 = error_finder()  # load error and pass vector
17
    error_vector = vector_2[0]  # extract errors
18
    pass_vector = vector_2[1]  # extract pass
19
    print(str(len(error_vector)) + " Error")  # print  number of errors
20
    print("Please Check Following :\n")
21
    show_items(error_vector)  # print error
22
    for i, item in enumerate(pass_vector):
23
        print(str(i + len(error_vector) + 1) + "-" + item)
24
    enter_to_exit()  # get input from user to continue
25
    main_handler()
26
27
28
def file_handler():
29
    """ Write files
30
        call:
31
        -html_init
32
        -contain
33
        -css_creator
34
        -icon_creator
35
        -robot_maker
36
        -close_files
37
    """
38
    for i in ACTUAL_NAME:
39
        html_init(i)  # create pages html files
40
    menu_writer()  # write menu for each html file
41
    for i in ACTUAL_NAME:
42
        contain(i)  # write contains of each page
43
        html_end(i)  # end tags of each page
44
    css_creator()  # create css file
45
    icon_creator()
46
    robot_maker()
47
    close_files()
48
49
50
def main_handler_2(time_1=0):
51
    """ Second part of main handler
52
53
    :param time_1: time that passed but not counted in generation time
54
55
    call:
56
        -file_handler
57
        -address_print
58
        -print_warning
59
        -file_size
60
        -internet
61
        -server
62
        -preview
63
        -close_files
64
    """
65
    file_handler()  # call file_handler
66
    total_perf_time = generation_time(time_1)
67
    print("HOMEPAGE is ready,generated in " + str(total_perf_time) + " sec")
68
    print("Upload output folder contains directly to your host")
69
    print("Please Don't Change HTML Files Name")
70
    address_print()  # print files location
71
    print_warning()  # print all of the detected warnings
72
    file_size()
73
    logger(True, perf_time=total_perf_time)  # add success run of qpage to local logger
74
    if internet():  # check internet connection
75
        server()  # send query to qpage server
76
    browse = int(input("Preview HOMEPAGE?[1] or Not[2]"))  # get input from user for preview of site
77
    if browse == 1:  # check browse status
78
        preview()  # call preview function
79
        close_files()  # close all of the open files
80
    gc.collect()
81
82
83
def response_handler(response):
84
    """ Calculate the generation time
85
86
    :param response: if there was a response run main handler again
87
    call:
88
        -wait_func
89
        -main_handler
90
    """
91
    if response:  # check response status
92
        print(
93
            "At least one of the folders create for the first time ,\n"
94
            " please put your data in proper order and run program again\n Program Reboot Automatically in 3 Sec")
95
        wait_func(3)  # wait for 3 sec
96
        main_handler(False)  # call main_handler again with False VERSION control flag
97
        sys.exit()  # exit program
98
99
100
def sample_handler():
101
    """ Ask for run sample website
102
        call:
103
        -sample_site_download
104
        -is_sample_downloaded
105
    """
106
    # Get Input form user for loading sample files or continue
107
    response = input(
108
        "Press [S] to enter sample site material running or other keys to continue with your data")
109
    print_line(70)  # print line
110
    if response.upper() == "S":  # check response status
111
        sample_site_download(is_sample_downloaded())  # Call sample download
112
113
114
def main_handler(control_flag=True):
115
    """ Main Handler
116
117
    :param control_flag: Check if VERSION control passed in prev step then Check for new VERSION of qpage
118
        call:
119
        -generation_time
120
        -create_folder
121
        -print_logo
122
        -address_print
123
        -version_control
124
        -sample_handler
125
        -response_handler
126
        -page_name_update
127
        -main_handler_2
128
        -error_log
129
        -logger
130
        -close_files
131
        -enter_to_exi
132
133
134
    """
135
    try:
136
        start_time = generation_time()
137
        response = create_folder()  # Check Folder and Files Status
138
        print("QPAGE By S.Haghighi & M.M.Rahimi")
139
        print("VERSION : " + VERSION)
140
        print_logo()
141
        address_print()  # Print Files Location
142
        if control_flag:  # Check if VERSION control passed in prev step
143
            version_control()  # Check for new VERSION of qpage
144
        response_handler(response)  # call response_handler
145
        sample_handler()  # run sample handler
146
        clear_folder(OUT_DIR)  # clear all of files in output directory
147
        page_name_update()  # update page names
148
        main_handler_2(time_1=start_time)  # call part_2 of main_handler
149
    except FileNotFoundError as e:  # error exception in FileNotFound ( When Something Missed)
150
        error_log(e)
151
        logger(False)  # Add Failed Run to local logger file
152
        error_handler()  # call error_handler
153
    except ValueError as e:
154
        error_log(e)
155
        print("Bad Input")
156
        logger(False)  # Add Failed Run to local logger file
157
        close_files()  # Close all of the opne files
158
        enter_to_exit()  # get input from user to continue
159
        main_handler()  # call part_1 of main_handler , restart from the first
160
    except PermissionError as e:
161
        error_log(e)
162
        logger(False)  # Add Failed Run to local logger file
163
        print("Files Is Open By Another Program")
164
        close_files()  # Close all of the open files
165
        enter_to_exit()  # get input from user to continue
166
        main_handler()  # call part_1 of main_handler , resetart from the first
167
168
169
if __name__ == "__main__":
170
    main_handler()
171