Passed
Push — main ( 52c180...0d5c56 )
by Máté
01:52
created

main   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 114
dl 0
loc 154
rs 10
c 0
b 0
f 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A get_name_of_parent_article() 0 11 4
A configure_logging() 0 12 2
A get_grading() 0 12 3
A parse_arguments() 0 26 1
A get_desired_name_of_quiz() 0 15 5
B main() 0 50 3
1
from argparse import ArgumentParser, Namespace
2
import logging
3
from pathlib import Path
4
from sys import version_info
5
6
from .wiki import create_article, get_article_instructions, log_in_to_wiki  # type: ignore
7
from .quiz.illustrations.state_of_illustrations import StateOfIllustrations  # type: ignore
8
from .quiz.grading_types import GradingType  # type: ignore
9
from .quiz.quiz import Quiz  # type: ignore
10
from .helpers import clear_terminal, wait_for_pastebot_to_recognize_copy  # type: ignore
11
12
13
def main() -> None:
14
    # future: remove the conditional below when https://github.com/linkedin/shiv/issues/268 is fixed
15
    if version_info < (3, 12):
16
        raise SystemError(
17
            "This app requires Python 3.12 or later. Please upgrade it from https://www.python.org/downloads/!"
18
        )
19
20
    args = parse_arguments()
21
    configure_logging(args.verbose)
22
    logging.getLogger(__name__).debug("Program started...")
23
24
    quiz_title = get_desired_name_of_quiz(args.new)
25
    if args.new:
26
        parent_article = get_name_of_parent_article()
27
        grading = get_grading()
28
    else:
29
        parent_article = None
30
        grading = None
31
    quiz = Quiz(
32
        title=quiz_title,
33
        parent_article=parent_article,
34
        grading=grading,
35
    )
36
    absolute_source_path: Path = args.source_path.resolve()
37
    quiz.import_file_or_files(
38
        source_path=absolute_source_path,
39
        recursively=args.recursive,
40
    )
41
    wiki_domain = "https://vik.wiki"
42
    log_in_to_wiki(wiki_domain)
43
44
    print("Great!\n")
45
    (
46
        operating_system,
47
        parameters_for_opening_edit,
48
        wiki_editor_keys,
49
        wiki_modifier_keys,
50
    ) = get_article_instructions(quiz, wiki_domain)
51
52
    create_article(
53
        args,
54
        parameters_for_opening_edit,
55
        quiz_title,
56
        str(quiz),
57
        wiki_domain,
58
        wiki_modifier_keys,
59
        wiki_editor_keys,
60
        operating_system,
61
    )
62
    logging.getLogger(__name__).debug("Program finished!")
63
64
65
def parse_arguments() -> Namespace:
66
    parser = ArgumentParser(
67
        "Convert graded and downloaded Moodle quizzes to a vik.viki quiz wikitext."
68
    )
69
    parser.add_argument(
70
        "-v", "--verbose", action="store_true", help="increase output verbosity"
71
    )
72
    parser.add_argument(
73
        "-n",
74
        "--new",
75
        action="store_true",
76
        help="create a new quiz on vik.wiki by automatically opening an edit page for the new article",
77
    )
78
    parser.add_argument(
79
        "-r",
80
        "--recursive",
81
        action="store_true",
82
        help="import HTML files from the current directory recursively",
83
    )
84
    parser.add_argument(
85
        "source_path",
86
        type=Path,
87
        help="The absolute or relative path of the file or directory where the Moodle quiz HTML files are located. "
88
        "These HTML files should contain the 'Review' page of the quizzes.",
89
    )
90
    return parser.parse_args()
91
92
93
def configure_logging(verbose: bool) -> None:
94
    if verbose:
95
        logging.basicConfig(
96
            encoding="utf-8",
97
            format='%(asctime)s [%(levelname)s] "%(pathname)s:%(lineno)d": %(message)s',
98
            level=logging.DEBUG,
99
        )
100
    else:
101
        logging.basicConfig(
102
            encoding="utf-8",
103
            format="[%(levelname)s]: %(message)s",
104
            level=logging.INFO,
105
        )
106
107
108
def get_name_of_parent_article() -> str:
109
    while True:
110
        try:
111
            input_name = input(
112
                f"\nPlease enter the name of the vik.wiki article of the corresponding course then press Enter:\n"
113
            )
114
            if not input_name:
115
                raise ValueError("Nothing was entered!")
116
            return input_name
117
        except ValueError as error:
118
            print(error)
119
120
121
def get_desired_name_of_quiz(new: bool) -> str:
122
    while True:
123
        try:
124
            print(
125
                "\nPlease enter how the quiz should be named on vik.wiki then press Enter!"
126
                "\nThis is usually in the following form: `[course name] kvíz – [exam name]`. (The ` – [exam name]` can be omitted.)"
127
            )
128
            if not new:
129
                print("This might be an existing article name.")
130
            input_name = input()
131
            if not input_name:
132
                raise ValueError("Nothing was entered!")
133
            return input_name
134
        except ValueError as error:
135
            print(error)
136
137
138
def get_grading() -> GradingType:
139
    while True:
140
        try:
141
            grading_symbol = input(
142
                "\nPlease enter `+` or `-` as the grading type of the quiz then press Enter!"
143
                "\nSee https://vik.wiki/Segítség:Kvíz#Pontozás for further info.\n"
144
            )
145
            return GradingType(grading_symbol)
146
        except ValueError:
147
            print("This is not a valid grading type!")
148
        finally:
149
            clear_terminal()
150
151
152
if __name__ == "__main__":
153
    main()
154