Skip to content
Snippets Groups Projects
Commit b3dd8f02 authored by imcovangent's avatar imcovangent
Browse files

Extended CMDOWS functions (add_header(), add_element_to_element_of_uid(),...

Extended CMDOWS functions (add_header(), add_element_to_element_of_uid(), etc.) based on KE-chain requirements.


Former-commit-id: b4a49bcb81a57615566576fb7d3e6d59be9fd85f
parent 3eba9036
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,8 @@ import urllib2
import logging
import os
import copy
from datetime import datetime
from lxml import etree
from lxml.etree import ElementTree
from collections import Counter, OrderedDict
......@@ -16,7 +18,6 @@ parser = etree.XMLParser(remove_blank_text=True)
logger = logging.getLogger(__name__)
class CMDOWS(object):
"""Class for with various methods for checking and manipulating CMDOWS files"""
......@@ -268,6 +269,20 @@ class CMDOWS(object):
# ----------------------------------------- #
# Add / change file functions #
# ----------------------------------------- #
def add_header(self, creator, description, timestamp=None, fileVersion="0.0"):
"""Method to add a header to a CMDOWS file."""
# Assert header element exists
el = self.ensure_abs_xpath('/cmdows/header')
# Add elements
el.add("creator", creator)
el.add("description", description)
el.add("timestamp", str(datetime.now()) if timestamp is None else timestamp)
el.add("fileVersion", fileVersion)
el.add("cmdowsVersion", self.version())
return
def add_contact(self, name, email, uid, company=None, department=None, function=None, address=None, telephone=None, country=None, roles=None):
"""Method to add a contact element to the organization branch."""
......@@ -329,6 +344,33 @@ class CMDOWS(object):
return
def add_element_to_element_of_uid(self, uid, element_to_add, expected_tag_uid_el=None, expected_tag_new_el=None):
"""Generic method to add a subelement to an element with a certain UID."""
# Assert that there is a UID element
xpath = self.get_xpath_of_uid(uid, expected_tag=expected_tag_uid_el)
# Assert that an inputs element is given
if expected_tag_new_el:
self.assert_element_tag(element_to_add, expected_tag_new_el)
# Get element of uid
parent_element = self.get_element_of_uid(uid)
# Add new element
parent_element.append(element_to_add)
return
def add_dc_inputs_element(self, dc_uid, inputs_element):
"""Method to add a inputs element to a DC element."""
self.add_element_to_element_of_uid(dc_uid, inputs_element, 'designCompetence', 'inputs')
return
def add_dc_outputs_element(self, dc_uid, outputs_element):
"""Method to add a outputs element to a DC element."""
self.add_element_to_element_of_uid(dc_uid, outputs_element, 'designCompetence', 'outputs')
return
def add_dc_general_info(self, dc_uid, description, status=None, creation_date=None, owner_uid=None, creator_uid=None,
operator_uid=None, model_definition=None):
"""Method to add a general info element to a dc branch."""
......@@ -455,6 +497,11 @@ class CMDOWS(object):
parent_element.append(remote_component_element)
return
def add_new_parameters_from_element(self, parameters_element):
"""Method to add the new parameters based on a parameters element."""
# TODO: Create this function...
return
def add_actor(self, contact_uid, role):
"""Method to add a role element to the organization branch."""
......@@ -518,27 +565,44 @@ class CMDOWS(object):
"""Method to remove the inputs of a CMDOWS file executableBlock entry"""
assert self.get_element_of_uid(exblock_uid).getparent().getparent().tag == 'executableBlocks', \
'UID ' + exblock_uid + ' does not seem to refer to an executableBlock.'
self.remove_children_of(exblock_uid, children_to_remove=['inputs'])
self.remove_children_of_xpath(exblock_uid, children_to_remove=['inputs'])
def remove_outputs(self, exblock_uid):
"""Method to remove the outputs of a CMDOWS file executableBlock entry"""
assert self.get_element_of_uid(exblock_uid).getparent().getparent().tag == 'executableBlocks', \
'UID ' + exblock_uid + ' does not seem to refer to an executableBlock.'
self.remove_children_of(exblock_uid, children_to_remove=['outputs'])
self.remove_children_of_xpath(exblock_uid, children_to_remove=['outputs'])
def remove_in_and_outputs(self, exblock_uid):
"""Method to remove the in- and outputs of a CMDOWS file executableBlock entry"""
assert self.get_element_of_uid(exblock_uid).getparent().getparent().tag == 'executableBlocks', \
'UID ' + exblock_uid + ' does not seem to refer to an executableBlock.'
self.remove_children_of(exblock_uid, children_to_remove=['inputs', 'outputs'])
self.remove_children_of_xpath(exblock_uid, children_to_remove=['inputs', 'outputs'])
def remove_children_of(self, uid, children_to_remove='all'):
"""Method to remove the children of a CMDOWS file element."""
def remove_children_of_uid(self, uid, children_to_remove='__all__', children_to_keep='__none__'):
"""Method to remove the children of a CMDOWS file element based on a UID."""
el = self.get_element_of_uid(uid)
for child in el.iterchildren():
if children_to_remove == 'all':
if children_to_remove == '__all__':
if not child.tag in children_to_keep:
el.remove(child)
elif child.tag in children_to_remove:
if child.tag in children_to_keep:
raise AssertionError("Child tag {} is both in the children_to_remove and _keep list"
.format(child.tag))
el.remove(child)
def remove_children_of_xpath(self, xpath, children_to_remove='__all__', children_to_keep='__none__'):
"""Method to remove the children of a CMDOWS file element based on an XPath."""
el = self.root.xpath(xpath)
for child in el.iterchildren():
if children_to_remove == '__all__':
if not child.tag in children_to_keep:
el.remove(child)
elif child.tag in children_to_remove:
if child.tag in children_to_keep:
raise AssertionError("Child tag {} is both in the children_to_remove and _keep list"
.format(child.tag))
el.remove(child)
def remove_parameters_element(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment