Netconf bytes fixes (#41607)

* Solve some bytes issues on iosxr

* Solve some bytes issues in junos

* Do the correct thing with tostring based on lxml or not
This commit is contained in:
Nathaniel Case
2018-06-22 11:50:44 -04:00
committed by GitHub
parent c144adc9de
commit 9aa8c652ba
23 changed files with 54 additions and 113 deletions

View File

@@ -109,7 +109,7 @@ class NetconfConnection(Connection):
def transform_reply():
reply = '''<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
return b'''<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<xsl:template match="/|comment()|processing-instruction()">
@@ -131,10 +131,6 @@ def transform_reply():
</xsl:template>
</xsl:stylesheet>
'''
if sys.version < '3':
return reply
else:
return reply.encode('UTF-8')
# Note: Workaround for ncclient 0.5.3

View File

@@ -260,31 +260,25 @@ def build_xml(container, xmap=None, params=None, opcode=None):
for item in subtree_list:
container_ele.append(item)
return etree.tostring(root)
return etree.tostring(root, encoding='unicode')
def etree_find(root, node):
try:
element = etree.fromstring(root).find('.//' + to_bytes(node, errors='surrogate_then_replace').strip())
except Exception:
element = etree.fromstring(etree.tostring(root)).find('.//' + to_bytes(node, errors='surrogate_then_replace').strip())
root = etree.fromstring(to_bytes(root))
except (ValueError, etree.XMLSyntaxError):
pass
if element is not None:
return element
return None
return root.find('.//%s' % node.strip())
def etree_findall(root, node):
try:
element = etree.fromstring(root).findall('.//' + to_bytes(node, errors='surrogate_then_replace').strip())
except Exception:
element = etree.fromstring(etree.tostring(root)).findall('.//' + to_bytes(node, errors='surrogate_then_replace').strip())
root = etree.fromstring(to_bytes(root))
except (ValueError, etree.XMLSyntaxError):
pass
if element is not None:
return element
return None
return root.findall('.//%s' % node.strip())
def is_cliconf(module):

View File

@@ -27,10 +27,10 @@ from ansible.module_utils.network.common.netconf import NetconfConnection
from ansible.module_utils._text import to_text
try:
from lxml.etree import Element, SubElement, fromstring, tostring
from lxml.etree import Element, SubElement, tostring as xml_to_string
HAS_LXML = True
except ImportError:
from xml.etree.ElementTree import Element, SubElement, fromstring, tostring
from xml.etree.ElementTree import Element, SubElement, tostring as xml_to_string
HAS_LXML = False
ACTIONS = frozenset(['merge', 'override', 'replace', 'update', 'set'])
@@ -62,6 +62,13 @@ junos_top_spec = {
junos_argument_spec.update(junos_top_spec)
def tostring(element, encoding='UTF-8'):
if HAS_LXML:
return xml_to_string(element, encoding='unicode')
else:
return to_text(xml_to_string(element, encoding), encoding=encoding)
def get_provider_argspec():
return junos_provider_spec