Passed
Push — main ( 8fc681...73c4d6 )
by Máté
01:40 queued 38s
created

main.get_desired_name_of_quiz()   A

Complexity

Conditions 5

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 15
rs 9.2833
c 0
b 0
f 0
cc 5
nop 1
1
from argparse import ArgumentParser, Namespace
2
import logging
3
import os
4
import time
5
from urllib.parse import urlencode
6
import webbrowser
7
8
# future: delete the comment below when stubs for the package below are available
9
import pyperclip  # type: ignore
10
11
# future: report false positive to JetBrains developers
12
# noinspection PyPackages
13
from .grading_types import GradingType  # type: ignore
14
15
# noinspection PyPackages
16
from .quiz import Quiz  # type: ignore
17
18
# noinspection PyPackages
19
from .quiz_helpers import clear_terminal  # type: ignore
20
21
22
def main() -> None:
23
    args = parse_arguments()
24
    configure_logging(args.verbose)
25
    logging.getLogger(__name__).debug("Program started...")
26
27
    quiz_title = get_desired_name_of_quiz(args.new)
28
    quiz = Quiz(
29
        parent_article=get_name_of_parent_article(),
30
        title=quiz_title,
31
        grading=get_grading(),
32
    )
33
    quiz.import_files(
34
        directory=os.getcwd(),
35
        recursively=args.recursive,
36
    )
37
38
    quiz_wikitext = str(quiz)
39
    wiki_domain = "https://test.vik.wiki"
40
    webbrowser.open_new_tab(f"{wiki_domain}/index.php?title=Speciális:Belépés")
41
    input("Please log in to the wiki then press Enter to continue...")
42
    parameters_for_opening_edit = {
43
        "action": "edit",
44
        "summary": "Kvíz bővítése "
45
        "a https://github.com/gy-mate/moodle-to-vikwikiquiz segítségével importált Moodle-kvízekből",
46
    }
47
    clear_terminal()
48
    create_article(
49
        args, parameters_for_opening_edit, quiz_title, quiz_wikitext, wiki_domain
50
    )
51
    logging.getLogger(__name__).debug("Program finished!")
52
53
54
def parse_arguments() -> Namespace:
55
    parser = ArgumentParser()
56
    parser.add_argument(
57
        "-v", "--verbose", action="store_true", help="increase output verbosity"
58
    )
59
    parser.add_argument(
60
        "-n",
61
        "--new",
62
        action="store_true",
63
        help="Create a new quiz on vik.wiki by automatically opening an edit page for the new article.",
64
    )
65
    parser.add_argument(
66
        "-r",
67
        "--recursive",
68
        action="store_true",
69
        help="Import HTML files in the current directory recursively.",
70
    )
71
    return parser.parse_args()
72
73
74
def configure_logging(verbose: bool) -> None:
75
    if verbose:
76
        logging.basicConfig(
77
            encoding="utf-8",
78
            format='%(asctime)s [%(levelname)s] "%(pathname)s:%(lineno)d": %(message)s',
79
            level=logging.DEBUG,
80
        )
81
    else:
82
        logging.basicConfig(
83
            encoding="utf-8",
84
            format="[%(levelname)s]: %(message)s",
85
            level=logging.INFO,
86
        )
87
88
89
def get_name_of_parent_article() -> str:
90
    while True:
91
        try:
92
            input_name = input(
93
                f"\nPlease enter the name of the vik.wiki article on the corresponding course on then press Enter: "
94
            )
95
            if not input_name:
96
                raise ValueError("Nothing was entered!")
97
            return input_name
98
        except ValueError as error:
99
            print(error)
100
101
102
def get_desired_name_of_quiz(new: bool) -> str:
103
    while True:
104
        try:
105
            print(
106
                "\nPlease enter how the quiz should be named on vik.wiki then press Enter."
107
                "\nThis is usually in the following form: `[course name] kvíz – [exam name]`. (The ` – [exam name]` can be omitted.)"
108
            )
109
            if not new:
110
                print("This might be an existing article name.")
111
            input_name = input()
112
            if not input_name:
113
                raise ValueError("Nothing was entered!")
114
            return input_name
115
        except ValueError as error:
116
            print(error)
117
118
119
def get_grading() -> GradingType:
120
    while True:
121
        try:
122
            grading_symbol = input(
123
                "\nPlease enter `+` or `-` as the grading type of the quiz then press Enter.\n"
124
                "See https://vik.wiki/wiki/Segítség:Kvíz#Pontozás for further info.\n"
125
            )
126
            return GradingType(grading_symbol)
127
        except ValueError:
128
            print("This is not a valid grading type!")
129
130
131
def create_article(
132
    args: Namespace,
133
    parameters_for_opening_edit: dict,
134
    quiz_title: str,
135
    quiz_wikitext: str,
136
    wiki_domain: str,
137
) -> None:
138
    if args.new:
139
        parameters_for_opening_edit_with_paste = parameters_for_opening_edit.copy()
140
        parameters_for_opening_edit_with_paste.update(
141
            {
142
                "preload": "Sablon:Előbetöltés",
143
                "preloadparams[]": quiz_wikitext,
144
            }
145
        )
146
        parameters_for_opening_edit_with_paste["summary"] = (
147
            parameters_for_opening_edit_with_paste["summary"].replace(
148
                "bővítése", "létrehozása"
149
            )
150
        )
151
        url = f"{wiki_domain}/wiki/{quiz_title}?{urlencode(parameters_for_opening_edit_with_paste)}"
152
        if len(url) >= 2048:
153
            logging.getLogger(__name__).warning(
154
                "I can't create the article automatically "
155
                "because the URL would be too long for some browsers (or the server)."
156
            )
157
            if args.verbose:
158
                pyperclip.copy(url)
159
                print(
160
                    "This URL has been copied to the clipboard! "
161
                    "It will be overwritten but you may recall it later if you use an app like Pastebot."
162
                )
163
                wait_for_pastebot_to_recognize_copy()
164
            parameters_for_opening_edit["summary"] = parameters_for_opening_edit[
165
                "summary"
166
            ].replace("bővítése", "létrehozása")
167
        else:
168
            pyperclip.copy(quiz_wikitext)
169
            print(
170
                "The wikitext of the quiz has been copied to the clipboard! "
171
                "This will be overwritten but you may recall it later if you use an app like Pastebot."
172
            )
173
            wait_for_pastebot_to_recognize_copy()
174
            if args.verbose:
175
                pyperclip.copy(url)
176
                print("The URL has been copied to the clipboard!")
177
            webbrowser.open_new_tab(url)
178
            print(
179
                "The edit page of the new quiz article has been opened in your browser with the wikitext pre-filled!"
180
            )
181
            return
182
    pyperclip.copy(quiz_wikitext)
183
    print("The wikitext of the quiz has been copied to the clipboard!")
184
    url = f"{wiki_domain}/wiki/{quiz_title}?{urlencode(parameters_for_opening_edit)}"
185
    webbrowser.open_new_tab(url)
186
    print(
187
        "The edit page of the quiz article has been opened in your browser! Please paste the wikitext there manually."
188
    )
189
190
191
def wait_for_pastebot_to_recognize_copy() -> None:
192
    print("Waiting 2 seconds for Pastebot to recognize it...")
193
    time.sleep(2)
194
    print("...done!")
195
196
197
if __name__ == "__main__":
198
    main()
199