| 108 | |
| 109 | |
| 110 | def check_node( |
| 111 | state, name, index=0, typestr="{{ordinal}} node", missing_msg=None, expand_msg=None |
| 112 | ): |
| 113 | |
| 114 | if missing_msg is None: |
| 115 | missing_msg = "The system wants to check the {{typestr}} but hasn't found it." |
| 116 | if expand_msg is None: |
| 117 | expand_msg = "Check the {{typestr}}. " |
| 118 | |
| 119 | stu_out = state.ast_dispatcher.find(name, state.student_ast) |
| 120 | sol_out = state.ast_dispatcher.find(name, state.solution_ast) |
| 121 | |
| 122 | # check if there are enough nodes for index |
| 123 | fmt_kwargs = { |
| 124 | "ordinal": get_ord(index + 1) if isinstance(index, int) else "", |
| 125 | "index": index, |
| 126 | "name": name, |
| 127 | } |
| 128 | fmt_kwargs["typestr"] = render(typestr, fmt_kwargs) |
| 129 | |
| 130 | # test if node can be indexed succesfully |
| 131 | try: |
| 132 | stu_out[index] |
| 133 | except (KeyError, IndexError): # TODO comment errors |
| 134 | state.report(missing_msg, fmt_kwargs) |
| 135 | |
| 136 | # get node at index |
| 137 | stu_part = stu_out[index] |
| 138 | sol_part = sol_out[index] |
| 139 | |
| 140 | append_message = FeedbackComponent(expand_msg, fmt_kwargs) |
| 141 | |
| 142 | return part_to_child(stu_part, sol_part, append_message, state, node_name=name) |
| 143 | |
| 144 | |
| 145 | # context functions ----------------------------------------------------------- |