@@ -41,19 +41,13 @@ class CustomParser(optparse.OptionParser):
4141 def __init__ (self , * args , ** kwargs ):
4242 super (CustomParser , self ).__init__ (* args , ** kwargs )
4343
44- def custom_error (self , msg ):
45- """custom_error (msg : string)
44+ def morph_error (self , msg , error ):
45+ """morph_error (msg : string)
4646
4747 Print a message incorporating 'msg' to stderr and exit. Does
4848 not print usage.
4949 """
50- dis = (
51- "To except this error on Python, "
52- "please use 'except SystemExit:'."
53- )
54- self .exit (
55- 2 , "%s: error: %s\n %s\n " % (self .get_prog_name (), msg , dis )
56- )
50+ raise error (f"{ self .get_prog_name ()} : { msg } " )
5751
5852 parser = CustomParser (
5953 usage = "\n " .join (
@@ -513,14 +507,17 @@ def single_morph(
513507 parser , opts , pargs , stdout_flag = True , python_wrap = False , pymorphs = None
514508):
515509 if len (pargs ) < 2 :
516- parser .error ("You must supply MORPHFILE and TARGETFILE." )
510+ parser .morph_error (
511+ "You must supply MORPHFILE and TARGETFILE." , TypeError
512+ )
517513 elif len (pargs ) > 2 and not python_wrap :
518- parser .error (
514+ parser .morph_error (
519515 "Too many arguments. Make sure you only supply "
520- "MORPHFILE and TARGETFILE."
516+ "MORPHFILE and TARGETFILE." ,
517+ TypeError ,
521518 )
522519 elif not (len (pargs ) == 2 or len (pargs ) == 6 ) and python_wrap :
523- parser .error ("Python wrapper error." )
520+ parser .morph_error ("Python wrapper error." , RuntimeError )
524521
525522 # Get the PDFs
526523 # If we get from python, we may wrap, which has input size 4
@@ -534,9 +531,9 @@ def single_morph(
534531 x_target , y_target = getPDFFromFile (pargs [1 ])
535532
536533 if y_morph is None :
537- parser .error (f"No data table found in: { pargs [0 ]} ." )
534+ parser .morph_error (f"No data table found in: { pargs [0 ]} ." , ValueError )
538535 if y_target is None :
539- parser .error (f"No data table found in: { pargs [1 ]} ." )
536+ parser .morph_error (f"No data table found in: { pargs [1 ]} ." , ValueError )
540537
541538 # Get tolerance
542539 tolerance = 1e-08
@@ -555,8 +552,8 @@ def single_morph(
555552 and opts .xmax is not None
556553 and opts .xmax <= opts .xmin
557554 ):
558- e = "xmin must be less than xmax"
559- parser .custom_error ( e )
555+ emsg = "xmin must be less than xmax"
556+ parser .morph_error ( emsg , ValueError )
560557
561558 # Set up the morphs
562559 chain = morphs .MorphChain (config )
@@ -613,7 +610,9 @@ def single_morph(
613610 squeeze_dict_in .update ({f"a{ idx } " : float (coeff )})
614611 idx += 1
615612 except ValueError :
616- parser .error (f"{ coeff } could not be converted to float." )
613+ parser .morph_error (
614+ f"{ coeff } could not be converted to float." , ValueError
615+ )
617616 squeeze_poly_deg = len (squeeze_dict_in .keys ())
618617 squeeze_morph = morphs .MorphSqueeze ()
619618 chain .append (squeeze_morph )
@@ -733,7 +732,7 @@ def single_morph(
733732 # Adjust all parameters
734733 refiner .refine (* refpars )
735734 except ValueError as e :
736- parser .custom_error (str (e ))
735+ parser .morph_error (str (e ), ValueError )
737736 # Smear is not being refined, but baselineslope needs to refined to apply
738737 # smear
739738 # Note that baselineslope is only added to the refine list if smear is
@@ -744,7 +743,7 @@ def single_morph(
744743 "baselineslope" , baselineslope = config ["baselineslope" ]
745744 )
746745 except ValueError as e :
747- parser .custom_error (str (e ))
746+ parser .morph_error (str (e ), ValueError )
748747 else :
749748 chain (x_morph , y_morph , x_target , y_target )
750749
@@ -833,9 +832,9 @@ def single_morph(
833832 stdout_flag = stdout_flag ,
834833 )
835834
836- except (FileNotFoundError , RuntimeError ):
835+ except (FileNotFoundError , RuntimeError ) as e :
837836 save_fail_message = "Unable to save to designated location."
838- parser .custom_error (save_fail_message )
837+ parser .morph_error (save_fail_message , type ( e ) )
839838
840839 if opts .plot :
841840 pairlist = [chain .xy_target_out , chain .xy_morph_out ]
@@ -877,25 +876,29 @@ def multiple_targets(parser, opts, pargs, stdout_flag=True, python_wrap=False):
877876 # Custom error messages since usage is distinct when --multiple tag is
878877 # applied
879878 if len (pargs ) < 2 :
880- parser .custom_error (
879+ parser .morph_error (
881880 "You must supply a FILE and DIRECTORY. "
882- "See --multiple-targets under --help for usage."
881+ "See --multiple-targets under --help for usage." ,
882+ TypeError ,
883883 )
884884 elif len (pargs ) > 2 :
885- parser .custom_error (
886- "Too many arguments. You must only supply a FILE and a DIRECTORY."
885+ parser .morph_error (
886+ "Too many arguments. You must only supply a FILE and a DIRECTORY." ,
887+ TypeError ,
887888 )
888889
889890 # Parse paths
890891 morph_file = Path (pargs [0 ])
891892 if not morph_file .is_file ():
892- parser .custom_error (
893- f"{ morph_file } is not a file. Go to --help for usage."
893+ parser .morph_error (
894+ f"{ morph_file } is not a file. Go to --help for usage." ,
895+ FileNotFoundError ,
894896 )
895897 target_directory = Path (pargs [1 ])
896898 if not target_directory .is_dir ():
897- parser .custom_error (
898- f"{ target_directory } is not a directory. Go to --help for usage."
899+ parser .morph_error (
900+ f"{ target_directory } is not a directory. Go to --help for usage." ,
901+ NotADirectoryError ,
899902 )
900903
901904 # Get list of files from target directory
@@ -932,12 +935,14 @@ def multiple_targets(parser, opts, pargs, stdout_flag=True, python_wrap=False):
932935 )
933936 except KeyError :
934937 if opts .serfile is not None :
935- parser .custom_error (
936- "The requested field was not found in the metadata file."
938+ parser .morph_error (
939+ "The requested field was not found in the metadata file." ,
940+ KeyError ,
937941 )
938942 else :
939- parser .custom_error (
940- "The requested field is missing from a file header."
943+ parser .morph_error (
944+ "The requested field is missing from a file header." ,
945+ KeyError ,
941946 )
942947 else :
943948 # Default is alphabetical sort
@@ -959,9 +964,9 @@ def multiple_targets(parser, opts, pargs, stdout_flag=True, python_wrap=False):
959964 save_morphs_here = io .create_morphs_directory (save_directory )
960965
961966 # Could not create directory or find names to save morphs as
962- except (FileNotFoundError , RuntimeError ):
967+ except (FileNotFoundError , RuntimeError ) as e :
963968 save_fail_message = "\n Unable to create directory"
964- parser .custom_error (save_fail_message )
969+ parser .morph_error (save_fail_message , type ( e ) )
965970
966971 try :
967972 save_names = io .get_multisave_names (
@@ -970,7 +975,7 @@ def multiple_targets(parser, opts, pargs, stdout_flag=True, python_wrap=False):
970975 # Could not create directory or find names to save morphs as
971976 except FileNotFoundError :
972977 save_fail_message = "\n Unable to read from save names file"
973- parser .custom_error (save_fail_message )
978+ parser .morph_error (save_fail_message , FileNotFoundError )
974979
975980 # Morph morph_file against all other files in target_directory
976981 morph_results = {}
@@ -1019,9 +1024,9 @@ def multiple_targets(parser, opts, pargs, stdout_flag=True, python_wrap=False):
10191024 verbose = opts .verbose ,
10201025 stdout_flag = stdout_flag ,
10211026 )
1022- except (FileNotFoundError , RuntimeError ):
1027+ except (FileNotFoundError , RuntimeError ) as e :
10231028 save_fail_message = "Unable to save summary to directory."
1024- parser .custom_error (save_fail_message )
1029+ parser .morph_error (save_fail_message , type ( e ) )
10251030
10261031 # Plot the values of some parameter for each target if requested
10271032 if plot_opt :
@@ -1038,8 +1043,9 @@ def multiple_targets(parser, opts, pargs, stdout_flag=True, python_wrap=False):
10381043 # Not an available parameter to plot or no values found for the
10391044 # parameter
10401045 if param_list is None :
1041- parser .custom_error (
1042- "Cannot find specified plot parameter. No plot shown."
1046+ parser .morph_error (
1047+ "Cannot find specified plot parameter. No plot shown." ,
1048+ KeyError ,
10431049 )
10441050 else :
10451051 try :
@@ -1051,9 +1057,10 @@ def multiple_targets(parser, opts, pargs, stdout_flag=True, python_wrap=False):
10511057 # i.e. --smear is not selected as an option, but smear is the
10521058 # plotting parameter
10531059 except ValueError :
1054- parser .custom_error (
1060+ parser .morph_error (
10551061 "The plot parameter is missing values for at least one "
1056- "morph and target pair. No plot shown."
1062+ "morph and target pair. No plot shown." ,
1063+ ValueError ,
10571064 )
10581065
10591066 return morph_results
@@ -1063,25 +1070,29 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True, python_wrap=False):
10631070 # Custom error messages since usage is distinct when --multiple tag is
10641071 # applied
10651072 if len (pargs ) < 2 :
1066- parser .custom_error (
1073+ parser .morph_error (
10671074 "You must supply a DIRECTORY and FILE. "
1068- "See --multiple-morphs under --help for usage."
1075+ "See --multiple-morphs under --help for usage." ,
1076+ TypeError ,
10691077 )
10701078 elif len (pargs ) > 2 :
1071- parser .custom_error (
1072- "Too many arguments. You must only supply a DIRECTORY and FILE."
1079+ parser .morph_error (
1080+ "Too many arguments. You must only supply a DIRECTORY and FILE." ,
1081+ TypeError ,
10731082 )
10741083
10751084 # Parse paths
10761085 target_file = Path (pargs [1 ])
10771086 if not target_file .is_file ():
1078- parser .custom_error (
1079- f"{ target_file } is not a file. Go to --help for usage."
1087+ parser .morph_error (
1088+ f"{ target_file } is not a file. Go to --help for usage." ,
1089+ FileNotFoundError ,
10801090 )
10811091 morph_directory = Path (pargs [0 ])
10821092 if not morph_directory .is_dir ():
1083- parser .custom_error (
1084- f"{ morph_directory } is not a directory. Go to --help for usage."
1093+ parser .morph_error (
1094+ f"{ morph_directory } is not a directory. Go to --help for usage." ,
1095+ NotADirectoryError ,
10851096 )
10861097
10871098 # Get list of files from morph directory
@@ -1118,12 +1129,14 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True, python_wrap=False):
11181129 )
11191130 except KeyError :
11201131 if opts .serfile is not None :
1121- parser .custom_error (
1122- "The requested field was not found in the metadata file."
1132+ parser .morph_error (
1133+ "The requested field was not found in the metadata file." ,
1134+ KeyError ,
11231135 )
11241136 else :
1125- parser .custom_error (
1126- "The requested field is missing from a PDF file header."
1137+ parser .morph_error (
1138+ "The requested field is missing from a PDF file header." ,
1139+ KeyError ,
11271140 )
11281141 else :
11291142 # Default is alphabetical sort
@@ -1145,9 +1158,9 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True, python_wrap=False):
11451158 save_morphs_here = io .create_morphs_directory (save_directory )
11461159
11471160 # Could not create directory or find names to save morphs as
1148- except (FileNotFoundError , RuntimeError ):
1161+ except (FileNotFoundError , RuntimeError ) as e :
11491162 save_fail_message = "\n Unable to create directory"
1150- parser .custom_error (save_fail_message )
1163+ parser .morph_error (save_fail_message , type ( e ) )
11511164
11521165 try :
11531166 save_names = io .get_multisave_names (
@@ -1156,7 +1169,7 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True, python_wrap=False):
11561169 # Could not create directory or find names to save morphs as
11571170 except FileNotFoundError :
11581171 save_fail_message = "\n Unable to read from save names file"
1159- parser .custom_error (save_fail_message )
1172+ parser .morph_error (save_fail_message , FileNotFoundError )
11601173
11611174 # Morph morph_file against all other files in target_directory
11621175 morph_results = {}
@@ -1206,9 +1219,9 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True, python_wrap=False):
12061219 stdout_flag = stdout_flag ,
12071220 mm = True ,
12081221 )
1209- except (FileNotFoundError , RuntimeError ):
1222+ except (FileNotFoundError , RuntimeError ) as e :
12101223 save_fail_message = "Unable to save summary to directory."
1211- parser .custom_error (save_fail_message )
1224+ parser .morph_error (save_fail_message , type ( e ) )
12121225
12131226 # Plot the values of some parameter for each target if requested
12141227 if plot_opt :
@@ -1225,8 +1238,9 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True, python_wrap=False):
12251238 # Not an available parameter to plot or no values found for the
12261239 # parameter
12271240 if param_list is None :
1228- parser .custom_error (
1229- "Cannot find specified plot parameter. No plot shown."
1241+ parser .morph_error (
1242+ "Cannot find specified plot parameter. No plot shown." ,
1243+ KeyError ,
12301244 )
12311245 else :
12321246 try :
@@ -1238,9 +1252,10 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True, python_wrap=False):
12381252 # i.e. --smear is not selected as an option, but smear is the
12391253 # plotting parameter
12401254 except ValueError :
1241- parser .custom_error (
1255+ parser .morph_error (
12421256 "The plot parameter is missing values for at least one "
1243- "morph and target pair. No plot shown."
1257+ "morph and target pair. No plot shown." ,
1258+ ValueError ,
12441259 )
12451260
12461261 return morph_results
0 commit comments