@@ -36,6 +36,10 @@ class PowerConfigSchemaValidationException(RsCustomException):
3636 def __init__ (self , ex : ValidationError ):
3737 super ().__init__ (f"Parsing Error: { ex .messages } " )
3838
39+ class PowerConfigNotAvailable (RsCustomException ):
40+ def __init__ (self ):
41+ super ().__init__ (f"Power config data not available" )
42+
3943class ElementType (Enum ):
4044 BRAM = 'bram'
4145 CLOCKING = 'clocking'
@@ -169,25 +173,25 @@ def post_load(self, data, **kwargs):
169173 return RsPowerConfigData (** data )
170174
171175class RsPowerConfig :
172- def __init__ (self , filepath : str ) -> None :
173- self .filepath = filepath
176+ def __init__ (self ) -> None :
177+ self .filepath = None
174178 self .data : RsPowerConfigData = None
175- self .load ()
179+ self .loaded = False
176180
177- def load (self ) -> bool :
181+ def load (self , filepath : str ) -> bool :
178182 try :
179183 # read the main power config json file
180- with open (self . filepath , 'r' ) as fd :
184+ with open (filepath , 'r' ) as fd :
181185 rawdata = json .load (fd )
182186
183187 # resolve all $ref nodes
184- resolved_data = jsonref .replace_refs (rawdata , base_uri = 'file:///' + os .path .abspath (os .path .dirname (self . filepath )).replace ('\\ ' , '/' ) + '/' )
188+ resolved_data = jsonref .replace_refs (rawdata , base_uri = 'file:///' + os .path .abspath (os .path .dirname (filepath )).replace ('\\ ' , '/' ) + '/' )
185189
186190 # verify json structure
187191 data = RsPowerConfigDataSchema ().load (resolved_data )
188192
189193 # store data
190- self .data = data
194+ self .filepath , self . data , self . loaded = filepath , data , True
191195
192196 except FileNotFoundError as ex :
193197 raise PowerConfigFileNotFoundException (self .filepath )
@@ -201,13 +205,24 @@ def load(self) -> bool:
201205 raise ex
202206 return True
203207
208+ def is_loaded (self ) -> bool :
209+ return self .loaded
210+
204211 def get_static_component (self , type : ElementType ) -> RsStaticPowerElement :
212+ # raise power data not available exception
213+ if not self .loaded :
214+ raise PowerConfigNotAvailable ()
215+
205216 comps = [c for c in self .data .static if c .type == type ]
206217 if comps :
207218 return comps [0 ]
208219 raise PowerConfigStaticComponentNotFoundException (type .value )
209220
210221 def get_component (self , type : ElementType ) -> RsDynamicPowerComponent :
222+ # raise power data not available exception
223+ if not self .loaded :
224+ raise PowerConfigNotAvailable ()
225+
211226 comps = [c for c in self .data .components if c .type == type ]
212227 if comps :
213228 return comps [0 ]
0 commit comments