Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 2 | Created on 03-Dec-2012 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [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 | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [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 | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [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 | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 24 | ''' |
| 25 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 26 | """ |
| 27 | xmldict |
| 28 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 29 | |
| 30 | Convert xml to python dictionaries. |
| 31 | """ |
| 32 | import datetime |
| 33 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 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 | 696f426 | 2017-10-17 10:56:26 -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 | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 42 | root = ElementTree.XML( root_or_str ) |
| 43 | try: |
| 44 | return { root.tag: _from_xml( root, strict ) } |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 45 | except StandardError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 46 | return None |
| 47 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 48 | def dict_to_xml( dict_xml ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 49 | """ |
| 50 | Converts `dict_xml` which is a python dict to corresponding xml. |
| 51 | """ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 52 | return _to_xml( dict_xml ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 53 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 54 | def _to_xml( el ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 55 | """ |
| 56 | Converts `el` to its xml representation. |
| 57 | """ |
| 58 | val = None |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 59 | if isinstance( el, dict ): |
| 60 | val = _dict_to_xml( el ) |
| 61 | elif isinstance( el, bool ): |
| 62 | val = str( el ).lower() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 63 | else: |
| 64 | val = el |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 65 | if val is None: |
| 66 | val = 'null' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 67 | return val |
| 68 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 69 | def _extract_attrs( els ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 70 | """ |
| 71 | Extracts attributes from dictionary `els`. Attributes are keys which start |
| 72 | with '@' |
| 73 | """ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 74 | if not isinstance( els, dict ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 75 | return '' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 76 | return ''.join( ' %s="%s"' % ( key[ 1: ], value ) for key, value in els.iteritems() if key.startswith( '@' ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 77 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 78 | def _dict_to_xml( els ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 79 | """ |
| 80 | Converts `els` which is a python dict to corresponding xml. |
| 81 | """ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 82 | def process_content( tag, content ): |
| 83 | attrs = _extract_attrs( content ) |
| 84 | text = isinstance( content, dict ) and content.get( '#text', '' ) or '' |
| 85 | return '<%s%s>%s%s</%s>' % ( tag, attrs, _to_xml( content ), text, tag ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 86 | |
| 87 | tags = [] |
| 88 | for tag, content in els.iteritems(): |
| 89 | # Text and attributes |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 90 | if tag.startswith( '@' ) or tag == '#text': |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 91 | continue |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 92 | elif isinstance( content, list ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 93 | for el in content: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 94 | tags.append( process_content( tag, el ) ) |
| 95 | elif isinstance( content, dict ): |
| 96 | tags.append( process_content( tag, content ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 97 | else: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 98 | tags.append( '<%s>%s</%s>' % ( tag, _to_xml( content ), tag ) ) |
| 99 | return ''.join( tags ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 100 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 101 | def _is_xml_el_dict( el ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 102 | """ |
| 103 | Returns true if `el` is supposed to be a dict. |
| 104 | This function makes sense only in the context of making dicts out of xml. |
| 105 | """ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 106 | if len( el ) == 1 or el[ 0 ].tag != el[ 1 ].tag: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 107 | return True |
| 108 | return False |
| 109 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 110 | def _is_xml_el_list( el ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 111 | """ |
| 112 | Returns true if `el` is supposed to be a list. |
| 113 | This function makes sense only in the context of making lists out of xml. |
| 114 | """ |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 115 | if len( el ) > 1 and el[ 0 ].tag == el[ 1 ].tag: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 116 | return True |
| 117 | return False |
| 118 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 119 | def _str_to_datetime( date_str ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 120 | try: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 121 | val = datetime.datetime.strptime( date_str, "%Y-%m-%dT%H:%M:%SZ" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 122 | except ValueError: |
| 123 | val = date_str |
| 124 | return val |
| 125 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 126 | def _str_to_boolean( bool_str ): |
| 127 | if bool_str.lower() != 'false' and bool( bool_str ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 128 | return True |
| 129 | return False |
| 130 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 131 | def _from_xml( el, strict ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 132 | """ |
| 133 | Extracts value of xml element element `el`. |
| 134 | """ |
| 135 | val = None |
| 136 | # Parent node. |
| 137 | if el: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 138 | if _is_xml_el_dict( el ): |
| 139 | val = _dict_from_xml( el, strict ) |
| 140 | elif _is_xml_el_list( el ): |
| 141 | val = _list_from_xml( el, strict ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 142 | # Simple node. |
| 143 | else: |
| 144 | attribs = el.items() |
| 145 | # An element with attributes. |
| 146 | if attribs and strict: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 147 | val = dict( ( '@%s' % k, v ) for k, v in dict( attribs ).iteritems() ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 148 | if el.text: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 149 | converted = _val_and_maybe_convert( el ) |
| 150 | val[ '#text' ] = el.text |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 151 | if converted != el.text: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 152 | val[ '#value' ] = converted |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 153 | elif el.text: |
| 154 | # An element with no subelements but text. |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 155 | val = _val_and_maybe_convert( el ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 156 | elif attribs: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 157 | val = dict( attribs ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 158 | return val |
| 159 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 160 | def _val_and_maybe_convert( el ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 161 | """ |
| 162 | Converts `el.text` if `el` has attribute `type` with valid value. |
| 163 | """ |
| 164 | text = el.text.strip() |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 165 | data_type = el.get( 'type' ) |
| 166 | convertor = _val_and_maybe_convert.convertors.get( data_type ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 167 | if convertor: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 168 | return convertor( text ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 169 | else: |
| 170 | return text |
| 171 | _val_and_maybe_convert.convertors = { |
| 172 | 'boolean': _str_to_boolean, |
| 173 | 'datetime': _str_to_datetime, |
| 174 | 'integer': int |
| 175 | } |
| 176 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 177 | def _list_from_xml( els, strict ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 178 | """ |
| 179 | Converts xml elements list `el_list` to a python list. |
| 180 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 181 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 182 | temp = {} |
| 183 | for el in els: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 184 | tag = el.attrib[ "name" ] |
| 185 | temp[ tag ] = ( _from_xml( el, strict ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 186 | return temp |
| 187 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 188 | def _dict_from_xml( els, strict ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | """ |
| 190 | Converts xml doc with root `root` to a python dict. |
| 191 | """ |
| 192 | # An element with subelements. |
| 193 | res = {} |
| 194 | for el in els: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame^] | 195 | res[ el.tag ] = _from_xml( el, strict ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 196 | return res |