Skip to content

Commit c3c3eb3

Browse files
author
Cleveland
committed
add an optional pre and post parser hook and and add_parser_arg hook
1 parent 86a066c commit c3c3eb3

File tree

1 file changed

+74
-10
lines changed

1 file changed

+74
-10
lines changed

opppy/interactive_utils.py

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,17 @@ def append_pickle(self, args):
156156
print("This version of OPPPY is ", __version__)
157157
print("Delete the old ", args.pickle_name, "file and rebuild it")
158158
sys.exit(0)
159-
159+
160+
if hasattr(self.opppy_parser, "pre_parse"):
161+
self.opppy_parser.pre_parse(args)
162+
160163
# append new dictionary data to the pickle file
161164
append_output_dictionary(data, args.output_files, self.opppy_parser, args.append_date, args.nthreads)
162-
165+
166+
if hasattr(self.opppy_parser, "post_parse"):
167+
self.opppy_parser.post_parse(args, data)
168+
169+
163170
pickle.dump(data,open(args.pickle_name,"wb"))
164171
print("Output Data Saved To: ", args.pickle_name)
165172

@@ -170,6 +177,8 @@ def pickle_output_parser(self, subparser):
170177
pickle_parser.add_argument('-pf','--pickle_file', dest='pickle_name', help='Pickle file name to be created or appended to', required=True )
171178
pickle_parser.add_argument('-ad','--append_date', dest='append_date', help='Append the date and time to the output file name', nargs='?', type=bool, const=True, default=False)
172179
pickle_parser.add_argument('-nt','--nthreads', dest='nthreads', help='Number of threads to use during parsing', nargs='?', type=int, default=0)
180+
if hasattr(self.opppy_parser, "add_parser_args"):
181+
self.opppy_parser.add_parser_args(pickle_parser)
173182
pickle_parser.set_defaults(func=self.append_pickle)
174183

175184

@@ -184,6 +193,8 @@ def plot_dictionary_parser(self, subparser):
184193
plot_parser.add_argument('-nt','--nthreads', dest='nthreads', help='Number of threads to use during parsing', nargs='?', type=int, default=0)
185194
self.dict_ploter = plot_dictionary()
186195
self.dict_ploter.setup_parser(plot_parser)
196+
if hasattr(self.opppy_parser, "add_parser_args"):
197+
self.opppy_parser.add_parser_args(plot_parser)
187198
plot_parser.set_defaults(func=self.plot_dictionary)
188199

189200
def plot_dictionary(self, args):
@@ -196,8 +207,14 @@ def plot_dictionary(self, args):
196207
dictionaries = []
197208
file_names = []
198209
if args.output_files is not None:
210+
if hasattr(self.opppy_parser, "pre_parse"):
211+
self.opppy_parser.pre_parse(args)
199212
dictionaries, file_names = build_output_dictionary_list(args.output_files,
200-
self.opppy_parser, nthreads=args.nthreads)
213+
self.opppy_parser,
214+
nthreads=args.nthreads)
215+
if hasattr(self.opppy_parser, "post_parse"):
216+
for data in dictionaries:
217+
self.opppy_parser.post_parse(args, data)
201218
else:
202219
# get the dictionaries from the pickle files
203220
file_names = args.pickle_files
@@ -214,6 +231,8 @@ def plot_output_parser(self, subparser):
214231
input_type_parser.add_argument('-pf','--pickle_files', dest='pickle_files', help='pickle files to be plotted (run1.p run2.p etc...)', nargs='+' )
215232
input_type_parser.add_argument('-of','--output_files', dest='output_files', help='output files to be parsed and plotted (output_file1.txt output_file2.txt etc...)', nargs='+', action='append')
216233
plot_output_parser.add_argument('-nt','--nthreads', dest='nthreads', help='Number of threads to use during parsing', nargs='?', type=int, default=0)
234+
if hasattr(self.opppy_parser, "add_parser_args"):
235+
self.opppy_parser.add_parser_args(plot_output_parser)
217236
plot_output_parser.set_defaults(func=self.plot_output)
218237

219238
def get_plot_option(self):
@@ -268,9 +287,14 @@ def plot_output(self, args):
268287
dictionary_data=[]
269288
dictionary_names=[]
270289
if args.output_files is not None:
290+
if hasattr(self.opppy_parser, "pre_parse"):
291+
self.opppy_parser.pre_parse(args)
271292
dictionary_data, dictionary_names = build_output_dictionary_list(args.output_files,
272293
self.opppy_parser,
273294
nthreads=args.nthreads)
295+
if hasattr(self.opppy_parser, "post_parse"):
296+
for data in dictionary_data:
297+
self.opppy_parser.post_parse(args, data)
274298
else:
275299
# We no longer flatten this data
276300
#file_list = []
@@ -502,6 +526,8 @@ def parse_output_plot_args(self, input_string):
502526
parser.add_argument('-dn','--dictionary_name', dest='dictionary_name', help='dictionary that the plotting data is contained in', required=True, type=str)
503527
parser.add_argument('-x','--x_data', dest='x_value_name', help='dictionary data to be plotted on the x axis.', required=True)
504528
parser.add_argument('-y','--y_data', dest='y_value_names', help='dictionary data to be plotted on the y axis.', required=True, action='append')
529+
if hasattr(self.opppy_parser, "add_parser_args"):
530+
self.opppy_parser.add_parser_args(parser)
505531
add_plot_options(parser);
506532

507533
return parser.parse_args(shlex.split(input_string))
@@ -534,7 +560,6 @@ def __init__(self, opppy_dump_parser, argument_parser):
534560
'''
535561
self.dump_parser = opppy_dump_parser
536562
self.parser = argument_parser
537-
538563
self.subparser = self.parser.add_subparsers(help="Dump options", dest='command')
539564
self.pickle_dumps_parser(self.subparser)
540565
self.plot_1d_parser(self.subparser)
@@ -558,6 +583,8 @@ def plot_1d_parser(self, subparser):
558583
plot_parser.add_argument('-kw','--key_words', dest='key_words', help='Only extract the specified key_words', nargs='+', default=None )
559584
self.ploter_1d = plot_1d_dump_dictionary()
560585
self.ploter_1d.setup_parser(plot_parser)
586+
if hasattr(self.dump_parser, "add_parser_args"):
587+
self.dump_parser.add_parser_args(plot_parser)
561588
plot_parser.set_defaults(func=self.plot_1d)
562589

563590
def plot_2d_parser(self, subparser):
@@ -574,6 +601,8 @@ def plot_2d_parser(self, subparser):
574601
plot_parser.add_argument('-kw','--key_words', dest='key_words', help='Only extract the specified key_words', nargs='+', default=None )
575602
self.ploter_2d = plot_2d_dump_dictionary()
576603
self.ploter_2d.setup_parser(plot_parser)
604+
if hasattr(self.dump_parser, "add_parser_args"):
605+
self.dump_parser.add_parser_args(plot_parser)
577606
plot_parser.set_defaults(func=self.plot_2d)
578607

579608
def plot_3d_parser(self, subparser):
@@ -590,6 +619,8 @@ def plot_3d_parser(self, subparser):
590619
plot_parser.add_argument('-kw','--key_words', dest='key_words', help='Only extract the specified key_words', nargs='+', default=None )
591620
self.ploter_3d = plot_3d_dump_dictionary()
592621
self.ploter_3d.setup_parser(plot_parser)
622+
if hasattr(self.dump_parser, "add_parser_args"):
623+
self.dump_parser.add_parser_args(plot_parser)
593624
plot_parser.set_defaults(func=self.plot_3d)
594625

595626
def plot_3d(self, args):
@@ -616,6 +647,8 @@ def pickle_dumps_parser(self, subparser):
616647
pickle_parser.add_argument('-pf','--pickle_file', dest='pickle_name', help='Pickle file name to be created or appended to', required=True )
617648
pickle_parser.add_argument('-kw','--key_words', dest='key_words', help='Only extract the specified key_words', nargs='+', default=None )
618649
pickle_parser.add_argument('-nt','--nthreads', dest='nthreads', help='Specify number of threads for dump parsing', nargs='?', type=int, default=0 )
650+
if hasattr(self.dump_parser, "add_parser_args"):
651+
self.dump_parser.add_parser_args(pickle_parser)
619652
pickle_parser.set_defaults(func=self.pickle_dumps)
620653

621654
def pickle_dumps(self, args):
@@ -682,6 +715,8 @@ def plot_series_point_parser(self, subparser):
682715
# suppress the x and y variable request
683716
plot_parser.add_argument('-x','--x_data',dest='x_value_name', help=argparse.SUPPRESS)
684717
plot_parser.add_argument('-y','--y_data',dest='y_value_name', help=argparse.SUPPRESS)
718+
if hasattr(self.dump_parser, "add_parser_args"):
719+
self.dump_parser.add_parser_args(plot_parser)
685720
plot_parser.set_defaults(func=self.plot_series_point)
686721

687722
def plot_series_point(self, args):
@@ -749,6 +784,8 @@ def plot_series_line_parser(self, subparser):
749784
# suppress the x and y variable request
750785
plot_parser.add_argument('-x','--x_data',dest='x_value_name', help=argparse.SUPPRESS)
751786
plot_parser.add_argument('-y','--y_data',dest='y_value_name', help=argparse.SUPPRESS)
787+
if hasattr(self.dump_parser, "add_parser_args"):
788+
self.dump_parser.add_parser_args(plot_parser)
752789
plot_parser.set_defaults(func=self.plot_series_line)
753790

754791
def plot_series_line(self, args):
@@ -806,6 +843,8 @@ def plot_series_contour_parser(self, subparser):
806843
# suppress the x and y variable request
807844
plot_parser.add_argument('-x','--x_data',dest='x_value_name', help=argparse.SUPPRESS)
808845
plot_parser.add_argument('-y','--y_data',dest='y_value_name', help=argparse.SUPPRESS)
846+
if hasattr(self.dump_parser, "add_parser_args"):
847+
self.dump_parser.add_parser_args(plot_parser)
809848
plot_parser.set_defaults(func=self.plot_series_contour)
810849

811850
def plot_series_contour(self, args):
@@ -937,8 +976,14 @@ def append_pickle(self, args):
937976
print("Delete the old ", args.pickle_name, "file and rebuild it")
938977
sys.exit(0)
939978

979+
if hasattr(self.opppy_parser, "pre_parse"):
980+
self.opppy_parser.pre_parse(args)
981+
940982
# append new dictionary data to the pickle file
941983
append_tally_dictionary(data, args.tally_files, self.opppy_parser, args.append_date, args.nthreads)
984+
985+
if hasattr(self.opppy_parser, "post_parse"):
986+
self.opppy_parser.post_parse(args, data)
942987

943988
pickle.dump(data,open(args.pickle_name,"wb"))
944989
print("Output Data Saved To: ", args.pickle_name)
@@ -950,6 +995,8 @@ def pickle_tally_parser(self, subparser):
950995
pickle_parser.add_argument('-pf','--pickle_file', dest='pickle_name', help='Pickle file name to be created or appended to', required=True )
951996
pickle_parser.add_argument('-ad','--append_date', dest='append_date', help='Append the date and time to the output file name', nargs='?', type=bool, const=True, default=False)
952997
pickle_parser.add_argument('-nt','--nthreads', dest='nthreads', help='Number of threads to use during parsing', nargs='?', type=int, default=0)
998+
if hasattr(self.opppy_parser, "add_parser_args"):
999+
self.opppy_parser.add_parser_args(pickle_parser)
9531000
pickle_parser.set_defaults(func=self.append_pickle)
9541001

9551002

@@ -966,6 +1013,8 @@ def plot_tally_parser(self, subparser):
9661013
plot_parser.add_argument('-nt','--nthreads', dest='nthreads', help='Number of threads to use during parsing', nargs='?', type=int, default=0)
9671014
self.dict_ploter = plot_dictionary()
9681015
self.dict_ploter.setup_parser(plot_parser)
1016+
if hasattr(self.opppy_parser, "add_parser_args"):
1017+
self.opppy_parser.add_parser_args(plot_parser)
9691018
plot_parser.set_defaults(func=self.plot_tally)
9701019

9711020
def plot_tally(self, args):
@@ -978,8 +1027,14 @@ def plot_tally(self, args):
9781027
raw_dictionary_data=[]
9791028
raw_dictionary_names=[]
9801029
if args.tally_files is not None:
1030+
if hasattr(self.opppy_parser, "pre_parse"):
1031+
self.opppy_parser.pre_parse(args)
9811032
raw_dictionary_data, raw_dictionary_names = build_tally_dictionary_list(args.tally_files,
982-
self.opppy_parser, nthreads=args.nthreads)
1033+
self.opppy_parser,
1034+
nthreads=args.nthreads)
1035+
if hasattr(self.opppy_parser, "post_parse"):
1036+
for data in raw_dictionary_data:
1037+
self.opppy_parser.post_parse(args, data)
9831038
else:
9841039
for pickle_file_name in args.pickle_files:
9851040
raw_dictionary_names.append(pickle_file_name.split('/')[-1].split('.p')[0])
@@ -1016,7 +1071,8 @@ def plot_interactive_tally_parser(self, subparser):
10161071
input_type_parser.add_argument('-pf','--pickle_files', dest='pickle_files', help='pickle files to be plotted (run1.p run2.p etc...)', nargs='+' )
10171072
input_type_parser.add_argument('-tf','--tally_files', dest='tally_files', help='tally files to be parsed and plotted (tally_file1.txt tally_file2.txt etc...)', nargs='+', action='append')
10181073
plot_parser.add_argument('-nt','--nthreads', dest='nthreads', help='Number of threads to use during parsing', nargs='?', type=int, default=0)
1019-
1074+
if hasattr(self.opppy_parser, "add_parser_args"):
1075+
self.opppy_parser.add_parser_args(plot_parser)
10201076
plot_parser.set_defaults(func=self.plot_interactive_tally)
10211077

10221078
def get_plot_option(self):
@@ -1053,9 +1109,9 @@ def get_interactive_plot_parser(self):
10531109
parser.add_argument('-p','--plot', dest='plot', help='re-open plot', nargs='?', type=bool, const=True, default=False)
10541110
parser.add_argument('-l','--labels', dest='legend_labels', help='specify the legend labels [line1_label, line2_label,...]', type=str, nargs='+')
10551111
parser.add_argument('-rs','--resize', dest='plot_size', help='specify the plot size [x_size, y_size]', type=float, nargs=2)
1056-
1057-
10581112
add_plot_options(parser)
1113+
if hasattr(self.opppy_parser, "add_parser_args"):
1114+
self.opppy_parser.add_parser_args(parser)
10591115
return parser
10601116

10611117
def plot_interactive_tally(self, args):
@@ -1071,8 +1127,14 @@ def plot_interactive_tally(self, args):
10711127
raw_dictionary_data=[]
10721128
raw_dictionary_names=[]
10731129
if args.tally_files is not None:
1074-
raw_dictionary_data, raw_dictionary_names = build_tally_dictionary_list(args.tally_files,
1075-
self.opppy_parser, nthreads=args.nthreads)
1130+
if hasattr(self.opppy_parser, "pre_parse"):
1131+
self.opppy_parser.pre_parse(args)
1132+
raw_dictionary_data, raw_dictionary_names = build_tally_dictionary_list(args.tally_files,
1133+
self.opppy_parser,
1134+
nthreads=args.nthreads)
1135+
if hasattr(self.opppy_parser, "post_parse"):
1136+
for data in raw_dictionary_data:
1137+
self.opppy_parser.post_parse(args, data)
10761138
else:
10771139
for pickle_file_name in args.pickle_files:
10781140
raw_dictionary_names.append(pickle_file_name.split('/')[-1].split('.p')[0])
@@ -1321,6 +1383,8 @@ def parse_tally_plot_args(self, input_string):
13211383
parser.add_argument('-y','--y_data', dest='y_value_names', help='dictionary data to be plotted on the y axis.', required=True, action='append')
13221384
parser.add_argument('-sk','--series_key', dest='series_key', help='Series key string to access the data (i.e time or cycle)', nargs='?', required=True)
13231385
parser.add_argument('-sv','--series_value', dest='series_value', help='Series value to plot the data at (default is the last value of the series_key data)', nargs='?', action='append', type=float, default=None)
1386+
if hasattr(self.opppy_parser, "add_parser_args"):
1387+
self.opppy_parser.add_parser_args(parser)
13241388
add_plot_options(parser);
13251389

13261390
return parser.parse_args(shlex.split(input_string))

0 commit comments

Comments
 (0)