Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 1 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 2 | Created on 03-Dec-2012 |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 3 | Copyright 2012 Open Networking Foundation ( ONF ) |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 4 | |
| 5 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 6 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 7 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 8 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 9 | author:: Anil Kumar ( anilkumar.s@paxterrasolutions.com ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 10 | |
| 11 | TestON is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published by |
| 13 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 14 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 15 | |
| 16 | TestON is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 22 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 23 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 24 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | """ |
| 26 | xmldict |
| 27 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 28 | |
| 29 | Convert xml to python dictionaries. |
| 30 | """ |
| 31 | import datetime |
| 32 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 33 | |
| 34 | def xml_to_dict( root_or_str, strict=True ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 35 | """ |
| 36 | Converts `root_or_str` which can be parsed xml or a xml string to dict. |
| 37 | |
| 38 | """ |
| 39 | root = root_or_str |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 40 | if isinstance( root, str ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | import xml.etree.cElementTree as ElementTree |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 42 | root = ElementTree.XML( root_or_str ) |
| 43 | try: |
| 44 | return { root.tag: _from_xml( root, strict ) } |
| 45 | except Exception: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 46 | return None |
| 47 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 48 | |
| 49 | def dict_to_xml( dict_xml ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 50 | """ |
| 51 | Converts `dict_xml` which is a python dict to corresponding xml. |
| 52 | """ |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 53 | return _to_xml( dict_xml ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 55 | |
| 56 | def _to_xml( el ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | """ |
| 58 | Converts `el` to its xml representation. |
| 59 | """ |
| 60 | val = None |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 61 | if isinstance( el, dict ): |
| 62 | val = _dict_to_xml( el ) |
| 63 | elif isinstance( el, bool ): |
| 64 | val = str( el ).lower() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 65 | else: |
| 66 | val = el |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 67 | if val is None: |
| 68 | val = 'null' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 69 | return val |
| 70 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 71 | |
| 72 | def _extract_attrs( els ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 73 | """ |
| 74 | Extracts attributes from dictionary `els`. Attributes are keys which start |
| 75 | with '@' |
| 76 | """ |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 77 | if not isinstance( els, dict ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 78 | return '' |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 79 | return ''.join( ' %s="%s"' % ( key[ 1: ], value ) for key, value in els.iteritems() |
| 80 | if key.startswith( '@' ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 81 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 82 | |
| 83 | def _dict_to_xml( els ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 84 | """ |
| 85 | Converts `els` which is a python dict to corresponding xml. |
| 86 | """ |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 87 | def process_content( tag, content ): |
| 88 | attrs = _extract_attrs( content ) |
| 89 | text = isinstance( content, dict ) and content.get( '#text', '' ) or '' |
| 90 | return '<%s%s>%s%s</%s>' % ( tag, attrs, _to_xml( content ), text, tag ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 91 | |
| 92 | tags = [] |
| 93 | for tag, content in els.iteritems(): |
| 94 | # Text and attributes |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 95 | if tag.startswith( '@' ) or tag == '#text': |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 96 | continue |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 97 | elif isinstance( content, list ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 98 | for el in content: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 99 | tags.append( process_content( tag, el ) ) |
| 100 | elif isinstance( content, dict ): |
| 101 | tags.append( process_content( tag, content ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 102 | else: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 103 | tags.append( '<%s>%s</%s>' % ( tag, _to_xml( content ), tag ) ) |
| 104 | return ''.join( tags ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 105 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 106 | |
| 107 | def _is_xml_el_dict( el ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 108 | """ |
| 109 | Returns true if `el` is supposed to be a dict. |
| 110 | This function makes sense only in the context of making dicts out of xml. |
| 111 | """ |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 112 | if len( el ) == 1 or el[ 0 ].tag != el[ 1 ].tag: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 113 | return True |
| 114 | return False |
| 115 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 116 | |
| 117 | def _is_xml_el_list( el ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 118 | """ |
| 119 | Returns true if `el` is supposed to be a list. |
| 120 | This function makes sense only in the context of making lists out of xml. |
| 121 | """ |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 122 | if len( el ) > 1 and el[ 0 ].tag == el[ 1 ].tag: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 123 | return True |
| 124 | return False |
| 125 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 126 | |
| 127 | def _str_to_datetime( date_str ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 128 | try: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 129 | val = datetime.datetime.strptime( date_str, "%Y-%m-%dT%H:%M:%SZ" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 130 | except ValueError: |
| 131 | val = date_str |
| 132 | return val |
| 133 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 134 | |
| 135 | def _str_to_boolean( bool_str ): |
| 136 | if bool_str.lower() != 'false' and bool( bool_str ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 137 | return True |
| 138 | return False |
| 139 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 140 | |
| 141 | def _from_xml( el, strict ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 142 | """ |
| 143 | Extracts value of xml element element `el`. |
| 144 | """ |
| 145 | val = None |
| 146 | # Parent node. |
| 147 | if el: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 148 | if _is_xml_el_dict( el ): |
| 149 | val = _dict_from_xml( el, strict ) |
| 150 | elif _is_xml_el_list( el ): |
| 151 | val = _list_from_xml( el, strict ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 152 | # Simple node. |
| 153 | else: |
| 154 | attribs = el.items() |
| 155 | # An element with attributes. |
| 156 | if attribs and strict: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 157 | val = dict( ( '@%s' % k, v ) for k, v in dict( attribs ).iteritems() ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 158 | if el.text: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 159 | converted = _val_and_maybe_convert( el ) |
| 160 | val[ '#text' ] = el.text |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 161 | if converted != el.text: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 162 | val[ '#value' ] = converted |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 163 | elif el.text: |
| 164 | # An element with no subelements but text. |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 165 | val = _val_and_maybe_convert( el ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 166 | elif attribs: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 167 | val = dict( attribs ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 168 | return val |
| 169 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 170 | |
| 171 | def _val_and_maybe_convert( el ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 172 | """ |
| 173 | Converts `el.text` if `el` has attribute `type` with valid value. |
| 174 | """ |
| 175 | text = el.text.strip() |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 176 | data_type = el.get( 'type' ) |
| 177 | convertor = _val_and_maybe_convert.convertors.get( data_type ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 178 | if convertor: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 179 | return convertor( text ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 180 | else: |
| 181 | return text |
| 182 | _val_and_maybe_convert.convertors = { |
| 183 | 'boolean': _str_to_boolean, |
| 184 | 'datetime': _str_to_datetime, |
| 185 | 'integer': int |
| 186 | } |
| 187 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 188 | |
| 189 | def _list_from_xml( els, strict ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 190 | """ |
| 191 | Converts xml elements list `el_list` to a python list. |
| 192 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 193 | temp = {} |
| 194 | for el in els: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 195 | tag = el.attrib[ "name" ] |
| 196 | temp[ tag ] = ( _from_xml( el, strict ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 197 | return temp |
| 198 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 199 | |
| 200 | def _dict_from_xml( els, strict ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 201 | """ |
| 202 | Converts xml doc with root `root` to a python dict. |
| 203 | """ |
| 204 | # An element with subelements. |
| 205 | res = {} |
| 206 | for el in els: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 207 | res[ el.tag ] = _from_xml( el, strict ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 208 | return res |