Skip to content

Commit

Permalink
fix: parse_xml should be in Blocklike
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmccormick committed Feb 12, 2024
1 parent e13b78f commit 7b5e8d6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
48 changes: 48 additions & 0 deletions xblock/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,54 @@ def fields(cls): # pylint: disable=no-self-argument

return fields

@classmethod
def parse_xml(cls, node, runtime, keys, id_generator):
"""
Use `node` to construct a new block.
Arguments:
node (:class:`~xml.etree.ElementTree.Element`): The xml node to parse into an xblock.
runtime (:class:`.Runtime`): The runtime to use while parsing.
keys (:class:`.ScopeIds`): The keys identifying where this block
will store its data.
id_generator (:class:`.IdGenerator`): An object that will allow the
runtime to generate correct definition and usage ids for
children of this block.
"""
block = runtime.construct_xblock_from_class(cls, keys)

# The base implementation: child nodes become child blocks.
# Or fields, if they belong to the right namespace.
for child in node:
if child.tag is etree.Comment:
continue
qname = etree.QName(child)
tag = qname.localname
namespace = qname.namespace

Check warning on line 323 in xblock/core.py

View check run for this annotation

Codecov / codecov/patch

xblock/core.py#L319-L323

Added lines #L319 - L323 were not covered by tests

if namespace == XML_NAMESPACES["option"]:
cls._set_field_if_present(block, tag, child.text, child.attrib)

Check warning on line 326 in xblock/core.py

View check run for this annotation

Codecov / codecov/patch

xblock/core.py#L325-L326

Added lines #L325 - L326 were not covered by tests
else:
block.runtime.add_node_as_child(block, child, id_generator)

Check warning on line 328 in xblock/core.py

View check run for this annotation

Codecov / codecov/patch

xblock/core.py#L328

Added line #L328 was not covered by tests

# Attributes become fields.
for name, value in list(node.items()): # lxml has no iteritems
cls._set_field_if_present(block, name, value, {})

# Text content becomes "content", if such a field exists.
if "content" in block.fields and block.fields["content"].scope == Scope.content:
text = node.text
if text:
text = text.strip()
if text:
block.content = text

return block

@classmethod
def _set_field_if_present(cls, block, name, value, attrs):
"""
Expand Down
6 changes: 3 additions & 3 deletions xblock/test/test_core_capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class HasChildren(XBlock):
class WithoutChildren(XBlock):
"""Toy class for hierarchy testing"""

class InheritedChildren(XBlock):
class InheritedChildren(HasChildren):
"""Toy class for hierarchy testing"""

def test_children_metaclass(self):
Expand Down Expand Up @@ -181,7 +181,7 @@ def an_unsupported_view(self):
"""
# pragma: no cover

test_xblock = SupportsDecoratorTester(runtime=None)
test_xblock = SupportsDecoratorTester(None, None, None)

for view_name, functionality, expected_result in (
("functionality_supported_view", "a_functionality", True),
Expand Down Expand Up @@ -213,7 +213,7 @@ def has_support(self, view, functionality):
"""
return functionality == "a_functionality"

test_xblock = HasSupportOverrideTester(runtime=None)
test_xblock = HasSupportOverrideTester(None, None, None)

for view_name, functionality, expected_result in (
("functionality_supported_view", "a_functionality", True),
Expand Down

0 comments on commit 7b5e8d6

Please sign in to comment.