Skip to content

Commit b02eb8d

Browse files
EnovotnyEric Novotny
andauthored
fix: Update ini import (#212)
update ini import to deal with custom variables in the ini file. --------- Co-authored-by: Eric Novotny <nov00002@umn.edu>
1 parent c855125 commit b02eb8d

4 files changed

Lines changed: 686 additions & 18 deletions

File tree

cwmscli/usgs/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,23 @@ def getusgs_ratings(office, days_back, api_root, api_key, api_key_loc, rating_su
105105
type=str,
106106
help="filename of ratings ini file to be processed",
107107
)
108+
@click.option(
109+
"--dry-run",
110+
is_flag=True,
111+
default=False,
112+
help="Preview changes without updating the database",
113+
)
108114
@api_root_option
109115
@api_key_option
110116
@api_key_loc_option
111117
@requires(reqs.cwms, reqs.requests)
112-
def ratingsinifileimport(filename, api_root, api_key, api_key_loc):
118+
def ratingsinifileimport(filename, dry_run, api_root, api_key, api_key_loc):
113119
from cwmscli.usgs.rating_ini_file_import import rating_ini_file_import
114120

115121
api_key = get_api_key(api_key, api_key_loc)
116-
rating_ini_file_import(api_root=api_root, api_key=api_key, ini_filename=filename)
122+
rating_ini_file_import(
123+
api_root=api_root, api_key=api_key, ini_filename=filename, dry_run=dry_run
124+
)
117125

118126

119127
@usgs_group.command("measurements", help="Store USGS measurements into CWMS database")

cwmscli/usgs/rating_ini_file_import.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
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)

tests/usgs/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)