diff --git a/tetras_extraction/macao_12/script/extract_page.py b/tetras_extraction/macao_12/script/extract_page.py index e38e2df685eca85971d4db01030f26232a69197f..d9b1121341c326b6145eee6f531ecaffddf2c4c4 100644 --- a/tetras_extraction/macao_12/script/extract_page.py +++ b/tetras_extraction/macao_12/script/extract_page.py @@ -50,14 +50,12 @@ class RegexParser: raise ParseError("Failed to find function 'entrerDonnees'") body = func_split[1] - activity_type, activity_var_name = self.parse_activity_constructor(body) - print(activity_type, file=output) + activity_type, activity_var_name = self._parse_activity_constructor(body) + print(activity_type, end="", file=output) + if activity_type == "ExerciceQC_QCU": + print(" ", self._parse_qcu_answers(body), end="", file=output) - for line in body.splitlines(): - line = line.strip() - pass - - def parse_activity_constructor(self, code: str) -> tuple[str, str]: + def _parse_activity_constructor(self, code: str) -> tuple[str, str]: """ Find activity constructor call, return the activity type and resulting variable name. @@ -80,6 +78,24 @@ class RegexParser: act_type += "_" + args.replace('"', "") return act_type, var_name + def _parse_qcu_answers(self, code: str) -> list[bool]: + """Parse the correct answers for a QCU activity, as a list of booleans""" + correct_choices = [] + index = 0 + for line in code.splitlines(): + line = line.strip() + m = re.match(r"var nr = (\d+);", line) + if m is not None: + # "index" line + index = int(m.group(1)) + elif line == "exo.tabStylesR[nr] = CODE_F;": + # "incorrect answer" line + insert_grow(correct_choices, index, False, fill_value=False) + elif line == "exo.tabStylesR[nr] = CODE_V;": + # "correct answer" line + insert_grow(correct_choices, index, True, fill_value=False) + return correct_choices + def __str__(self) -> str: return "RegexParser" @@ -108,8 +124,10 @@ class XpathParser: xml = self.to_xml(jstree.toDict(), "jstree") try: self.fun = self.request_function(xml)[0] - print(self._parse_activity_type(), file=output) - # self._parse_qcu_choices() + act_type = self._parse_activity_type() + print(act_type, end="", file=output) + if act_type == "ExerciceQC_QCU": + print(" ", self._parse_qcu_answers(), end="", file=output) except Exception as e: raise ParseError(e) @@ -124,18 +142,20 @@ class XpathParser: case other: return other - def _parse_qcu_choices(self): + def _parse_qcu_answers(self) -> list[bool]: + """Parse the correct answers for a QCU activity, as a list of booleans""" indexes_and_values = self.request_index_and_values(self.fun) correct_choices = [] + index = 0 for e in indexes_and_values: value = e.xpath("@value") if len(value) != 0: # "index line" - index = value[0] + index = int(value[0]) else: # "true line" insert_grow(correct_choices, index, True, fill_value=False) - print(correct_choices) + return correct_choices def to_xml(self, obj, tag_name: Optional[str] = None): """Recursively convert an object structure to an XML `ElementTree`. @@ -242,7 +262,7 @@ class MatchParser: pass def print(self, s: str): - print(s, file=self.output) + print(s, end="", file=self.output) def match_function(self, func: dict): """Checks if `func` matches a function declaration named `entrerDonnees`, @@ -301,7 +321,7 @@ def parse_page(graph: Graph, filepath: str, id: str): # Try different parsers, each writing to a different file to compare their results for parser in [XpathParser(), MatchParser(graph, id), RegexParser()]: with open(f"/tmp/{str(parser)}.txt", "a") as f: - print(f"{id:8}", end="", file=f) + print(f"\n{id:8}", end="", file=f) try: parser.parse(js, output=f) except ParseError as e: