-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
745 lines (607 loc) · 30.9 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# This file is part of the QuestionPy SDK. (https://questionpy.org)
# The QuestionPy SDK is free software released under terms of the MIT license. See LICENSE.md.
# (c) Technische Universität Berlin, innoCampus <[email protected]>
from __future__ import annotations
import re
from random import Random
from typing import Any
import lxml.html
import lxml.html.clean
from lxml import etree
from pydantic import BaseModel
from questionpy_common.api.attempt import DisplayRole
from questionpy_sdk.webserver.question_ui.errors import (
ConversionError,
ExpectedAncestorError,
InvalidAttributeValueError,
InvalidCleanOptionError,
InvalidContentError,
PlaceholderReferenceError,
RenderErrorCollection,
UnknownAttributeError,
UnknownElementError,
XMLSyntaxError,
)
_XHTML_NAMESPACE: str = "http://www.w3.org/1999/xhtml"
_QPY_NAMESPACE: str = "http://questionpy.org/ns/question"
def _assert_element_list(query: Any) -> list[etree._Element]:
"""Checks if the XPath query result is a list of Elements.
- If it is, returns the list.
- Otherwise, raises an error.
Args:
query: The result of an XPath query.
Returns:
list: The result of the XPath query.
Raises:
TypeError: If the result is not a list.
"""
if not isinstance(query, list):
msg = "XPath query result is not a list."
raise TypeError(msg)
return query
def _set_element_value(element: etree._Element, value: str, name: str, xpath: etree.XPathDocumentEvaluator) -> None:
"""Sets value on user input element.
Args:
element: XHTML element to set value on.
value: Value to set.
name: Element name.
xpath: XPath evaluator.
"""
type_attr = element.get("type", "text") if element.tag.endswith("}input") else etree.QName(element).localname
if type_attr in {"checkbox", "radio"}:
if element.get("value") == value:
element.set("checked", "checked")
elif type_attr == "select":
# Iterate over child <option> elements to set 'selected' attribute
for option in _assert_element_list(xpath(f".//xhtml:option[parent::xhtml:select[@name='{name}']]")):
opt_value = option.get("value") if option.get("value") is not None else option.text
if opt_value == value:
option.set("selected", "selected")
break
elif type_attr == "textarea":
element.text = value
elif type_attr not in {"button", "submit", "hidden"}:
element.set("value", value)
def _check_shuffled_index_is_in_nested_shuffle_contents(
container: etree._Element, index_element: etree._Element
) -> bool:
ancestor = index_element.getparent()
while ancestor is not None and ancestor != container:
if f"{{{_QPY_NAMESPACE}}}shuffle-contents" in ancestor.attrib:
return True
ancestor = ancestor.getparent()
return False
def _replace_shuffled_indices(container: etree._Element, element: etree._Element, index: int) -> None:
for index_element in _assert_element_list(
element.xpath(".//qpy:shuffled-index", namespaces={"qpy": _QPY_NAMESPACE})
):
if _check_shuffled_index_is_in_nested_shuffle_contents(container, index_element):
# The index element is in a nested shuffle-contents.
# We want it to be replaced with the index of the inner shuffle, so we ignore it for now.
continue
format_style = index_element.get("format", "123")
if format_style == "123":
index_str = str(index)
elif format_style == "abc":
index_str = _int_to_letter(index).lower()
elif format_style == "ABC":
index_str = _int_to_letter(index).upper()
elif format_style == "iii":
index_str = _int_to_roman(index).lower()
elif format_style == "III":
index_str = _int_to_roman(index).upper()
else:
index_str = str(index)
# Replace the index element with the new index string
new_text_node = etree.Element("span") # Using span to replace the custom element
new_text_node.text = index_str
if index_element.tail:
new_text_node.tail = index_element.tail
parent = index_element.getparent()
if parent is not None:
parent.replace(index_element, new_text_node)
def _int_to_letter(index: int) -> str:
"""Converts an integer to its corresponding letter (1 -> a, 2 -> b, etc.)."""
return chr(ord("a") + index - 1)
def _int_to_roman(index: int) -> str:
"""Converts an integer to its Roman numeral representation. Simplified version."""
val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
syb = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
roman_num = ""
i = 0
while index > 0:
for _ in range(index // val[i]):
roman_num += syb[i]
index -= val[i]
i += 1
return roman_num
def _add_text_before(before: etree._Element, text: str) -> None:
"""Add plain text before the given sibling.
LXML doesn't represent text as nodes, but as attributes of the parent or of the preceding node, which makes this
less trivial than one might expect.
"""
prev = before.getprevious()
if prev is None:
parent = _require_parent(before)
# The parent's 'text' attribute sets the text before the first child.
parent.text = ("" if parent.text is None else parent.text) + text
else:
prev.tail = ("" if prev.tail is None else prev.tail) + text
def _require_parent(node: etree._Element) -> etree._Element:
parent = node.getparent()
if parent is None:
msg = f"Node '{node}' on line '{node.sourceline}' somehow has no parent."
raise ValueError(msg)
return parent
def _remove_element(node: etree._Element) -> None:
_require_parent(node).remove(node)
def _remove_preserving_tail(node: etree._Element) -> None:
if node.tail is not None:
_add_text_before(node, node.tail)
_remove_element(node)
class QuestionMetadata:
def __init__(self) -> None:
self.correct_response: dict[str, str] = {}
self.expected_data: dict[str, str] = {}
self.required_fields: list[str] = []
class QuestionDisplayOptions(BaseModel):
general_feedback: bool = True
specific_feedback: bool = True
right_answer: bool = True
roles: set[DisplayRole] = {
DisplayRole.DEVELOPER,
DisplayRole.PROCTOR,
DisplayRole.SCORER,
DisplayRole.TEACHER,
}
readonly: bool = False
class QuestionUIRenderer:
"""General renderer for the question UI except for the formulation part."""
def __init__(
self,
xml: str,
placeholders: dict[str, str],
options: QuestionDisplayOptions,
seed: int | None = None,
attempt: dict | None = None,
) -> None:
self._html: str | None = None
xml = self._replace_qpy_urls(xml)
self._error_collector = _RenderErrorCollector(xml, placeholders)
try:
root = etree.fromstring(xml)
except etree.XMLSyntaxError:
parser = etree.XMLParser(recover=True)
root = etree.fromstring(xml, parser=parser)
self._xml = etree.ElementTree(root)
self._xpath = etree.XPathDocumentEvaluator(self._xml)
self._xpath.register_namespace("xhtml", _XHTML_NAMESPACE)
self._xpath.register_namespace("qpy", _QPY_NAMESPACE)
self._placeholders = placeholders
self._options = options
self._random = Random(seed)
self._attempt = attempt
def render(self) -> tuple[str, RenderErrorCollection]:
"""Applies transformations to the xml.
Returns:
tuple: The rendered html and a render errors collection.
"""
if self._html is None:
self._resolve_placeholders()
self._hide_unwanted_feedback()
self._hide_if_role()
self._set_input_values_and_readonly()
self._soften_validation()
self._defuse_buttons()
self._shuffle_contents()
self._add_styles()
self._format_floats()
# TODO: mangle_ids_and_names
self._clean_up()
self._html = etree.tostring(self._xml, pretty_print=True, method="html").decode()
self._error_collector.collect()
return self._html, self._error_collector.errors
def _replace_qpy_urls(self, xml: str) -> str:
"""Replace QPY-URLs to package files with SDK-URLs."""
return re.sub(r"qpy://(static|static-private)/((?:[a-z_][a-z0-9_]{0,126}/){2})", r"/worker/\2file/\1/", xml)
def _resolve_placeholders(self) -> None:
"""Replace placeholder PIs such as `<?p my_key plain?>` with the appropriate value from `self.placeholders`.
TODD: remove comment or change call-order
Since QPy transformations should not be applied to the content of the placeholders, this method should be called
last.
"""
for p_instruction in _assert_element_list(self._xpath("//processing-instruction('p')")):
if not p_instruction.text:
_remove_element(p_instruction)
continue
parts = p_instruction.text.strip().split()
key = parts[0]
clean_option = parts[1].lower() if len(parts) > 1 else "clean"
parent = p_instruction.getparent()
if parent is None:
continue
if key not in self._placeholders:
parent.remove(p_instruction)
continue
raw_value = self._placeholders[key]
if clean_option == "plain":
# Treat the value as plain text.
_add_text_before(p_instruction, raw_value)
else:
# html.clean works on different element classes than etree, so we need to use different parse functions.
# Since the HTML elements are subclasses of the etree elements though, we can reuse them without dumping
# and reparsing.
# It doesn't really matter what element we wrap the fragment with, as we'll unwrap it immediately.
fragment = lxml.html.fragment_fromstring(raw_value, create_parent=True)
if clean_option != "noclean":
lxml.html.clean.clean(fragment)
if fragment.text is not None:
_add_text_before(p_instruction, fragment.text)
for child in fragment:
p_instruction.addprevious(child)
_remove_preserving_tail(p_instruction)
def _hide_unwanted_feedback(self) -> None:
"""Hides elements marked with `qpy:feedback` if the type of feedback is disabled in `options`."""
for element in _assert_element_list(self._xpath("//*[@qpy:feedback]")):
feedback_type = element.get(f"{{{_QPY_NAMESPACE}}}feedback")
# Check conditions to remove the element
if not (
(feedback_type == "general" and self._options.general_feedback)
or (feedback_type == "specific" and self._options.specific_feedback)
):
_remove_element(element)
def _hide_if_role(self) -> None:
"""Hides elements based on user role.
Removes elements with `qpy:if-role` attributes if the user matches none of the roles.
"""
for element in _assert_element_list(self._xpath("//*[@qpy:if-role]")):
if attr := element.get(f"{{{_QPY_NAMESPACE}}}if-role"):
allowed_roles = [role.upper() for role in re.split(r"[\s|]+", attr)]
has_role = any(role in allowed_roles and role in self._options.roles for role in DisplayRole)
if not has_role and (parent := element.getparent()) is not None:
parent.remove(element)
def _set_input_values_and_readonly(self) -> None:
"""Transforms input(-like) elements.
- If `options` is set, the input is disabled.
- If a value was saved for the input in a previous step, the latest value is added to the HTML.
Requires the unmangled name of the element, so must be called `before` `mangle_ids_and_names`
"""
elements = _assert_element_list(
self._xpath("//xhtml:button | //xhtml:input | //xhtml:select | //xhtml:textarea")
)
for element in elements:
# Disable the element if options specify readonly
if self._options.readonly:
element.set("disabled", "disabled")
name = element.get("name")
if not name or not self._attempt:
continue
last_value = self._attempt.get(name)
if last_value is not None:
_set_element_value(element, last_value, name, self._xpath)
def _soften_validation(self) -> None:
"""Replaces HTML attributes so that submission is not prevented.
Removes attributes `pattern`, `required`, `minlength`, `maxlength`, `min`, `max` from elements, so form
submission is not affected. The standard attributes are replaced with `data-qpy_X`, which are then evaluated in
JavaScript.
"""
def handle_attribute(
elements: list[str], attribute: str, data_attribute: str, aria_attribute: str | None = None
) -> None:
xhtml_elems = " | ".join(f".//xhtml:{elem}" for elem in elements)
element_list = _assert_element_list(self._xpath(f"({xhtml_elems})[@{attribute}]"))
for element in element_list:
value = element.get(attribute)
element.attrib.pop(attribute)
if value:
value = "true" if value == attribute else value
element.set(data_attribute, value)
if aria_attribute:
element.set(aria_attribute, value)
# 'pattern' attribute for <input> elements
handle_attribute(["input"], "pattern", "data-qpy_pattern")
# 'required' attribute for <input>, <select>, <textarea> elements
handle_attribute(["input", "select", "textarea"], "required", "data-qpy_required", "aria-required")
# 'minlength'/'maxlength' attribute for <input>, <textarea> elements
handle_attribute(["input", "textarea"], "minlength", "data-qpy_minlength")
handle_attribute(["input", "textarea"], "maxlength", "data-qpy_maxlength")
# 'min'/'max' attributes for <input> elements
handle_attribute(["input"], "min", "data-qpy_min", "aria-valuemin")
handle_attribute(["input"], "max", "data-qpy_max", "aria-valuemax")
def _defuse_buttons(self) -> None:
"""Turns submit and reset buttons into simple buttons without a default action."""
for element in _assert_element_list(
self._xpath("(//xhtml:input | //xhtml:button)[@type = 'submit' or @type = 'reset']")
):
element.set("type", "button")
def _shuffle_contents(self) -> None:
"""Shuffles children of elements marked with `qpy:shuffle-contents`.
Also replaces `qpy:shuffled-index` elements which are descendants of each child with the new index of the child.
"""
for element in _assert_element_list(self._xpath("//*[@qpy:shuffle-contents]")):
# Collect child elements to shuffle them
child_elements = [child for child in element if isinstance(child, etree._Element)]
self._random.shuffle(child_elements)
# Reinsert shuffled elements, preserving non-element nodes
for i, child in enumerate(child_elements, 1):
_replace_shuffled_indices(element, child, i)
# Move each child element back to its parent at the correct position
element.append(child)
element.attrib.pop(f"{{{_QPY_NAMESPACE}}}shuffle-contents")
def _clean_up(self) -> None:
"""Removes remaining QuestionPy elements and attributes as well as comments and xmlns declarations."""
for element in _assert_element_list(self._xpath("//qpy:*")):
_remove_element(element)
# Remove attributes in the QuestionPy namespace
for element in _assert_element_list(self._xpath("//*")):
qpy_attributes = [attr for attr in element.attrib if attr.startswith(f"{{{_QPY_NAMESPACE}}}")] # type: ignore[arg-type]
for attr in qpy_attributes:
del element.attrib[attr]
# Remove comments
for comment in _assert_element_list(self._xpath("//comment()")):
_remove_element(comment)
# Remove namespaces from all elements. (QPy elements should all have been consumed previously anyhow.)
for element in _assert_element_list(self._xpath("//*")):
qname = etree.QName(element)
if qname.namespace == _XHTML_NAMESPACE:
element.tag = qname.localname
etree.cleanup_namespaces(self._xml, top_nsmap={None: _XHTML_NAMESPACE}) # type: ignore[dict-item]
def _add_class_names(self, element: etree._Element, *class_names: str) -> None:
"""Adds the given class names to the elements `class` attribute if not already present."""
existing_classes = element.get("class", "").split()
for class_name in class_names:
if class_name not in existing_classes:
existing_classes.append(class_name)
element.set("class", " ".join(existing_classes))
def _add_styles(self) -> None:
"""Adds CSS classes to various elements."""
# First group: input (not checkbox, radio, button, submit, reset), select, textarea
for element in _assert_element_list(
self._xpath("""
//xhtml:input[@type != 'checkbox' and @type != 'radio' and
@type != 'button' and @type != 'submit' and @type != 'reset']
| //xhtml:select | //xhtml:textarea
""")
):
self._add_class_names(element, "form-control", "qpy-input")
# Second group: input (button, submit, reset), button
for element in _assert_element_list(
self._xpath("""
//xhtml:input[@type = 'button' or @type = 'submit' or @type = 'reset']
| //xhtml:button
""")
):
self._add_class_names(element, "btn", "btn-primary", "qpy-input")
# Third group: input (checkbox, radio)
for element in _assert_element_list(self._xpath("//xhtml:input[@type = 'checkbox' or @type = 'radio']")):
self._add_class_names(element, "qpy-input")
def _format_floats(self) -> None:
"""Handles `qpy:format-float`.
Uses `format_float` and optionally adds thousands separators.
"""
thousands_sep = "," # Placeholder for thousands separator
decimal_sep = "." # Placeholder for decimal separator
for element in _assert_element_list(self._xpath("//qpy:format-float")):
if element.text is None:
continue
try:
float_val = float(element.text)
precision_txt = element.get("precision", "-1")
precision = int(precision_txt)
strip_zeroes = "strip-zeros" in element.attrib
formatted_str = f"{float_val:.{precision}f}" if precision >= 0 else str(float_val)
if strip_zeroes:
formatted_str = (
formatted_str.rstrip("0").rstrip(decimal_sep) if "." in formatted_str else formatted_str
)
thousands_sep_attr = element.get("thousands-separator", "no")
if thousands_sep_attr == "yes":
parts = formatted_str.split(decimal_sep)
integral_part = parts[0]
integral_part_with_sep = f"{int(integral_part):,}".replace(",", thousands_sep)
if len(parts) > 1:
formatted_str = integral_part_with_sep + decimal_sep + parts[1]
else:
formatted_str = integral_part_with_sep
except ValueError:
# There was an error while converting a text to a numeric value.
formatted_str = etree.tostring(element, encoding="unicode")
new_text = etree.Element("span")
new_text.text = formatted_str
parent = element.getparent()
new_text.tail = element.tail
if parent is not None:
parent.insert(parent.index(element), new_text)
parent.remove(element)
class QuestionFormulationUIRenderer(QuestionUIRenderer):
"""Renderer for the formulation UI part that provides metadata."""
def __init__(
self,
xml: str,
placeholders: dict[str, str],
options: QuestionDisplayOptions,
seed: int | None = None,
attempt: dict | None = None,
) -> None:
super().__init__(xml, placeholders, options, seed, attempt)
self.metadata = self._get_metadata()
def _get_metadata(self) -> QuestionMetadata:
"""Extracts metadata from the question UI."""
question_metadata = QuestionMetadata()
namespaces: dict[str, str] = {"xhtml": _XHTML_NAMESPACE, "qpy": _QPY_NAMESPACE}
# Extract correct responses
for element in self._xml.findall(".//*[@qpy:correct-response]", namespaces=namespaces):
name = element.get("name")
if not name:
continue
if element.tag.endswith("input") and element.get("type") == "radio":
value = element.get("value")
else:
value = element.get(f"{{{_QPY_NAMESPACE}}}correct-response")
if not value:
continue
question_metadata.correct_response[name] = value
# Extract other metadata
for element_type in ["input", "select", "textarea", "button"]:
for element in self._xml.findall(f".//xhtml:{element_type}", namespaces=namespaces):
name = element.get("name")
if not name:
continue
question_metadata.expected_data[name] = "Any"
if element.get("required") is not None:
question_metadata.required_fields.append(name)
return question_metadata
class _RenderErrorCollector:
def __init__(
self,
xml: str,
placeholders: dict[str, str],
) -> None:
self.errors = RenderErrorCollection()
try:
root = etree.fromstring(xml)
except etree.XMLSyntaxError as error:
parser = etree.XMLParser(recover=True)
root = etree.fromstring(xml, parser=parser)
self.errors.insert(XMLSyntaxError(error=error))
self._xml = etree.ElementTree(root)
self._xpath = etree.XPathDocumentEvaluator(self._xml)
self._xpath.register_namespace("xhtml", _XHTML_NAMESPACE)
self._xpath.register_namespace("qpy", _QPY_NAMESPACE)
self._placeholders = placeholders
def collect(self) -> RenderErrorCollection:
"""Applies transformations to the xml and collects the render errors."""
self._validate_placeholders()
self._validate_feedback()
self._validate_if_role()
self._validate_shuffle_contents_and_shuffled_index()
self._validate_format_floats()
self._look_for_unknown_qpy_elements_and_attributes()
return self.errors
def _validate_placeholders(self) -> None:
"""Collects potential render errors for the placeholder PIs."""
for p_instruction in _assert_element_list(self._xpath("//processing-instruction('p')")):
if not p_instruction.text:
reference_error = PlaceholderReferenceError(
element=p_instruction, placeholder=None, available=self._placeholders
)
self.errors.insert(reference_error)
return
parts = p_instruction.text.strip().split(maxsplit=1)
key = parts[0]
clean_option = parts[1].lower() if len(parts) == 2 else "clean" # noqa: PLR2004
expected = ("plain", "clean", "noclean")
if clean_option not in expected:
option_error = InvalidCleanOptionError(element=p_instruction, option=clean_option, expected=expected)
self.errors.insert(option_error)
if key not in self._placeholders:
reference_error = PlaceholderReferenceError(
element=p_instruction, placeholder=key, available=self._placeholders
)
self.errors.insert(reference_error)
def _validate_feedback(self) -> None:
"""Validate elements marked with `qpy:feedback`."""
for element in _assert_element_list(self._xpath("//*[@qpy:feedback]")):
feedback_type = element.get(f"{{{_QPY_NAMESPACE}}}feedback")
expected = ("general", "specific")
if feedback_type not in expected:
error = InvalidAttributeValueError(
element=element, attribute="qpy:feedback", value=feedback_type or "", expected=expected
)
self.errors.insert(error)
def _validate_if_role(self) -> None:
"""Validates elements with `qpy:if-role` attributes."""
for element in _assert_element_list(self._xpath("//*[@qpy:if-role]")):
if attr := element.get(f"{{{_QPY_NAMESPACE}}}if-role"):
allowed_roles = [role.upper() for role in re.split(r"[\s|]+", attr)]
expected = list(DisplayRole)
if unexpected := [role for role in allowed_roles if role not in expected]:
error = InvalidAttributeValueError(
element=element,
attribute="qpy:if-role",
value=unexpected,
expected=expected,
)
self.errors.insert(error)
def _validate_shuffle_contents_and_shuffled_index(self) -> None:
"""Validates elements marked with `qpy:shuffle-contents`."""
for element in _assert_element_list(self._xpath("//*[@qpy:shuffle-contents]")):
child_elements = [child for child in element if isinstance(child, etree._Element)]
for child in child_elements:
for index_element in _assert_element_list(
child.xpath(".//qpy:shuffled-index", namespaces={"qpy": _QPY_NAMESPACE})
):
format_style = index_element.get("format", "123")
if format_style not in {"123", "abc", "ABC", "iii", "III"}:
attribute_error = InvalidAttributeValueError(
element=index_element, attribute="format", value=format_style
)
self.errors.insert(attribute_error)
# Gather every qpy:shuffle-contents with direct text nodes or processing instructions.
for element in _assert_element_list(
self._xpath("//*[@qpy:shuffle-contents and (text()[normalize-space()] != '' or processing-instruction())]")
):
placement_error = InvalidContentError(element=element, attribute="qpy:shuffle-contents")
self.errors.insert(placement_error)
# Gather every qpy:shuffled-index without qpy:shuffle-contents ancestor.
for element in _assert_element_list(
self._xpath("//qpy:shuffled-index[not(ancestor::*[@qpy:shuffle-contents])]")
):
ancestor_error = ExpectedAncestorError(element=element, expected_ancestor_attribute="qpy:shuffle-contents")
self.errors.insert(ancestor_error)
def _validate_format_floats(self) -> None:
"""Validates the `qpy:format-float` element."""
for element in _assert_element_list(self._xpath("//qpy:format-float")):
if element.text is None:
# TODO: Show an error message?
return
# As PHP parses floats and integers differently than Python, we enforce a stricter format.
# E.g. parsing '20_000' or '1d1' results in:
# Python -> 20000 Error
# PHP -> 20 1
if re.match(r"^\s*((\d+\.?\d*)|(\d*\.\d+)|(\d+e\d+))\s*$", element.text) is None:
float_error = ConversionError(element=element, value=element.text, to_type=float)
self.errors.insert(float_error)
precision_text: str | None = element.get("precision")
if precision_text is not None:
if not precision_text or (precision_text[0] == "-" and precision_text[1:].isnumeric()):
# Empty or negative value.
precision_error = InvalidAttributeValueError(
element=element, attribute="precision", value=precision_text
)
self.errors.insert(precision_error)
elif not precision_text.isnumeric():
# We disallow the usage of underscores to separate numeric literals, see above for an explanation.
conversion_error = ConversionError(
element=element, value=precision_text, to_type=int, attribute="precision"
)
self.errors.insert(conversion_error)
thousands_sep_attr = element.get("thousands-separator", "no")
expected = ("yes", "no")
if thousands_sep_attr not in expected:
thousands_sep_error = InvalidAttributeValueError(
element=element, attribute="thousands-separator", value=thousands_sep_attr, expected=expected
)
self.errors.insert(thousands_sep_error)
def _look_for_unknown_qpy_elements_and_attributes(self) -> None:
"""Checks if there are any unknown qpy-elements or -attributes."""
# Gather unknown elements.
known_elements = ["shuffled-index", "format-float"]
xpath_elements = " and ".join(f"local-name() != '{element}'" for element in known_elements)
xpath_query = f"//qpy:*[{xpath_elements}]"
for element in _assert_element_list(self._xpath(xpath_query)):
unknown_element_error = UnknownElementError(element=element)
self.errors.insert(unknown_element_error)
# Gather unknown attributes.
known_attrs = ["feedback", "if-role", "shuffle-contents", "correct-response"]
xpath_attrs = " and ".join(f"local-name() != '{attr}'" for attr in known_attrs)
xpath_query = f"//*[@qpy:*[{xpath_attrs}]]"
for element in _assert_element_list(self._xpath(xpath_query)):
unknown_attributes: list[str] = [
attr.replace(f"{{{_QPY_NAMESPACE}}}", "qpy:")
for attr in map(str, element.attrib)
if attr.startswith(f"{{{_QPY_NAMESPACE}}}") and attr.split("}")[1] not in known_attrs
]
if unknown_attributes:
unknown_attribute_error = UnknownAttributeError(element=element, attributes=unknown_attributes)
self.errors.insert(unknown_attribute_error)