1111}
1212
1313
14- def rating_ini_file_import (api_root , api_key , ini_filename ):
15- init_cwms_session (cwms , api_root = api_root , api_key = "apikey " + api_key )
14+ def rating_ini_file_import (api_root , api_key , ini_filename , dry_run = False ):
15+ if dry_run :
16+ logging .info ("DRY RUN MODE - no changes will be made" )
17+ init_cwms_session (cwms , api_root = api_root )
18+ else :
19+ init_cwms_session (cwms , api_root = api_root , api_key = "apikey " + api_key )
1620
1721 logging .info (f"CDA connection: { api_root } " )
1822 logging .info (f"Opening ini file: { ini_filename } " )
@@ -21,7 +25,6 @@ def rating_ini_file_import(api_root, api_key, ini_filename):
2125 ini_file .close ()
2226
2327 params = {}
24- keywords = ["cwms_office" , "db_base" , "db_exsa" , "db_corr" , "localid" ]
2528 rating_errors = []
2629 for i in range (len (lines )):
2730 line = lines [i ][:- 1 ].strip ()
@@ -33,26 +36,41 @@ def rating_ini_file_import(api_root, api_key, ini_filename):
3336 continue
3437 if "=" in line :
3538 fields = line .split ("=" )
36- if fields [0 ] in keywords :
37- if fields [0 ] == "cwms_office" :
38- fields [1 ] = fields [1 ].upper ()
39- params [fields [0 ]] = fields [1 ]
39+ key = fields [0 ].strip ().lower ()
40+ value = fields [1 ].strip ()
41+ if key == "cwms_office" :
42+ value = value .upper ()
43+ params [key ] = value
4044 else :
4145 fields = parse_ini_line (line )
4246 if fields [0 ] in rating_types .keys ():
43- rating_db_type = rating_types [fields [0 ]]["db_type" ]
44- if f"$(${ rating_db_type } )" in fields :
45- rating_spec = params [rating_db_type ].replace (
46- "\$localid" , params ["localid" ]
47- )
47+ # Find the database reference in the fields (e.g., $($db_tail), $($db_exsa), etc.)
48+ db_key = None
49+ for field in fields [1 :]:
50+ if field .startswith ("$(" ) and field .endswith (")" ):
51+ # Extract the key name from $(...), e.g., "db_exsa" from "$($db_exsa)"
52+ potential_key = field [2 :- 1 ].lstrip ("$" )
53+ if potential_key in params :
54+ db_key = potential_key
55+ break
56+
57+ if db_key :
58+ rating_spec = params [db_key ]
59+ # Substitute any custom parameters found in rating_spec
60+ for param_key , param_value in params .items ():
61+ placeholder = f"\\ ${ param_key } "
62+ if placeholder in rating_spec :
63+ rating_spec = rating_spec .replace (placeholder , param_value )
4864 logging .info (f"Updating rating specification: { rating_spec } " )
4965 try :
5066 update_rating_spec (
5167 rating_spec ,
52- params [ "cwms_office" ] ,
68+ params . get ( "cwms_office" ) ,
5369 rating_types [fields [0 ]]["db_disc" ],
70+ dry_run = dry_run ,
5471 )
55- logging .info ("SUCCESS: rating specification changes stored" )
72+ if not dry_run :
73+ logging .info ("SUCCESS: rating specification changes stored" )
5674 except :
5775 logging .error (
5876 "ERROR: rating specificataion could not be update"
@@ -109,7 +127,7 @@ def parse_ini_line(line):
109127 return fields
110128
111129
112- def update_rating_spec (rating_id , office_id , db_disc ):
130+ def update_rating_spec (rating_id , office_id , db_disc , dry_run = False ):
113131 rating_spec = cwms .get_rating_spec (rating_id = rating_id , office_id = office_id )
114132 data = rating_spec .df
115133 data = data .drop ("effective-dates" , axis = 1 )
@@ -127,4 +145,8 @@ def update_rating_spec(rating_id, office_id, db_disc):
127145 disc = data .loc [0 , "description" ]
128146 logging .info (f"Saving specification discription as: { disc } " )
129147 data_xml = cwms .rating_spec_df_to_xml (data )
130- cwms .store_rating_spec (data = data_xml , fail_if_exists = False )
148+ if dry_run :
149+ logging .info ("DRY RUN: Would store rating specification with XML:" )
150+ logging .info (data_xml )
151+ else :
152+ cwms .store_rating_spec (data = data_xml , fail_if_exists = False )
0 commit comments