1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-12 08:58:17 +03:00

B #6064: Replace dicttoxml by dict2xml (#2482)

This commit is contained in:
Jan Orel 2023-02-07 08:26:45 +01:00 committed by GitHub
parent 41ed5fa477
commit 81512242e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View File

@ -14,14 +14,34 @@
# limitations under the License.
import dicttoxml
import dict2xml
import xmltodict
from lxml.etree import tostring
from collections import OrderedDict
from aenum import IntEnum
# this is a bit hackish way to avoid escaping <,> characters
# by dict2xml, as it would break <!CDATA[]> which is injected before
dict2xml.Node.entities = []
TEMPLATE_ALWAYS_LIST_ELEM = ['SNAPSHOT']
def add_cdata(d):
'''
recursively traverse nested dict and add xml
CDATA element to the leaves
'''
if isinstance(d, list):
return [ add_cdata(x) for x in d ]
elif isinstance(d, dict):
return { k: add_cdata(v) for (k,v) in d.items()}
else:
return f"<![CDATA[{str(d)}]]>"
def cast2one(param):
'''
@ -52,8 +72,8 @@ def cast2one(param):
root = list(param.values())[0]
if is_nested_dict(param):
# We return this dictionary as XML
return dicttoxml.dicttoxml(param, root=False, attr_type=False,
cdata=True).decode('utf8')
return dict2xml.dict2xml(add_cdata(param),
indent="", newlines=False)
else:
# We return this dictionary as attribute=value vector
ret = u""

View File

@ -24,7 +24,7 @@ here = path.abspath(path.dirname(__file__))
install_requires = [
'lxml',
'dicttoxml',
'dict2xml',
'xmltodict',
'six',
'aenum',