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