Source code for smia.utilities.properties_file_utils
"""This class contains utility methods related to the ConfigMap file."""importloggingimportconfigparserimportos.pathfromsmia.logic.exceptionsimportCriticalErrorfromsmia.utilities.smia_general_infoimportSMIAGeneralInfo_logger=logging.getLogger(__name__)# ---------------# General methods# ---------------
[docs]defcreate_empty_file():""" This method creates a properties configuration file for SMIA with default values ('#'). """config_prop=configparser.RawConfigParser()config_prop['DT']={'dt.version':'#','dt.agentID':'#','dt.password':'#','dt.xmpp-server':'#','dt.web-ui':'#'}config_prop['AAS']={'aas.meta-model.version':'#','aas.model.serialization':'#','aas.model.folder':'#','aas.model.file':'#'}config_prop['ONTOLOGY']={'ontology.file':'#','ontology.inside-aasx':'#'}update_properties_file_by_parser(config_prop)
[docs]defupdate_properties_file_by_parser(new_config_parser):""" This method updates the content of the properties file by a given parser. Args: new_config_parser (configparser.RawConfigParser): the parser with the new content for the properties file. """with(open(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_GENERAL_PROPERTIES_FILENAME,'w')aspropertiesFile):new_config_parser.write(propertiesFile)
[docs]defupdate_properties_file_by_bytes(new_config_bytes):""" This method updates the content of the properties file by given properties in the form of bytes. Args: new_config_bytes (configparser.RawConfigParser): the parser with the new content for the properties file. """config_parser=configparser.RawConfigParser()new_config_str=new_config_bytes.decode('utf-8')config_parser.read_string(new_config_str)# Only not defined data need to be updatedarchive_config_parser=configparser.RawConfigParser()archive_config_parser.read(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_GENERAL_PROPERTIES_FILENAME)forsectioninarchive_config_parser.sections():forkey,valueinarchive_config_parser.items(section):ifvalue=='#':archive_config_parser[section][key]=config_parser[section][key]update_properties_file_by_parser(archive_config_parser)
# ----------------------------------# Methods related to AAS information# ----------------------------------
[docs]defget_aas_general_property(property_name):""" This method returns the property of the AAS set in the ConfigMap by the AAS Controller during the deployment process. This information is stored in "general.properties" file within "AAS" section (with the 'aas.' prefix). Args: property_name (str): The name of the property. Returns: str: The general property of the AAS. """# Read submodels configurationconfig_sm=configparser.RawConfigParser()config_sm.read(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_GENERAL_PROPERTIES_FILENAME)try:returnconfig_sm['AAS']['aas.'+property_name]exceptKeyErrorase:_logger.error("The 'general.properties' file in the ConfigMap is not valid.")returnNone
[docs]defset_aas_general_property(property_name,property_value):""" This method sets a new value for a property of the AAS. The information is stored in "general.properties" file within "AAS" section (with the 'aas.' prefix). Args: property_name (str): The name of the property. property_value (str): The new value of the property. """# Read submodels configurationconfig_prop=configparser.RawConfigParser()config_prop.read(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_GENERAL_PROPERTIES_FILENAME)try:config_prop['AAS']['aas.'+property_name]=property_valueupdate_properties_file_by_parser(config_prop)exceptKeyErrorase:_logger.error("The 'general.properties' file in the ConfigMap is not valid regarding 'AAS' section.")returnNone
[docs]defget_aas_model_filepath():""" This method returns the AAS model file path. The AAS model is specified in the ‘general.properties’ file within the ‘AAS’ section, with 'aas.model.file' attribute. Returns: str: The AAS model file path within the SMIA Archive. """# config_sm = configparser.RawConfigParser()# config_sm.read(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH + '/' + SMIAGeneralInfo.CM_GENERAL_PROPERTIES_FILENAME)# return SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH + '/' + config_sm['AAS']['aas.model.file']returnSMIAGeneralInfo.CONFIGURATION_AAS_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_AAS_MODEL_FILENAME
# --------------------------------------# Methods related to DT information# --------------------------------------
[docs]defget_dt_general_property(property_name):""" This method returns the DT property set in the ConfigMap during the deployment process. This information is stored in the ‘general.properties’ file within the ‘DT’ section (with the 'dt.' prefix). Returns: str: The general property of the DT. """config_sm=configparser.RawConfigParser()ifnotos.path.exists(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_GENERAL_PROPERTIES_FILENAME):raiseCriticalError("General properties file not found in ["+SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_GENERAL_PROPERTIES_FILENAME+"], the path is invalid.")config_sm.read(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_GENERAL_PROPERTIES_FILENAME)try:returnconfig_sm['DT']['dt.'+property_name]exceptKeyErrorase:raiseCriticalError("The 'general.properties' file in the ConfigMap does not have valid keys.")
# --------------------------------------# Methods related to ontology information# --------------------------------------
[docs]defget_ontology_general_property(property_name):""" This method returns the property of the OWL set in the ConfigMap by the AAS Controller during the deployment process. This information is stored in "general.properties" file within "ONTOLOGY" section (with the 'ontology.' prefix). Args: property_name (str): The name of the property. Returns: str: The general property of the ontology. """# Read submodels configurationconfig_sm=configparser.RawConfigParser()config_sm.read(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_GENERAL_PROPERTIES_FILENAME)try:returnconfig_sm['ONTOLOGY']['ontology.'+property_name]exceptKeyErrorase:_logger.error("The 'initialization.properties' file in the SMIA Archive is not valid or it is not defined.")returnNone
[docs]defget_defined_ontology_filepath():""" This method returns the OWL ontology file path. If the file for the OWL ontology is specified in the ‘general.properties’ file, it is located within the ‘ONTOLOGY’ section, with 'ontology.owl.file' attribute. Returns: str: The ontology file path within the SMIA Archive. """config_sm=configparser.RawConfigParser()config_sm.read(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_GENERAL_PROPERTIES_FILENAME)returnSMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+config_sm['ONTOLOGY']['ontology.file']
[docs]defcreate_ontology_file(file_bytes_content):""" This method creates the ontology OWL file with the given file content. This method is used when the ontology file is inside the AASX package, so it must be created outside in order to be available for loading. Returns: str: The new ontology OWL file path within the SMIA Archive. """# The new ontology file is created in SMIA Archive inside configuration foldernew_ontology_file_path=SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+"ontology.owl"withopen(new_ontology_file_path,"wb")asbinary_file:# Write bytes to filebinary_file.write(file_bytes_content)returnnew_ontology_file_path
# --------------------------------------# Methods related to asset information# --------------------------------------
[docs]defget_asset_type():""" This method returns the asset type of the AAS set in the ConfigMap by the AAS Controller during the deployment process. This information is stored in "asset.properties" file. Returns: str: The asset type of the AAS. """# Read submodels configurationconfig_sm=configparser.RawConfigParser()config_sm.read(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_ASSET_PROPERTIES_FILENAME)returnconfig_sm['DEFAULT']['assetType']
# ----------------------------# Methods related to submodels# ----------------------------
[docs]defget_submodel_names():""" This method returns all submodel names that have been selected for the AAS instance. It is the submodel properties file sections of the AAS configuration in the ConfigMap that will determine which submodels these are. Returns: list(str): A list with the sections of the submodel properties file, and therefore, the submodel names."""# Read submodels configurationconfig_sm=configparser.RawConfigParser()config_sm.read(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_SM_PROPERTIES_FILENAME)returnconfig_sm.sections()
[docs]defget_submodel_information(submodel_name):""" This method returns the submodel information of a specific submodel, from the submodel properties file of the configuration from the ConfigMap. Args: submodel_name (str): The name of the submodel. To read from the submodel properties file, it is also the name of the section. Returns: dict: The submodel information in the same format as the submodel properties file content. """# Read submodels configurationconfig_sm=configparser.RawConfigParser()config_sm.read(SMIAGeneralInfo.CONFIGURATION_FOLDER_PATH+'/'+SMIAGeneralInfo.CM_SM_PROPERTIES_FILENAME)returnconfig_sm.items(submodel_name)