config-tools: refine append_node and add get_node

Refine the "append_node" which can add new node with an attribute and
return the appended node.

The method "get_node" finds the xpath value and return it if there is an
unique node exists, otherwise it returns None.
It is used to get an xpath element node or can determine the xpath existence.

The "get_text" is replaced with "get_node". The only get_text in
hv_ram.py is modified accordingly.

Tracked-On: #5980
Signed-off-by: Yang,Yu-chu <yu-chu.yang@intel.com>
Reviewed-by: Junjie Mao <junjie.mao@intel.com>
This commit is contained in:
Yang,Yu-chu 2021-04-08 16:27:00 -07:00 committed by wenlingz
parent 81a867bc57
commit 3ed36ff02a
2 changed files with 11 additions and 7 deletions

View File

@ -191,10 +191,10 @@ def get_xml_attrib(config_file, attrib):
def count_nodes(xpath, etree): def count_nodes(xpath, etree):
return int(etree.xpath(f"count({xpath})")) return int(etree.xpath(f"count({xpath})"))
def get_text(xpath, etree): def get_node(xpath, etree):
result = etree.xpath(f"{xpath}/text()") result = etree.xpath(f"{xpath}")
assert len(result) == 1, "Internal error: cannot get texts from multiple nodes at a time" assert len(result) <= 1, f"Internal error: multiple element nodes are found for {xpath}"
return result[0] return result[0] if len(result) == 1 else None
def update_text(xpath, value, etree, overwrite=False): def update_text(xpath, value, etree, overwrite=False):
result = etree.xpath(f"{xpath}") result = etree.xpath(f"{xpath}")
@ -202,7 +202,7 @@ def update_text(xpath, value, etree, overwrite=False):
if overwrite or not result[0].text: if overwrite or not result[0].text:
result[0].text = str(value) result[0].text = str(value)
def append_node(xpath, value, etree): def append_node(xpath, value, etree, **attribute):
# Look for an existing ancestor node # Look for an existing ancestor node
parts = xpath.split("/") parts = xpath.split("/")
ancestor_level = 1 ancestor_level = 1
@ -220,7 +220,11 @@ def append_node(xpath, value, etree):
child = lxml.etree.Element(tag) child = lxml.etree.Element(tag)
ancestor.append(child) ancestor.append(child)
ancestor = child ancestor = child
child.text = str(value) if value:
child.text = str(value)
for key, value in attribute.items():
child.set(key, value)
return ancestor
def get_board_name(): def get_board_name():
""" """

View File

@ -24,7 +24,7 @@ def fn(board_etree, scenario_etree, allocation_etree):
post_launched_vm_num += 1 post_launched_vm_num += 1
hv_ram_size = common.HV_BASE_RAM_SIZE + common.POST_LAUNCHED_VM_RAM_SIZE * post_launched_vm_num hv_ram_size = common.HV_BASE_RAM_SIZE + common.POST_LAUNCHED_VM_RAM_SIZE * post_launched_vm_num
ivshmem_enabled = common.get_text("//IVSHMEM_ENABLED", scenario_etree) ivshmem_enabled = common.get_node("//IVSHMEM_ENABLED/text()", scenario_etree)
total_shm_size = 0 total_shm_size = 0
if ivshmem_enabled == 'y': if ivshmem_enabled == 'y':
raw_shmem_regions = scenario_etree.xpath("//IVSHMEM_REGION/text()") raw_shmem_regions = scenario_etree.xpath("//IVSHMEM_REGION/text()")