Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2017 the original author or authors. |
| 4 | # |
| 5 | # Code adapted from https://github.com/choppsv1/netconf |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # |
| 19 | from netconf.constants import Constants as C |
| 20 | from lxml import etree |
| 21 | |
| 22 | ns_map = C.NS_MAP |
| 23 | |
| 24 | def qmap(key): |
| 25 | if ns_map.has_key(key): |
| 26 | return ''.join(['{', ns_map[key], '}']) |
| 27 | |
| 28 | def ns(key): |
| 29 | if ns_map.has_key(key): |
| 30 | return ns_map[key] |
| 31 | |
| 32 | def qname(tag): |
| 33 | try: |
| 34 | return etree.QName(tag) |
| 35 | except ValueError: |
| 36 | prefix, base = tag.split(":") |
| 37 | return etree.QName(ns(prefix), base) |
| 38 | |
| 39 | def elm(tag, attrib=None, **extra): |
| 40 | if attrib is None: |
| 41 | attrib = dict() |
| 42 | return etree.Element(qname(tag), attrib, **extra) |
| 43 | |