"""This class contains utility methods related to submodels."""importloggingimportosfromlxmlimportetreefromsmia.utilitiesimportsmia_archive_utilsfromsmia.utilitiesimportconfigmap_utilsfromsmia.utilities.smia_general_infoimportSMIAGeneralInfo_logger=logging.getLogger(__name__)# ------------------------# Methods related to files# ------------------------
[docs]defcreate_submodel_folder():"""Create folder to save submodels."""os.mkdir(SMIAGeneralInfo.SUBMODEL_FOLDER_PATH)
[docs]defcreate_submodel_files(submodel_names_list):""" This method creates all the files associated to the selected submodels. Args: submodel_names_list (list(str)): list of submodel names. """forsubmodel_nameinsubmodel_names_list:# Get the submodel information from ConfigMapsubmodel_data=configmap_utils.get_submodel_information(submodel_name)matchsubmodel_name:case"technical-data-submodel":create_technical_data_sm(submodel_data)case"configuration-submodel":create_configuration_sm(submodel_data)case_:_logger.warning("Submodel not found.")break
# -------------------------------------# Methods related to specific submodels# -------------------------------------
[docs]defcreate_technical_data_sm(submodel_data):""" This method creates the 'Technical Data' submodel XML file. Args: submodel_data (dict): information of the submodel in the same format as the submodel properties file content. """# Generate the XML of the submodelsubmodel_xml_content=etree.Element("submodel",name="technical_data_submodel")technical_data_level=etree.SubElement(submodel_xml_content,"technical_data")# Add data to XMLfor(key,val)insubmodel_data:# print(key + ": " + val)etree.SubElement(technical_data_level,key).text=val# Write the content of submodel in a filesmia_archive_utils.xml_to_file(SMIAGeneralInfo.SUBMODEL_FOLDER_PATH+'/'+SMIAGeneralInfo.TECHNICAL_DATA_SM_FILENAME,etree.tostring(submodel_xml_content))
[docs]defcreate_configuration_sm(submodel_data):""" This method creates the 'Configuration' submodel XML file. Args: submodel_data (dict): information of the submodel in the same format as the submodel properties file content. """# Generate the XML of the submodelsubmodel_xml_content=etree.Element("submodel",name="configuration_submodel")configuration_level=etree.SubElement(submodel_xml_content,"configuration")# Add data to XMLfor(key,val)insubmodel_data:# print(key + ": " + val)etree.SubElement(configuration_level,key).text=val# Write the content of submodel in a filesmia_archive_utils.xml_to_file(SMIAGeneralInfo.SUBMODEL_FOLDER_PATH+'/'+SMIAGeneralInfo.CONFIGURATION_SM_FILENAME,etree.tostring(submodel_xml_content))
[docs]defcheck_if_submodel_exists(submodel_name):""" This method checks if a submodel exist by its name. Args: submodel_name(str): Name of the submodel Returns: boolean: True if submodel exists and False if not. """# First, if the submodel.properties file exists has to be checkedifos.path.isfile(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_SM_PROPERTIES_FILENAME)isFalse:returnFalseelse:# Read each submodel definition files to get the submodel# TODOreturnTrue# Under developing