You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: demeter/config_reader.py
+28-29Lines changed: 28 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,11 @@
16
16
fromconfigobjimportConfigObj
17
17
18
18
19
+
classValidationException(Exception):
20
+
def__init__(self,*args,**kwargs):
21
+
Exception.__init__(self,*args,**kwargs)
22
+
23
+
19
24
classReadConfig:
20
25
21
26
def__init__(self, config_file):
@@ -191,14 +196,14 @@ def ck_ts(t, st_y, ed_y):
191
196
ts=int(t)
192
197
193
198
if (rng==0) and (ts!=1):
194
-
raiseRuntimeError('Parameter "timestep" value must be 1 if only running one year. Your start year and end year are the same in your config file. Exiting...')
199
+
raiseValidationException('Parameter "timestep" value must be 1 if only running one year. Your start year and end year are the same in your config file. Exiting...')
195
200
elif (rng==0) and (ts==1):
196
201
returnts
197
202
198
203
ck=rng/ts
199
204
200
205
ifck==0:
201
-
raiseRuntimeError('Parameter "timestep" value "{0}" is too large for start year of "{1}" and end year of "{2}". Max time step available based on year range is "{3}". Exiting...'.format(t, st_y, ed_y, ed_y-st_y))
206
+
raiseValidationException('Parameter "timestep" value "{0}" is too large for start year of "{1}" and end year of "{2}". Max time step available based on year range is "{3}". Exiting...'.format(t, st_y, ed_y, ed_y-st_y))
202
207
else:
203
208
returnts
204
209
@@ -212,7 +217,7 @@ def ck_yr(y, p):
212
217
:return: int
213
218
"""
214
219
iflen(y) !=4:
215
-
raiseRuntimeError('Year must be in four digit format (e.g., 2005) for parameter "{}". Value entered was "{}". Exiting...'.format(p, y))
220
+
raiseValidationException('Year must be in four digit format (e.g., 2005) for parameter "{}". Value entered was "{}". Exiting...'.format(p, y))
216
221
else:
217
222
returnint(y)
218
223
@@ -227,7 +232,7 @@ def ck_len(s, p, l=30):
227
232
:return: string
228
233
"""
229
234
iflen(s) >l:
230
-
raiseRuntimeError('Length of "{}" exceeds the max length of 20. Please revise. Exiting...'.format(p))
235
+
raiseValidationException('Length of "{}" exceeds the max length of 20. Please revise. Exiting...'.format(p))
231
236
else:
232
237
returns
233
238
@@ -244,7 +249,7 @@ def ck_vals(v, p, l):
244
249
ifvinl:
245
250
returnv
246
251
else:
247
-
raiseRuntimeError('Value "{0}" not in acceptable values for parameter "{1}". Acceptable values are: {2}. Exiting...'.format(v, p, l))
252
+
raiseValidationException('Value "{0}" not in acceptable values for parameter "{1}". Acceptable values are: {2}. Exiting...'.format(v, p, l))
248
253
249
254
@staticmethod
250
255
defck_limit(v, p, l):
@@ -259,7 +264,7 @@ def ck_limit(v, p, l):
259
264
if (v>=l[0]) and (v<=l[1]):
260
265
returnv
261
266
else:
262
-
raiseRuntimeError('Value "{0}" does not fall within acceptable range of values for parameter {1} where min >= {2} and max <= {3}. Exiting...'.format(v, p, l[0], l[1]))
267
+
raiseValidationException('Value "{0}" does not fall within acceptable range of values for parameter {1} where min >= {2} and max <= {3}. Exiting...'.format(v, p, l[0], l[1]))
263
268
264
269
@staticmethod
265
270
defcheck_exist(f, kind, log):
@@ -296,7 +301,7 @@ def create_dir(d, log):
296
301
exceptExceptionase:
297
302
log.error(e)
298
303
log.error("ERROR: Failed to create directory.")
299
-
sys.exit(1)
304
+
raise
300
305
301
306
@staticmethod
302
307
defck_agg(a, log):
@@ -307,11 +312,11 @@ def ck_agg(a, log):
307
312
agg=int(a)
308
313
exceptTypeError:
309
314
log.error('"agg_level" parameter must be either 1 or 2. Exiting...')
310
-
sys.exit(1)
315
+
raise
311
316
312
317
ifagg<1oragg>2:
313
318
log.error('"agg_level" parameter must be either 1 or 2. Exiting...')
314
-
sys.exit(1)
319
+
raiseValidationException
315
320
316
321
else:
317
322
returnagg
@@ -433,7 +438,7 @@ def ck_limit(v, p, l):
433
438
if (v>=l[0]) and (v<=l[1]):
434
439
returnv
435
440
else:
436
-
raiseRuntimeError('Value "{0}" does not fall within acceptable range of values for parameter {1} where min >= {2} and max <= {3}. Exiting...'.format(v, p, l[0], l[1]))
441
+
raiseValidationException('Value "{0}" does not fall within acceptable range of values for parameter {1} where min >= {2} and max <= {3}. Exiting...'.format(v, p, l[0], l[1]))
437
442
438
443
@staticmethod
439
444
defck_len(s, p, l=20):
@@ -446,7 +451,7 @@ def ck_len(s, p, l=20):
446
451
:return: string
447
452
"""
448
453
iflen(s) >l:
449
-
raiseRuntimeError('Length of "{}" exceeds the max length of 20. Please revise. Exiting...'.format(p))
454
+
raiseValidationException('Length of "{}" exceeds the max length of 20. Please revise. Exiting...'.format(p))
450
455
else:
451
456
returns
452
457
@@ -696,14 +701,14 @@ def ck_ts(t, st_y, ed_y):
696
701
ts=int(t)
697
702
698
703
if (rng==0) and (ts!=1):
699
-
raiseRuntimeError('Parameter "timestep" value must be 1 if only running one year. Your start year and end year are the same in your config file. Exiting...')
704
+
raiseValidationException('Parameter "timestep" value must be 1 if only running one year. Your start year and end year are the same in your config file. Exiting...')
700
705
elif (rng==0) and (ts==1):
701
706
returnts
702
707
703
708
ck=rng/ts
704
709
705
710
ifck==0:
706
-
raiseRuntimeError('Parameter "timestep" value "{0}" is too large for start year of "{1}" and end year of "{2}". Max time step available based on year range is "{3}". Exiting...'.format(t, st_y, ed_y, ed_y-st_y))
711
+
raiseValidationException('Parameter "timestep" value "{0}" is too large for start year of "{1}" and end year of "{2}". Max time step available based on year range is "{3}". Exiting...'.format(t, st_y, ed_y, ed_y-st_y))
707
712
else:
708
713
returnts
709
714
@@ -717,7 +722,7 @@ def ck_yr(y, p):
717
722
:return: int
718
723
"""
719
724
iflen(y) !=4:
720
-
raiseRuntimeError('Year must be in four digit format (e.g., 2005) for parameter "{}". Value entered was "{}". Exiting...'.format(p, y))
725
+
raiseValidationException('Year must be in four digit format (e.g., 2005) for parameter "{}". Value entered was "{}". Exiting...'.format(p, y))
721
726
else:
722
727
returnint(y)
723
728
@@ -732,7 +737,7 @@ def ck_len(s, p, l=20):
732
737
:return: string
733
738
"""
734
739
iflen(s) >l:
735
-
raiseRuntimeError('Length of "{}" exceeds the max length of 20. Please revise. Exiting...'.format(p))
740
+
raiseValidationException('Length of "{}" exceeds the max length of 20. Please revise. Exiting...'.format(p))
736
741
else:
737
742
returns
738
743
@@ -749,7 +754,7 @@ def ck_vals(v, p, l):
749
754
ifvinl:
750
755
returnv
751
756
else:
752
-
raiseRuntimeError('Value "{0}" not in acceptable values for parameter "{1}". Acceptable values are: {2}. Exiting...'.format(v, p, l))
757
+
raiseValidationException('Value "{0}" not in acceptable values for parameter "{1}". Acceptable values are: {2}. Exiting...'.format(v, p, l))
753
758
754
759
@staticmethod
755
760
defck_limit(v, p, l):
@@ -764,7 +769,7 @@ def ck_limit(v, p, l):
764
769
if (v>=l[0]) and (v<=l[1]):
765
770
returnv
766
771
else:
767
-
raiseRuntimeError('Value "{0}" does not fall within acceptable range of values for parameter {1} where min >= {2} and max <= {3}. Exiting...'.format(v, p, l[0], l[1]))
772
+
raiseValidationException('Value "{0}" does not fall within acceptable range of values for parameter {1} where min >= {2} and max <= {3}. Exiting...'.format(v, p, l[0], l[1]))
0 commit comments