Commit d889bad0 authored by erodrigu's avatar erodrigu
Browse files

Search the variable based on the namelist name

parent d0e0e589
......@@ -9,10 +9,24 @@
#
############################################
from AutoRPE.UtilsRPE.Vault import load_vault
from AutoRPE.UtilsRPE.Getter import get_type_of_contents
import re
import AutoRPE.UtilsRPE.Error as Error
# Few functions needed
import AutoRPE.UtilsRPE.RegexPattern as RegexPattern
def get_namelist_name(_line):
"""
Return the namelist name if found
"""
_line = _line.strip()
if not _line:
return False
first_character = _line[0]
if first_character == "&":
if _line.count("!"):
_line = _line[:_line.find("!")]
return _line[1:].strip()
return False
def line_is_assignation(_line):
"""
Determine whether the string provided is a assignation.
......@@ -58,9 +72,6 @@ if __name__ == "__main__":
print("Not able to load %s !\n" % vault_path)
exit(1)
# Regex pattern used to catch the name of the variable which is being modified
pattern = "^([^\!]*)=([^\!]*)"
# Loop over all the namelists
for namelist in args.namelists:
# Open the file and read the content
......@@ -69,23 +80,27 @@ if __name__ == "__main__":
# Loop over all the lines
for index, _l in enumerate(data):
# In case the line corresponds to an assignation
_name = get_namelist_name(_l)
if _name:
namelist_name = _name
if line_is_assignation(_l):
# Use regex to read the variable name and the assigned value
m = re.search(pattern, _l)
m = re.search(RegexPattern.assignation_val, _l)
var, value = m.groups()
# Use the vault to determine the type of the variable
try:
type_of_var = vault.get_variable_from_namelist_by_name(var.strip()).type
variable = vault.get_variable_from_namelist_by_name(var.strip(), namelist_name)
except Error.VariableNotFound:
continue
# In case the variable is a rpe_var, add a %val suffix
if type_of_var == "rpe_var":
if variable.type == "rpe_var":
var = "%s%%val" % var.strip()
data[index] = "%s = %s\n" % (var, value)
# In case the variable is a fld_n derived type, add a custom fix
if type_of_var == "fld_n":
elif variable.type == "fld_n":
split_value = value.split(",")
# Add -1 to the first possition of the list
value = value.replace(split_value[1], "-1, " + split_value[1])
data[index] = "%s = %s" % (var, value)
......
......@@ -46,6 +46,8 @@ extended_type_declaration = "^ *type\s*,(\s*public\s*,|)\s*extends *\(\s*(\w*) *
general_type_declaration = '^ *type\s*(,|)\s*(public\s*::|public|)\s*(,|)\s*(extends\((\w*)\)\s*::|) * (\w*)'
# Captures end of groups
end_type_statement = "^ *END TYPE"
# Captures the variable and value of an assignation
assignation_val = "^([^\!]*)=([^\!]*)"
# REAL (Can be a variable declaration, or a function ret_val specification)
......
......@@ -50,8 +50,8 @@ class Vault:
for variable in self.variables:
self.indexed_variables[variable.id] = variable
def get_variable_from_namelist_by_name(self, var_name):
var = [var for var in self.variables if var.name == var_name and var.appears_in_namelist]
def get_variable_from_namelist_by_name(self, var_name, namelist):
var = [var for var in self.variables if var.name == var_name and var.appears_in_namelist and namelist in var.appears_in_namelist]
if len(var) > 0:
return var[0]
else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment