Passed
Push — main ( 7be5d4...629e09 )
by Máté
01:10
created

src.quiz.Quiz.__init__()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 4
1
import os
2
3
from bs4 import BeautifulSoup, Tag
4
5
from src.grading_types import GradingType
6
from src.question import Question
7
from src.question_types import QuestionType
8
from src.quiz_helpers import add_answers_to_existing_question, complete_correct_answers, get_answers, \
9
    get_grading_of_question, get_question_text, \
10
    question_already_exists
11
12
13
class Quiz:
14
    def __init__(self, parent_article: str, title: str, grading: GradingType | None = None):
15
        self.parent_article = parent_article
16
        self.title = title
17
        self.grading = grading
18
        
19
        self.questions: set[Question] = set()
20
    
21
    def __str__(self) -> str:
22
        text = f"{{{{Vissza | {self.parent_article}}}}}"
23
        text += f"""
24
25
{{{{Kvízoldal
26
|cím={self.title}"""
27
        if self.grading:
28
            text += f"\n|pontozás={self.grading.value}"
29
        text += "\n}}"
30
        for question in self.questions:
31
            text += f"\n\n\n{question}"
32
        text += "\n"
33
        return text
34
    
35
    def import_files(self, directory: str) -> None:
36
        for subdir, dirs, files in os.walk(directory):
37
            for file in files:
38
                self.import_questions(file, subdir)
39
    
40
    def import_questions(self, file: str, subdir: str) -> None:
41
        file_path = os.path.join(subdir, file)
42
        with open(file_path, "rb") as source_file:
43
            webpage = BeautifulSoup(source_file, "html.parser")
44
            
45
            multichoice_questions = webpage.find_all("div", class_="multichoice")
46
            for question in multichoice_questions:
47
                self.import_question(question)
48
    
49
    def import_question(self, question: Tag) -> None:
50
        correctly_answered, grade, maximum_points = get_grading_of_question(question)
51
        question_text = get_question_text(question)
52
        answer_texts, correct_answers = get_answers(question)
53
        if not correctly_answered:
54
            complete_correct_answers(answer_texts, correct_answers, grade, maximum_points, question_text)
55
        has_illustration = True if question.find("img", class_="img-responsive") else False
56
        self.add_question_no_duplicates(answer_texts, correct_answers, has_illustration, question_text)
57
    
58
    def add_question_no_duplicates(self, answer_texts: list[str], correct_answers: list[int], has_illustration: bool,
59
                                   question_text: str) -> None:
60
        for existing_question in self.questions:
61
            if question_already_exists(existing_question, question_text):
62
                add_answers_to_existing_question(answer_texts, correct_answers, existing_question)
63
                break
64
        else:
65
            self.questions.add(
66
                Question(q_type=QuestionType.MultipleChoice, text=question_text,
67
                         illustration=has_illustration,
68
                         answers=answer_texts,
69
                         correct_answers=correct_answers))
70