emane xml fully generated from lxml apis, removed xml functions embedded within emane nodes, emane manager, and emane models. Started consolidating emanexml logic into its own module, when it makes sense

This commit is contained in:
Blake J. Harnden 2018-07-03 18:49:36 -07:00
parent ae94c78fbb
commit f115b1a847
8 changed files with 249 additions and 424 deletions

View file

@ -59,7 +59,7 @@ def create_emane_config(node_id, emane_config, config):
emulator_element = etree.SubElement(emane_configuration, "emulator")
for emulator_config in emane_config.emulator_config:
value = config[emulator_config.id]
add_configuration(emulator_element, emane_config.id, value)
add_configuration(emulator_element, emulator_config.id, value)
nem_element = etree.SubElement(emane_configuration, "nem")
for nem_config in emane_config.nem_config:
@ -519,7 +519,7 @@ class CoreXmlWriter(object):
for model_name, config in all_configs.iteritems():
logger.info("writing emane config: %s", config)
if model_name == -1:
emane_configuration = create_emane_config(node_id, self.session.emane_config, config)
emane_configuration = create_emane_config(node_id, self.session.emane.emane_config, config)
else:
model = self.session.emane.models[model_name]
emane_configuration = create_emane_model_config(node_id, model, config)

View file

@ -0,0 +1,64 @@
from lxml import etree
from core import logger
from core.misc import utils
from core.xml import corexml
def _value_to_params(value):
"""
Helper to convert a parameter to a parameter tuple.
:param str value: value string to convert to tuple
:return: parameter tuple, None otherwise
"""
try:
values = utils.make_tuple_fromstr(value, str)
if not hasattr(values, "__iter__"):
return None
if len(values) < 2:
return None
return values
except SyntaxError:
logger.exception("error in value string to param list")
return None
def create_file(xml_element, doc_name, file_path):
doctype = '<!DOCTYPE %(doc_name)s SYSTEM "file:///usr/share/emane/dtd/%(doc_name)s.dtd">' % {"doc_name": doc_name}
corexml.write_xml_file(xml_element, file_path, doctype=doctype)
def add_param(xml_element, name, value):
etree.SubElement(xml_element, "param", name=name, value=value)
def add_configurations(xml_element, configurations, config, config_ignore):
"""
Add emane model configurations to xml element.
:param lxml.etree.Element xml_element: xml element to add emane configurations to
:param list[core.config.Configuration] configurations: configurations to add to xml
:param dict config: configuration values
:param set config_ignore: configuration options to ignore
:return:
"""
for configuration in configurations:
# ignore custom configurations
name = configuration.id
if name in config_ignore:
continue
# check if value is a multi param
value = str(config[name])
params = _value_to_params(value)
if params:
params_element = etree.SubElement(xml_element, "paramlist", name=name)
for param in params:
etree.SubElement(params_element, "item", value=param)
else:
add_param(xml_element, name, value)