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

quiz.Quiz.import_files()   A

Complexity

Conditions 3

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 3
nop 2
1
import contextlib
2
3
# future: report false positive to JetBrains developers
4
# noinspection PyUnresolvedReferences
5
import os
6
7
# future: report false positive to JetBrains developers
8
# noinspection PyUnresolvedReferences
9
from bs4 import BeautifulSoup, Tag
10
11
# future: report false positive to JetBrains developers
12
# noinspection PyPackages
13
# future: report false positive to mypy developers
14
from .grading_types import GradingType  # type: ignore
15
16
# noinspection PyPackages
17
# future: report false positive to mypy developers
18
from .question_types import QuestionType  # type: ignore
19
20
# noinspection PyPackages
21
# future: report false positive to mypy developers
22
from .quiz_helpers import *  # type: ignore
23
24
# noinspection PyPackages
25
# future: report false positive to mypy developers
26
from .question import Question  # type: ignore
27
28
29
class Quiz:
30
    def __init__(
31
        self, parent_article: str, title: str, grading: GradingType | None = None
32
    ):
33
        self.parent_article = parent_article
34
        self.title = title
35
        self.grading = grading
36
37
        self.questions: set[Question] = set()
38
39
    def __str__(self) -> str:
40
        text = f"{{{{Vissza | {self.parent_article}}}}}"
41
        text += f"""
42
43
{{{{Kvízoldal
44
|cím={self.title}"""
45
        if self.grading:
46
            text += f"\n|pontozás={self.grading.value}"
47
        text += "\n}}"
48
        for question in self.questions:
49
            text += f"\n\n\n{question}"
50
        text += "\n"
51
        return text
52
53
    def import_files(self, directory: str) -> None:
54
        for subdir, dirs, files in os.walk(directory):
55
            for file in files:
56
                self.import_questions(file, subdir)
57
58
    def import_questions(self, file: str, subdir: str) -> None:
59
        file_path = os.path.join(subdir, file)
60
        with open(file_path, "rb") as source_file:
61
            webpage = BeautifulSoup(source_file, "html.parser")
62
63
            multi_or_single_choice_questions = webpage.find_all(
64
                "div", class_="multichoice"
65
            )
66
            for question in multi_or_single_choice_questions:
67
                self.import_question(
68
                    question=question, file_name=os.path.basename(file_path)
69
                )
70
                clear_terminal()  # type: ignore
71
72
    def import_question(self, question: Tag, file_name: str) -> None:
73
        with contextlib.suppress(NotImplementedError):
74
            question_type = get_question_type(question)  # type: ignore
75
        correctly_answered, grade, maximum_points = get_grading_of_question(question)  # type: ignore
76
        question_text = get_question_text(question)  # type: ignore
77
        answer_texts, correct_answers = get_answers(question, grade, maximum_points)  # type: ignore
78
        if not correctly_answered:
79
            complete_correct_answers(  # type: ignore
80
                answer_texts,
81
                correct_answers,
82
                grade,
83
                maximum_points,
84
                question_text,
85
                question_type,
86
                file_name,
87
            )
88
        has_illustration = get_if_has_illustration(question)  # type: ignore
89
        self.add_question_no_duplicates(
90
            question_type,
91
            question_text,
92
            has_illustration,
93
            answer_texts,
94
            correct_answers,
95
        )
96
97
    def add_question_no_duplicates(
98
        self,
99
        question_type: QuestionType,
100
        question_text: str,
101
        has_illustration: bool,
102
        answer_texts: list[str],
103
        correct_answers: list[int],
104
    ) -> None:
105
        for existing_question in self.questions:
106
            if question_already_exists(existing_question, question_text):  # type: ignore
107
                add_answers_to_existing_question(  # type: ignore
108
                    answer_texts, correct_answers, existing_question
109
                )
110
                break
111
        else:
112
            self.questions.add(
113
                Question(
114
                    q_type=question_type,
115
                    text=question_text,
116
                    illustration=has_illustration,
117
                    answers=answer_texts,
118
                    correct_answers=correct_answers,
119
                )
120
            )
121