/usr/lib/python2.7/dist-packages/jnpr/junos/jxml.py is in python-junos-eznc 2.0.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | from ncclient import manager
from ncclient.xml_ import NCElement
from lxml import etree
"""
These are Junos XML 'helper' definitions use for generic XML processing
.DEL to delete an item
.REN to rename an item, requires the use of NAME()
.INSERT(<'before'|'after'>) to reorder an item, requires the use of NAME()
.BEFORE to reorder an item before another, requires the use of NAME()
.AFTER to reorder an item after another, requires the use of NAME()
.NAME(name) to assign the name attribute
"""
DEL = {'delete': 'delete'} # Junos XML resource delete
REN = {'rename': 'rename'} # Junos XML resource rename
ACTIVATE = {'active': 'active'} # activate resource
DEACTIVATE = {'inactive': 'inactive'} # deactivate resource
REPLACE = {'replace': 'replace'} # replace elements
def NAME(name):
return {'name': name}
def INSERT(cmd):
return {'insert': cmd}
BEFORE = {'insert': 'before'}
AFTER = {'insert': 'after'}
# used with <get-configuration> to load only the object identifiers and
# not all the subsequent configuration
NAMES_ONLY = {'recurse': "false"}
# for <get-configuration>, attributes to retrieve from apply-groups
INHERIT = {'inherit': 'inherit'}
INHERIT_GROUPS = {'inherit': 'inherit', 'groups': 'groups'}
INHERIT_DEFAULTS = {'inherit': 'defaults', 'groups': 'groups'}
# XSLT for on-box commit script
conf_xslt = '''\
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="subSelectionXPath" />
<xsl:template match="/">
<xsl:apply-templates
select="$subSelectionXPath/ancestor::*[position()=last()]"/>
</xsl:template>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="$subSelectionXPath/ancestor::*
[generate-id() = generate-id(current())]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:choose>
<xsl:when test="generate-id(.)=
generate-id($subSelectionXPath/ancestor::*[1])">
<xsl:copy-of select="$subSelectionXPath"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>'''
conf_xslt_root = etree.XML(conf_xslt)
conf_transform = etree.XSLT(conf_xslt_root)
normalize_xslt = '''\
<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()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
</xsl:stylesheet>'''
# XSLT to strip comments
strip_comments_xslt = '''\
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="comment()"/>
</xsl:stylesheet>'''
strip_xslt_root = etree.XML(strip_comments_xslt)
strip_comments_transform = etree.XSLT(strip_xslt_root)
def remove_namespaces(xml):
for elem in xml.getiterator():
if elem.tag is etree.Comment:
continue
i = elem.tag.find('}')
if i > 0:
elem.tag = elem.tag[i + 1:]
return xml
def rpc_error(rpc_xml):
"""
extract the various bits from an <rpc-error> element
into a dictionary
"""
remove_namespaces(rpc_xml)
if 'rpc-reply' == rpc_xml.tag:
rpc_xml = rpc_xml[0]
def find_strip(x):
ele = rpc_xml.find(x)
return ele.text.strip() if None != ele and None != ele.text else None
this_err = {}
this_err['severity'] = find_strip('error-severity')
this_err['source'] = find_strip('source-daemon')
this_err['edit_path'] = find_strip('error-path')
this_err['bad_element'] = find_strip('error-info/bad-element')
this_err['message'] = find_strip('error-message')
return this_err
def cscript_conf(reply):
try:
device_params = {'name': 'junos'}
device_handler = manager.make_device_handler(device_params)
transform_reply = device_handler.transform_reply()
return NCElement(reply, transform_reply)._NCElement__doc
except:
return None
|