Commit 557d5b47 authored by Łukasz Nowak's avatar Łukasz Nowak

Synchronise xml2dict and dict2xml

All tools from the ecosystem shall use the same way to convert between python's
dict and XML.
parent 3244ebb0
...@@ -50,7 +50,7 @@ try: ...@@ -50,7 +50,7 @@ try:
ComputerPartition as SlapComputerPartition, ComputerPartition as SlapComputerPartition,
SoftwareInstance, SoftwareInstance,
SoftwareRelease) SoftwareRelease)
from slapos.proxy.views import dict2xml from slapos.slap.util import dict2xml, xml2dict
except ImportError: except ImportError:
# Do no prevent instance from starting # Do no prevent instance from starting
# if libs are not installed # if libs are not installed
...@@ -68,6 +68,8 @@ except ImportError: ...@@ -68,6 +68,8 @@ except ImportError:
raise ImportError raise ImportError
def dict2xml(dictionary): def dict2xml(dictionary):
raise ImportError raise ImportError
def xml2dict(dictionary):
raise ImportError
from zLOG import LOG, INFO from zLOG import LOG, INFO
import xml_marshaller import xml_marshaller
...@@ -896,16 +898,7 @@ class SlapTool(BaseTool): ...@@ -896,16 +898,7 @@ class SlapTool(BaseTool):
def _instanceXmlToDict(self, xml): def _instanceXmlToDict(self, xml):
result_dict = {} result_dict = {}
try: try:
if xml is not None and xml != '': result_dict = xml2dict(xml)
tree = etree.fromstring(xml)
for element in tree.findall('parameter'):
key = element.get('id')
value = result_dict.get(key, None)
if value is not None:
value = value + ' ' + element.text
else:
value = element.text
result_dict[key] = value
except (etree.XMLSchemaError, etree.XMLSchemaParseError, except (etree.XMLSchemaError, etree.XMLSchemaParseError,
etree.XMLSchemaValidateError, etree.XMLSyntaxError): etree.XMLSchemaValidateError, etree.XMLSyntaxError):
LOG('SlapTool', INFO, 'Issue during parsing xml:', error=True) LOG('SlapTool', INFO, 'Issue during parsing xml:', error=True)
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
# #
############################################################################## ##############################################################################
from lxml import etree
import random import random
import string import string
import time import time
...@@ -37,7 +36,7 @@ from slapos.slap.slap import Computer, ComputerPartition, \ ...@@ -37,7 +36,7 @@ from slapos.slap.slap import Computer, ComputerPartition, \
SoftwareRelease, SoftwareInstance, NotFoundError SoftwareRelease, SoftwareInstance, NotFoundError
from slapos.proxy.db_version import DB_VERSION from slapos.proxy.db_version import DB_VERSION
import slapos.slap import slapos.slap
from slapos.util import bytes2str, str2bytes, unicode2str, sqlite_connect from slapos.util import bytes2str, unicode2str, sqlite_connect, xml2dict, dict2xml
from flask import g, Flask, request, abort from flask import g, Flask, request, abort
from slapos.util import loads, dumps from slapos.util import loads, dumps
...@@ -53,39 +52,6 @@ class UnauthorizedError(Exception): ...@@ -53,39 +52,6 @@ class UnauthorizedError(Exception):
pass pass
def xml2dict(xml):
result_dict = {}
if xml:
tree = etree.fromstring(str2bytes(xml))
for element in tree.iter(tag=etree.Element):
if element.tag == 'parameter':
key = element.get('id')
value = result_dict.get(key, None)
if value is not None:
value = value + ' ' + element.text
else:
value = element.text
result_dict[key] = value
return result_dict
def dict2xml(dictionary):
instance = etree.Element('instance')
for k, v in six.iteritems(dictionary):
if isinstance(k, bytes):
k = k.decode('utf-8')
if isinstance(v, bytes):
v = v.decode('utf-8')
elif not isinstance(v, six.text_type):
v = str(v)
etree.SubElement(instance, "parameter",
attrib={'id': k}).text = v
return bytes2str(etree.tostring(instance,
pretty_print=True,
xml_declaration=True,
encoding='utf-8'))
def partitiondict2partition(partition): def partitiondict2partition(partition):
slap_partition = ComputerPartition(partition['computer_reference'], slap_partition = ComputerPartition(partition['computer_reference'],
partition['reference']) partition['reference'])
......
from lxml import etree from lxml import etree
from six.moves.urllib import parse from six.moves.urllib import parse
from six import iteritems, text_type
from slapos.util import bytes2str
import netaddr import netaddr
#from slapproxy
#def xml2dict(xml):
# result_dict = {}
# if xml:
# tree = etree.fromstring(str2bytes(xml))
# for element in tree.iter(tag=etree.Element):
# if element.tag == 'parameter':
# key = element.get('id')
# value = result_dict.get(key, None)
# if value is not None:
# value = value + ' ' + element.text
# else:
# value = element.text
# result_dict[key] = value
# return result_dict
def dict2xml(dictionary):
instance = etree.Element('instance')
for k, v in iteritems(dictionary):
if isinstance(k, bytes):
k = k.decode('utf-8')
if isinstance(v, bytes):
v = v.decode('utf-8')
elif not isinstance(v, text_type):
v = str(v)
etree.SubElement(instance, "parameter",
attrib={'id': k}).text = v
return bytes2str(etree.tostring(instance,
pretty_print=True,
xml_declaration=True,
encoding='utf-8'))
#from slapos master
# def xml2dict
# if xml is not None and xml != '':
# tree = etree.fromstring(xml)
# for element in tree.findall('parameter'):
# key = element.get('id')
# value = result_dict.get(key, None)
# if value is not None:
# value = value + ' ' + element.text
# else:
# value = element.text
# result_dict[key] = value
def xml2dict(xml): def xml2dict(xml):
result_dict = {} result_dict = {}
if xml is not None and xml != '': if xml is not None and xml != '':
tree = etree.fromstring(xml.encode('utf-8')) tree = etree.fromstring(xml.encode('utf-8'))
for element in tree.iter(tag=etree.Element): for element in tree.findall('parameter'):
if element.tag == 'parameter': key = element.get('id')
key = element.get('id') value = result_dict.get(key, None)
value = result_dict.get(key, None) if value is not None:
if value is not None: value = value + ' ' + element.text
value = value + ' ' + element.text else:
else: value = element.text
value = element.text result_dict[key] = value
result_dict[key] = value
return result_dict return result_dict
def _addIpv6Brackets(url): def _addIpv6Brackets(url):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment