Passed
Branch main (462934)
by Máté
01:15
created

main   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 98
dl 0
loc 144
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A configure_logging() 0 12 2
B main() 0 97 6
A wait_for_pastebot_to_recognize_copy() 0 4 1
1
from argparse import ArgumentParser
2
import logging
3
import sys
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
    parser = ArgumentParser()
24
    parser.add_argument(
25
        "-v", "--verbose", action="store_true", help="increase output verbosity"
26
    )
27
    parser.add_argument(
28
        "source_directory",
29
        help="The absolute path of the directory where the Moodle quiz HTML files are located. "
30
        "These HTML files should contain the 'Review' page of the quizzes.",
31
    )
32
    parser.add_argument(
33
        "parent_article", help="The article name of the course on vik.wiki."
34
    )
35
    parser.add_argument(
36
        "-n",
37
        "--new",
38
        action="store_true",
39
        help="Create a new quiz on vik.wiki by automatically opening an edit page for the new article.",
40
    )
41
    parser.add_argument(
42
        "title",
43
        help="How the quiz should be named on [vik.wiki](https://vik.wiki/). "
44
        "This usually is in the following form: `[course name] kvíz – [exam name]`. "
45
        "The hyphen and the part after it can be omitted.",
46
    )
47
    parser.add_argument(
48
        "-g",
49
        "--grading",
50
        help="`+` or `-`. See https://vik.wiki/wiki/Segítség:Kvíz#Pontozás for further info.",
51
    )
52
    args = parser.parse_args()
53
54
    configure_logging(args.verbose)
55
    logging.getLogger(__name__).debug("Program started...")
56
57
    full_source_directory = args.source_directory
58
    if args.grading == "+":
59
        grading = GradingType.Kind
60
    elif args.grading == "-":
61
        grading = GradingType.Strict
62
    else:
63
        grading = None
64
    quiz = Quiz(parent_article=args.parent_article, title=args.title, grading=grading)
65
    quiz.import_files(full_source_directory)
66
67
    quiz_wikitext = str(quiz)
68
    wiki_domain = "https://test.vik.wiki"
69
    webbrowser.open_new_tab(f"{wiki_domain}/index.php?title=Speciális:Belépés")
70
    input("Please log in to the wiki then press Enter to continue...")
71
    parameters_for_opening_edit = {
72
        "action": "edit",
73
        "summary": "Kvíz létrehozása "
74
        "a https://github.com/gy-mate/moodle-to-vikwikiquiz segítségével importált Moodle-kvízekből",
75
    }
76
    clear_terminal()
77
    if args.new:
78
        parameters_for_opening_edit_with_paste = parameters_for_opening_edit.copy()
79
        parameters_for_opening_edit_with_paste.update(
80
            {
81
                "preload": "Sablon:Előbetöltés",
82
                "preloadparams[]": quiz_wikitext,
83
            }
84
        )
85
        url = f"{wiki_domain}/wiki/{args.title}?{urlencode(parameters_for_opening_edit_with_paste)}"
86
        if len(url) >= 2048:
87
            logging.getLogger(__name__).warning(
88
                "I can't create the article automatically "
89
                "because the URL would be too long for some browsers (or the server)."
90
            )
91
            if args.verbose:
92
                pyperclip.copy(url)
93
                print(
94
                    "This URL has been copied to the clipboard! "
95
                    "It will be overwritten but you may recall it later if you use an app like Pastebot."
96
                )
97
                wait_for_pastebot_to_recognize_copy()
98
        else:
99
            webbrowser.open_new_tab(url)
100
            print(
101
                "The edit page of the new quiz article has been opened in your browser!"
102
            )
103
            pyperclip.copy(quiz_wikitext)
104
            print(
105
                "The wikitext of the quiz has been copied to the clipboard! "
106
                "This will be overwritten but you may recall it later if you use an app like Pastebot."
107
            )
108
            wait_for_pastebot_to_recognize_copy()
109
            pyperclip.copy(url)
110
            print("The URL has been copied to the clipboard!")
111
    pyperclip.copy(quiz_wikitext)
112
    print("The wikitext of the quiz has been copied to the clipboard!")
113
    url = f"{wiki_domain}/wiki/{args.title}?{urlencode(parameters_for_opening_edit)}"
114
    webbrowser.open_new_tab(url)
115
    print(
116
        "The edit page of the quiz article has been opened in your browser! Please paste the wikitext there manually."
117
    )
118
    logging.getLogger(__name__).debug("...program finished!")
119
120
121
def wait_for_pastebot_to_recognize_copy():
122
    print("Waiting 2 seconds for Pastebot to recognize it...")
123
    time.sleep(2)
124
    print("...done!")
125
126
127
def configure_logging(verbose: bool) -> None:
128
    if verbose:
129
        logging.basicConfig(
130
            encoding="utf-8",
131
            format='%(asctime)s [%(levelname)s] "%(pathname)s:%(lineno)d": %(message)s',
132
            level=logging.DEBUG,
133
        )
134
    else:
135
        logging.basicConfig(
136
            encoding="utf-8",
137
            format="[%(levelname)s]: %(message)s",
138
            level=logging.INFO,
139
        )
140
141
142
if __name__ == "__main__":
143
    main()
144