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