1
1
#!/usr/bin/env python3
2
2
3
3
import re
4
+ import sys
4
5
5
6
# -----------------------------------------------------------------------------
6
7
def add_commands (StrCommands , filenameIn = 'PARAM.in' ,
@@ -64,16 +65,22 @@ def add_commands(StrCommands, filenameIn='PARAM.in',
64
65
raise TypeError ('StrCommands is not a string, StrCommands=' ,
65
66
StrCommands )
66
67
68
+ # return if is an empty string
69
+ if len (StrCommands ) == 0 :
70
+ return
71
+
67
72
# get all the commands in an array
68
73
command_I = StrCommands .split (',' )
69
74
75
+ IsChanged_I = [False for i in range (len (command_I ))]
76
+
70
77
with open (filenameIn , 'rt' ) as params :
71
78
lines = list (params )
72
79
73
80
# loop through all the lines
74
81
for iLine , line in enumerate (lines ):
75
82
# loop through all the commands
76
- for command in command_I :
83
+ for icom , command in enumerate ( command_I ) :
77
84
# well the line may contain the command name + ExtraStr
78
85
commands_line = line .split ()
79
86
@@ -98,13 +105,22 @@ def add_commands(StrCommands, filenameIn='PARAM.in',
98
105
if ((re .search (rf'\b{ ExtraStr } \b' , line ) and not DoUseMarker ) or
99
106
(re .search (rf'\b{ ExtraStr } \^(?=\W)' , line , re .IGNORECASE ) and DoUseMarker )):
100
107
lines [iLine ] = '#' + line
108
+ IsChanged_I [icom ] = True
101
109
elif not DoUseMarker :
102
110
# DoUseMarker = 0 and ExtraStr = None
103
111
lines [iLine ] = '#' + line
112
+ IsChanged_I [icom ] = True
104
113
else :
105
114
# DoUseMarker = 1 and ExtraStr = None
106
115
if '^' in line :
107
116
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!!!" )
108
124
109
125
with open (filenameOut , 'w' ) as file_output :
110
126
for line in lines :
@@ -137,16 +153,22 @@ def remove_commands(StrCommands, filenameIn='PARAM.in',
137
153
raise TypeError ('StrCommands is not a string, StrCommands=' ,
138
154
StrCommands )
139
155
156
+ # return if is an empty string
157
+ if len (StrCommands )== 0 :
158
+ return
159
+
140
160
# get all the commands in an array
141
161
command_I = StrCommands .split (',' )
142
162
163
+ IsChanged_I = [False for i in range (len (command_I ))]
164
+
143
165
with open (filenameIn , 'rt' ) as params :
144
166
lines = list (params )
145
167
146
168
# loop through all the lines
147
169
for iLine , line in enumerate (lines ):
148
170
# loop through all the commands
149
- for command in command_I :
171
+ for icom , command in enumerate ( command_I ) :
150
172
# well the line may contain the command name + ExtraStr
151
173
commands_line = line .split ()
152
174
@@ -170,14 +192,23 @@ def remove_commands(StrCommands, filenameIn='PARAM.in',
170
192
if ((re .search (rf'\b{ ExtraStr } \b' , line ) and not DoUseMarker ) or
171
193
(re .search (rf'\b{ ExtraStr } \^(?=\W)' , line , re .IGNORECASE ) and DoUseMarker )):
172
194
lines [iLine ] = line [1 :]
195
+ IsChanged_I [icom ] = True
173
196
elif not DoUseMarker :
174
197
# DoUseMarker = 0 and ExtraStr = None
175
198
lines [iLine ] = line [1 :]
199
+ IsChanged_I [icom ] = True
176
200
else :
177
201
# DoUseMarker = 1 and ExtraStr = None
178
202
if '^' in line :
179
203
lines [iLine ] = line [1 :]
204
+ IsChanged_I [icom ] = True
180
205
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
+
181
212
with open (filenameOut , 'w' ) as file_output :
182
213
for line in lines :
183
214
file_output .write (line )
@@ -288,11 +319,25 @@ def replace_commands(DictParam, filenameIn='PARAM.in',
288
319
if not isinstance (DictParam ,dict ):
289
320
raise TypeError ('DictParam is not a dict, DictParam=' , DictParam )
290
321
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
+
291
328
with open (filenameIn , 'rt' ) as params :
292
329
lines = list (params )
293
330
294
331
# 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
+
296
341
# loop through all lines
297
342
for iLine , line in enumerate (lines ):
298
343
# well the line may contain the command name + ExtraStr
@@ -302,14 +347,6 @@ def replace_commands(DictParam, filenameIn='PARAM.in',
302
347
if len (commands_line ) == 0 :
303
348
continue
304
349
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
-
313
350
# obtain the parameter list for the command to be replaced
314
351
strParam_I = DictParam [NameCommand ].split (',' )
315
352
@@ -344,13 +381,22 @@ def replace_commands(DictParam, filenameIn='PARAM.in',
344
381
if ((re .search (rf'\b{ ExtraStr } \b' , line ) and not DoUseMarker ) or
345
382
(re .search (rf'\b{ ExtraStr } \^(?=\W)' , line , re .IGNORECASE ) and DoUseMarker )):
346
383
lines [iLine + 1 :iLine + 1 + len_comm_orig ] = lines_command
384
+ IsChanged_I [icom ] = True
347
385
elif not DoUseMarker :
348
386
# DoUseMarker = 0 and ExtraStr = None
349
387
lines [iLine + 1 :iLine + 1 + len_comm_orig ] = lines_command
388
+ IsChanged_I [icom ] = True
350
389
else :
351
390
# DoUseMarker = 1 and ExtraStr = None
352
391
if '^' in line :
353
392
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!!!" )
354
400
355
401
with open (filenameOut , 'w' ) as file_output :
356
402
for line in lines :
@@ -420,13 +466,19 @@ def change_param_value(DictParam, filenameIn='PARAM.in',
420
466
if not isinstance (DictParam ,dict ):
421
467
raise TypeError ('DictParam is not a dict, DictParam=' , DictParam )
422
468
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
+
423
475
with open (filenameIn , 'rt' ) as params :
424
476
lines = list (params )
425
477
426
478
# loop through all lines
427
479
for iLine , line in enumerate (lines ):
428
480
# loop through all keys
429
- for key in DictParam .keys ():
481
+ for ikey , key in enumerate ( DictParam .keys () ):
430
482
# change the value if:
431
483
# 1. the name of the parameter is in the line if
432
484
# DoUseMarker = 0
@@ -437,12 +489,20 @@ def change_param_value(DictParam, filenameIn='PARAM.in',
437
489
value = DictParam [key ]
438
490
if isinstance (value , str ):
439
491
lines [iLine ] = value + '\t \t \t ' + key + '\n '
492
+ IsChanged_I [ikey ] = True
440
493
else :
441
494
try :
442
495
lines [iLine ] = str (value )+ '\t \t \t ' + key + '\n '
496
+ IsChanged_I [ikey ] = True
443
497
except Exception as error :
444
498
raise TypeError (error , "Value cannot convert to a string." )
445
499
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
+
446
506
with open (filenameOut , 'w' ) as file_output :
447
507
for line in lines :
448
508
file_output .write (line )
0 commit comments