Skip to content

Commit c688f53

Browse files
Stop if the corresponding action (add, remove, replace or change) is
not executed.
1 parent 47a1fa1 commit c688f53

File tree

1 file changed

+72
-12
lines changed

1 file changed

+72
-12
lines changed

Scripts/change_param.py

+72-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import re
4+
import sys
45

56
# -----------------------------------------------------------------------------
67
def add_commands(StrCommands, filenameIn='PARAM.in',
@@ -64,16 +65,22 @@ def add_commands(StrCommands, filenameIn='PARAM.in',
6465
raise TypeError('StrCommands is not a string, StrCommands=',
6566
StrCommands)
6667

68+
# return if is an empty string
69+
if len(StrCommands) == 0:
70+
return
71+
6772
# get all the commands in an array
6873
command_I=StrCommands.split(',')
6974

75+
IsChanged_I = [False for i in range(len(command_I))]
76+
7077
with open(filenameIn, 'rt') as params:
7178
lines = list(params)
7279

7380
# loop through all the lines
7481
for iLine, line in enumerate(lines):
7582
# loop through all the commands
76-
for command in command_I:
83+
for icom, command in enumerate(command_I):
7784
# well the line may contain the command name + ExtraStr
7885
commands_line = line.split()
7986

@@ -98,13 +105,22 @@ def add_commands(StrCommands, filenameIn='PARAM.in',
98105
if ((re.search(rf'\b{ExtraStr}\b', line) and not DoUseMarker) or
99106
(re.search(rf'\b{ExtraStr}\^(?=\W)', line, re.IGNORECASE) and DoUseMarker)):
100107
lines[iLine] = '#'+line
108+
IsChanged_I[icom] = True
101109
elif not DoUseMarker:
102110
# DoUseMarker = 0 and ExtraStr = None
103111
lines[iLine] = '#'+line
112+
IsChanged_I[icom] = True
104113
else:
105114
# DoUseMarker = 1 and ExtraStr = None
106115
if '^' in line:
107116
lines[iLine] = '#'+line
117+
IsChanged_I[icom] = True
118+
119+
if False in IsChanged_I:
120+
print('--------------------------------------------------------')
121+
print("command_I =", command_I)
122+
print("IsChanged_I =", IsChanged_I)
123+
sys.exit("Some commands are not added!!!")
108124

109125
with open(filenameOut, 'w') as file_output:
110126
for line in lines:
@@ -137,16 +153,22 @@ def remove_commands(StrCommands, filenameIn='PARAM.in',
137153
raise TypeError('StrCommands is not a string, StrCommands=',
138154
StrCommands)
139155

156+
# return if is an empty string
157+
if len(StrCommands)== 0:
158+
return
159+
140160
# get all the commands in an array
141161
command_I=StrCommands.split(',')
142162

163+
IsChanged_I = [False for i in range(len(command_I))]
164+
143165
with open(filenameIn, 'rt') as params:
144166
lines = list(params)
145167

146168
# loop through all the lines
147169
for iLine, line in enumerate(lines):
148170
# loop through all the commands
149-
for command in command_I:
171+
for icom, command in enumerate(command_I):
150172
# well the line may contain the command name + ExtraStr
151173
commands_line = line.split()
152174

@@ -170,14 +192,23 @@ def remove_commands(StrCommands, filenameIn='PARAM.in',
170192
if ((re.search(rf'\b{ExtraStr}\b', line) and not DoUseMarker) or
171193
(re.search(rf'\b{ExtraStr}\^(?=\W)', line, re.IGNORECASE) and DoUseMarker)):
172194
lines[iLine] = line[1:]
195+
IsChanged_I[icom] = True
173196
elif not DoUseMarker:
174197
# DoUseMarker = 0 and ExtraStr = None
175198
lines[iLine] = line[1:]
199+
IsChanged_I[icom] = True
176200
else:
177201
# DoUseMarker = 1 and ExtraStr = None
178202
if '^' in line:
179203
lines[iLine] = line[1:]
204+
IsChanged_I[icom] = True
180205

206+
if False in IsChanged_I:
207+
print('--------------------------------------------------------')
208+
print("command_I =", command_I)
209+
print("IsChanged_I =", IsChanged_I)
210+
sys.exit("Some commands are not removed!!!")
211+
181212
with open(filenameOut, 'w') as file_output:
182213
for line in lines:
183214
file_output.write(line)
@@ -288,11 +319,25 @@ def replace_commands(DictParam, filenameIn='PARAM.in',
288319
if not isinstance(DictParam,dict):
289320
raise TypeError('DictParam is not a dict, DictParam=', DictParam)
290321

322+
# return if is an empty dict
323+
if not DictParam:
324+
return
325+
326+
IsChanged_I = [False for i in range(len(DictParam.keys()))]
327+
291328
with open(filenameIn, 'rt') as params:
292329
lines = list(params)
293330

294331
# loop through all the keys
295-
for NameCommand in DictParam.keys():
332+
for icom, NameCommand in enumerate(DictParam.keys()):
333+
# check whether extra string is provided with () for the command to be replaced
334+
if '(' and ')' in NameCommand:
335+
commandLocal = NameCommand.split('(')[0]
336+
ExtraStr = NameCommand.split('(')[1].split(')')[0]
337+
else:
338+
commandLocal = NameCommand
339+
ExtraStr = None
340+
296341
# loop through all lines
297342
for iLine, line in enumerate(lines):
298343
# well the line may contain the command name + ExtraStr
@@ -302,14 +347,6 @@ def replace_commands(DictParam, filenameIn='PARAM.in',
302347
if len(commands_line) == 0:
303348
continue
304349

305-
# check whether extra string is provided with () for the command to be replaced
306-
if '(' and ')' in NameCommand:
307-
commandLocal = NameCommand.split('(')[0]
308-
ExtraStr = NameCommand.split('(')[1].split(')')[0]
309-
else:
310-
commandLocal = NameCommand
311-
ExtraStr = None
312-
313350
# obtain the parameter list for the command to be replaced
314351
strParam_I = DictParam[NameCommand].split(',')
315352

@@ -344,13 +381,22 @@ def replace_commands(DictParam, filenameIn='PARAM.in',
344381
if ((re.search(rf'\b{ExtraStr}\b', line) and not DoUseMarker) or
345382
(re.search(rf'\b{ExtraStr}\^(?=\W)', line, re.IGNORECASE) and DoUseMarker)):
346383
lines[iLine+1:iLine+1+len_comm_orig] = lines_command
384+
IsChanged_I[icom] = True
347385
elif not DoUseMarker:
348386
# DoUseMarker = 0 and ExtraStr = None
349387
lines[iLine+1:iLine+1+len_comm_orig] = lines_command
388+
IsChanged_I[icom] = True
350389
else:
351390
# DoUseMarker = 1 and ExtraStr = None
352391
if '^' in line:
353392
lines[iLine+1:iLine+1+len_comm_orig] = lines_command
393+
IsChanged_I[icom] = True
394+
395+
if False in IsChanged_I:
396+
print('--------------------------------------------------------')
397+
print("DictParam.keys =", DictParam.keys())
398+
print("IsChanged_I =", IsChanged_I)
399+
sys.exit("Some commands are not replaced!!!")
354400

355401
with open(filenameOut, 'w') as file_output:
356402
for line in lines:
@@ -420,13 +466,19 @@ def change_param_value(DictParam, filenameIn='PARAM.in',
420466
if not isinstance(DictParam,dict):
421467
raise TypeError('DictParam is not a dict, DictParam=', DictParam)
422468

469+
# return if is an empty dict
470+
if not DictParam:
471+
return
472+
473+
IsChanged_I = [False for i in range(len(DictParam.keys()))]
474+
423475
with open(filenameIn, 'rt') as params:
424476
lines = list(params)
425477

426478
# loop through all lines
427479
for iLine, line in enumerate(lines):
428480
# loop through all keys
429-
for key in DictParam.keys():
481+
for ikey, key in enumerate(DictParam.keys()):
430482
# change the value if:
431483
# 1. the name of the parameter is in the line if
432484
# DoUseMarker = 0
@@ -437,12 +489,20 @@ def change_param_value(DictParam, filenameIn='PARAM.in',
437489
value = DictParam[key]
438490
if isinstance(value, str):
439491
lines[iLine] = value+'\t\t\t'+key+'\n'
492+
IsChanged_I[ikey] = True
440493
else:
441494
try:
442495
lines[iLine] = str(value)+'\t\t\t'+key+'\n'
496+
IsChanged_I[ikey] = True
443497
except Exception as error:
444498
raise TypeError(error, "Value cannot convert to a string.")
445499

500+
if False in IsChanged_I:
501+
print('--------------------------------------------------------')
502+
print("DictParam.keys =", DictParam.keys())
503+
print("IsChanged_I =", IsChanged_I)
504+
sys.exit("Some params are not changed!!!")
505+
446506
with open(filenameOut, 'w') as file_output:
447507
for line in lines:
448508
file_output.write(line)

0 commit comments

Comments
 (0)