From: Claudio-Daniel Freire Date: Thu, 21 Apr 2011 10:34:24 +0000 (+0200) Subject: XML generation fixes: don't break when mandatory fields are missing - people might... X-Git-Tag: nepi_v2~140 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=43c7e1d3a1ffef4b893380432e63b787fa3b6dea;p=nepi.git XML generation fixes: don't break when mandatory fields are missing - people might want to save anyway --- diff --git a/src/nepi/util/parser/_xml.py b/src/nepi/util/parser/_xml.py index b3241cd8..b2407760 100644 --- a/src/nepi/util/parser/_xml.py +++ b/src/nepi/util/parser/_xml.py @@ -5,6 +5,8 @@ from nepi.core.attributes import Attribute from nepi.util.parser.base import ExperimentData, ExperimentParser from xml.dom import minidom +import sys + class XmlExperimentParser(ExperimentParser): def to_xml(self, experiment_description): data = self.to_data(experiment_description) @@ -21,7 +23,13 @@ class XmlExperimentParser(ExperimentParser): else: self.box_data_to_xml(doc, elements_tags, guid, data) doc.appendChild(exp_tag) - xml = doc.toprettyxml(indent=" ", encoding="UTF-8") + + try: + xml = doc.toprettyxml(indent=" ", encoding="UTF-8") + except: + print >>sys.stderr, "Oops: generating XML from %s" % (data,) + raise + return xml def testbed_data_to_xml(self, doc, parent_tag, guid, data): @@ -65,22 +73,24 @@ class XmlExperimentParser(ExperimentParser): def factory_attributes_data_to_xml(self, doc, parent_tag, guid, data): factory_attributes_tag = doc.createElement("factory_attributes") for (name, value) in data.get_factory_attribute_data(guid): - factory_attribute_tag = doc.createElement("factory_attribute") - factory_attributes_tag.appendChild(factory_attribute_tag) - factory_attribute_tag.setAttribute("name", name) - factory_attribute_tag.setAttribute("value", str(value)) - factory_attribute_tag.setAttribute("type", self.type_to_standard(value)) + if value is not None: + factory_attribute_tag = doc.createElement("factory_attribute") + factory_attributes_tag.appendChild(factory_attribute_tag) + factory_attribute_tag.setAttribute("name", name) + factory_attribute_tag.setAttribute("value", str(value)) + factory_attribute_tag.setAttribute("type", self.type_to_standard(value)) if factory_attributes_tag.hasChildNodes(): parent_tag.appendChild(factory_attributes_tag) def attributes_data_to_xml(self, doc, parent_tag, guid, data): attributes_tag = doc.createElement("attributes") for name, value in data.get_attribute_data(guid): - attribute_tag = doc.createElement("attribute") - attributes_tag.appendChild(attribute_tag) - attribute_tag.setAttribute("name", name) - attribute_tag.setAttribute("value", str(value)) - attribute_tag.setAttribute("type", self.type_to_standard(value)) + if value is not None: + attribute_tag = doc.createElement("attribute") + attributes_tag.appendChild(attribute_tag) + attribute_tag.setAttribute("name", name) + attribute_tag.setAttribute("value", str(value)) + attribute_tag.setAttribute("type", self.type_to_standard(value)) if attributes_tag.hasChildNodes(): parent_tag.appendChild(attributes_tag) diff --git a/src/nepi/util/parser/base.py b/src/nepi/util/parser/base.py index e8580f53..8fe87904 100644 --- a/src/nepi/util/parser/base.py +++ b/src/nepi/util/parser/base.py @@ -6,6 +6,13 @@ import sys class ExperimentData(object): def __init__(self): self.data = dict() + + def __repr__(self): + return "%s(%r)" % (self.__class__, self.data) + + def __str__(self): + from pprint import pformat + return "%s:%s" % (self.__class__, pformat(self.data)) @property def guids(self): diff --git a/test/testbeds/planetlab/design.py b/test/testbeds/planetlab/design.py index 3ab52b16..f11dcc21 100755 --- a/test/testbeds/planetlab/design.py +++ b/test/testbeds/planetlab/design.py @@ -2,13 +2,9 @@ # -*- coding: utf-8 -*- from nepi.core.design import ExperimentDescription, FactoriesProvider -import os -import shutil -import test_util import unittest -import uuid -class NetnsDesignTestCase(unittest.TestCase): +class PlanetlabDesignTestCase(unittest.TestCase): def test_design_if(self): exp_desc = ExperimentDescription() testbed_version = "01"