7
7
import yaml
8
8
from pyetm .clients import BaseClient
9
9
from pyetm .models .base import Base
10
+ from pyetm .models .warnings import WarningCollector
10
11
from pyetm .config .settings import get_settings
11
12
from pyetm .services .scenario_runners .fetch_output_curves import (
12
13
DownloadOutputCurveRunner ,
@@ -59,27 +60,32 @@ def retrieve(self, client, scenario) -> Optional[pd.DataFrame]:
59
60
60
61
except Exception as e :
61
62
self .add_warning (
62
- 'data' ,
63
- f"Failed to process curve data for { self .key } : { e } "
63
+ "data" , f"Failed to process curve data for { self .key } : { e } "
64
64
)
65
65
return None
66
66
67
67
except Exception as e :
68
68
# Unexpected error - add warning
69
- self .add_warning ('base' , f"Unexpected error retrieving curve { self .key } : { e } " )
69
+ self .add_warning (
70
+ "base" , f"Unexpected error retrieving curve { self .key } : { e } "
71
+ )
70
72
return None
71
73
72
74
def contents (self ) -> Optional [pd .DataFrame ]:
73
75
"""Open file from path and return contents"""
74
76
if not self .available ():
75
- self .add_warning ('file_path' , f"Curve { self .key } not available - no file path set" )
77
+ self .add_warning (
78
+ "file_path" , f"Curve { self .key } not available - no file path set"
79
+ )
76
80
return None
77
81
78
82
try :
79
83
df = pd .read_csv (self .file_path , index_col = 0 )
80
84
return df .dropna (how = "all" )
81
85
except Exception as e :
82
- self .add_warning ('file_path' , f"Failed to read curve file for { self .key } : { e } " )
86
+ self .add_warning (
87
+ "file_path" , f"Failed to read curve file for { self .key } : { e } "
88
+ )
83
89
return None
84
90
85
91
def remove (self ) -> bool :
@@ -92,7 +98,9 @@ def remove(self) -> bool:
92
98
self .file_path = None
93
99
return True
94
100
except Exception as e :
95
- self .add_warning ('file_path' , f"Failed to remove curve file for { self .key } : { e } " )
101
+ self .add_warning (
102
+ "file_path" , f"Failed to remove curve file for { self .key } : { e } "
103
+ )
96
104
return False
97
105
98
106
@classmethod
@@ -109,8 +117,8 @@ def from_json(cls, data: dict) -> OutputCurve:
109
117
"key" : data .get ("key" , "unknown" ),
110
118
"type" : data .get ("type" , "unknown" ),
111
119
}
112
- curve = cls .model_validate ( basic_data )
113
- curve .add_warning (' base' , f"Failed to create curve from data: { e } " )
120
+ curve = cls .model_construct ( ** basic_data )
121
+ curve .add_warning (" base" , f"Failed to create curve from data: { e } " )
114
122
return curve
115
123
116
124
@@ -135,19 +143,19 @@ def get_contents(self, scenario, curve_name: str) -> Optional[pd.DataFrame]:
135
143
curve = self ._find (curve_name )
136
144
137
145
if not curve :
138
- self .add_warning (' curves' , f"Curve { curve_name } not found in collection" )
146
+ self .add_warning (" curves" , f"Curve { curve_name } not found in collection" )
139
147
return None
140
148
141
149
if not curve .available ():
142
150
# Try to retrieve it
143
151
result = curve .retrieve (BaseClient (), scenario )
144
- # Merge any warnings from the curve retrieval
145
- self ._merge_submodel_warnings (curve )
152
+ # Merge any warnings from the curve retrieval using new system
153
+ self ._merge_submodel_warnings (curve , key_attr = "key" )
146
154
return result
147
155
else :
148
156
contents = curve .contents ()
149
- # Merge any warnings from reading contents
150
- self ._merge_submodel_warnings (curve )
157
+ # Merge any warnings from reading contents using new system
158
+ self ._merge_submodel_warnings (curve , key_attr = "key" )
151
159
return contents
152
160
153
161
@staticmethod
@@ -203,8 +211,8 @@ def get_curves_by_carrier_type(
203
211
if carrier_type not in carrier_mapping :
204
212
valid_types = ", " .join (carrier_mapping .keys ())
205
213
self .add_warning (
206
- ' carrier_type' ,
207
- f"Invalid carrier type '{ carrier_type } '. Valid types: { valid_types } "
214
+ " carrier_type" ,
215
+ f"Invalid carrier type '{ carrier_type } '. Valid types: { valid_types } " ,
208
216
)
209
217
return {}
210
218
@@ -225,25 +233,22 @@ def from_json(cls, data: list[dict]) -> OutputCurves:
225
233
Initialize OutputCurves collection from JSON data
226
234
"""
227
235
curves = []
228
- collection_warnings = {}
229
236
230
237
for curve_data in data :
231
238
try :
232
- key = curve_data ['key' ]
233
239
curve = OutputCurve .from_json (curve_data )
234
240
curves .append (curve )
235
241
except Exception as e :
236
- # Log the problematic curve but continue processing
237
- collection_warnings [f"OutputCurve(key={ key } )" ] = f"Skipped invalid curve data: { e } "
242
+ # Create a basic curve and continue processing
243
+ key = curve_data .get ("key" , "unknown" )
244
+ basic_curve = OutputCurve .model_construct (key = key , type = "unknown" )
245
+ basic_curve .add_warning (key , f"Skipped invalid curve data: { e } " )
246
+ curves .append (basic_curve )
238
247
239
248
collection = cls .model_validate ({"curves" : curves })
240
249
241
- # Add any collection-level warnings
242
- for loc , msg in collection_warnings .items ():
243
- collection .add_warning (loc , msg )
244
-
245
- # Merge warnings from individual curves
246
- collection ._merge_submodel_warnings (* curves , key_attr = 'key' )
250
+ # Merge warnings from individual curves using new system
251
+ collection ._merge_submodel_warnings (* curves , key_attr = "key" )
247
252
248
253
return collection
249
254
@@ -255,7 +260,7 @@ def from_service_result(
255
260
if not service_result .success or not service_result .data :
256
261
empty_curves = cls (curves = [])
257
262
for error in service_result .errors :
258
- empty_curves .add_warning (' base' , f"Service error: { error } " )
263
+ empty_curves .add_warning (" base" , f"Service error: { error } " )
259
264
return empty_curves
260
265
261
266
curves_list = []
@@ -281,17 +286,18 @@ def from_service_result(
281
286
curves_list .append (curve )
282
287
283
288
except Exception as e :
284
- curves_list . append (
285
- OutputCurve . model_validate ({ " key" : curve_name , " type" : " unknown"})
289
+ basic_curve = OutputCurve . model_construct (
290
+ key = curve_name , type = " unknown"
286
291
)
287
- curves_list [- 1 ].add_warning ('base' , f"Failed to process curve data: { e } " )
292
+ basic_curve .add_warning ("base" , f"Failed to process curve data: { e } " )
293
+ curves_list .append (basic_curve )
288
294
289
295
curves_collection = cls (curves = curves_list )
290
296
291
297
for error in service_result .errors :
292
- curves_collection .add_warning (' base' , f"Download warning: { error } " )
298
+ curves_collection .add_warning (" base" , f"Download warning: { error } " )
293
299
294
- curves_collection ._merge_submodel_warnings (curves_list , key_attr = ' key' )
300
+ curves_collection ._merge_submodel_warnings (* curves_list , key_attr = " key" )
295
301
296
302
return curves_collection
297
303
0 commit comments