Skip to content

Commit

Permalink
Merge pull request #8 from STIXProject/update_ns
Browse files Browse the repository at this point in the history
Update_ns.py
  • Loading branch information
gtback committed Jul 14, 2016
2 parents ebdce25 + 5672df1 commit f370d53
Show file tree
Hide file tree
Showing 7 changed files with 454 additions and 22 deletions.
25 changes: 8 additions & 17 deletions ramrod/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,6 @@ def _update_namespaces(self, node):
with the updated schema. This will also remove any disallowed
namespaces if found in the instance document.
Note:
Only nodes that exist within the namespaces defined by
the ``NS_MAP`` class attribute will be updated.
Note:
The lxml library does not allow you to modify the ``nsmap``
attribute of an ``_Element`` directly. To modify the ``nsmap``,
Expand All @@ -777,24 +773,19 @@ def _update_namespaces(self, node):
A copy of the `node` with an updated ``nsmap`` attribute. Each
of its descendants which belong to known namespaces are updated
as well.
If `node` is not an ``etree._Element`` (e.g, a comment node),
or it does not belong to any namespace defined in the class-level
``NSMAP``, then this function returns `node` itself.
"""
for child in utils.children(node):
self._update_namespaces(child)

ns = utils.get_namespace(node)
namespaces = self.NSMAP.itervalues()

if ns not in namespaces:
return node
if ns in self.UPDATE_NS_MAP:
new_node = self._update_nsmap(node)
utils.replace_xml_element(node, new_node)
node = new_node

for child in utils.children(node):
self._update_namespaces(child)
return node

new_node = self._update_nsmap(node)
utils.replace_xml_element(node, new_node)
return new_node

def _create_update_results(self, root, remapped=None, removed=None):
"""Creates and returns a :class:`UpdateResults` object instance
Expand Down
2 changes: 1 addition & 1 deletion ramrod/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(self, document):
allowed_types = (etree._Element, etree._ElementTree) # noqa

if not isinstance(document, allowed_types):
raise ValueError("Document must be one of %s" % (allowed_types,))
raise ValueError("Document must be one of %s, got %s" % (allowed_types, type(document)))

try:
self._document = document.getroottree()
Expand Down

0 comments on commit f370d53

Please sign in to comment.