@@ -180,13 +180,13 @@ class AppendDestination(DataframeAction):
180180 assume convention on naming
181181 TODO check if it's ok with FilterDest
182182 """
183-
184183 def __init__ (self , * args , ** kwargs ) -> None :
185184 self .already_called = False
186- # self.destinations = list(ConnectionRoles)
185+ self .destinations = list (ConnectionRoles )
187186 # default=list(ConnectionRoles),
188187 super ().__init__ (
189- * args , choices = CustomConnectionRolesChoices ([e .name for e in ConnectionRoles ]),
188+ * args ,
189+ choices = CustomConnectionRolesChoices ([e .name for e in ConnectionRoles ]),
190190 default = list (ConnectionRoles ), ** kwargs )
191191
192192
@@ -198,11 +198,17 @@ def __call__(self, parser, namespace, values, option_string=None):
198198 # TODO change the default ?
199199 # setattr(namespace, self.dest, [])
200200 print ("Already set" )
201+ print ("setting value %r" % values )
202+ # print("setting value %r" % self.destinations)
201203 # to make it unique
202- self .destinations = list (set (self .destinations .append (values )))
204+ self .destinations .append (values )
205+ self .destinations = list (set (self .destinations ))
206+ print ("new result %r" % self .destinations )
203207 else :
204- self .destinations = values
208+ print ("Received first value %s" % values )
209+ self .destinations = [values ]
205210
211+ self .already_called = True
206212 # df_name + "destinations"
207213 setattr (namespace , self .dest , self .destinations )
208214 # pcap1 = getattr(namespace, self.df_name + "1")
@@ -332,11 +338,11 @@ def __init__(self, df_name: str, **kwargs) -> None:
332338
333339 # assert self.field == "tcpdest" or self.field == "mptcpdest"
334340 # self.mptcp = mptcp
335- # self.seen
341+ # self.seen
336342 # init with all destinations
337343 self .destinations = list (ConnectionRoles )
338344 self .already_called = False
339- # TODO it could set choices automatically
345+ # TODO it could set choices automatically
340346 super ().__init__ (df_name , ** kwargs )
341347
342348 def __call__ (self , parser , namespace , values , option_string = None ):
@@ -454,6 +460,41 @@ def gen_bicap_parser(protocol, dest=False):
454460 # protocol=protocol,
455461 return gen_pcap_parser (input_pcaps = input_pcaps , direction = dest )
456462
463+ # argparse_completer.ACArgumentParser
464+ class MpTcpAnalyzerParser (argparse_completer .ACArgumentParser ):
465+ '''
466+ Wrapper around cmd2 argparse completer
467+ Should allow to switch backends easily.
468+ Also allows to do some postprocessing once the arguments are parsed
469+ like loading dataframes etc
470+
471+ '''
472+
473+ # def _parse_known_args(self, arg_strings, namespace):
474+ def parse_known_args (self , args = None , namespace = None ):
475+ """
476+ override it just to postprocess arguments
477+ """
478+
479+ # returns a 2-item tuple
480+ known , unknown = super ().parse_known_args (args , namespace )
481+ # print("res", res)
482+
483+ # TODO call filter_dataframe ?
484+ if getattr (known , "_dataframes" , None ):
485+ for name , df in known ._dataframes .items ():
486+ # print
487+ # so now we can filter the destination ?
488+ log .debug ("dataframe [%s] in namespace" % name )
489+ else :
490+ log .debug ("No dataframe in namespace" )
491+
492+ # postprocessing for filtering destination
493+
494+ log .debug ("MpTcpAnalyzerParser parser finished" )
495+ # TODO pass along known, dataframes ?
496+ return (known , unknown )
497+
457498
458499# map pcaps to a group
459500def gen_pcap_parser (
@@ -464,7 +505,7 @@ def gen_pcap_parser(
464505 parents = [],
465506 # TODO get rid of this/skip-stream
466507 skip_subflows : bool = True ,
467- ) -> argparse_completer . ACArgumentParser :
508+ ) -> MpTcpAnalyzerParser :
468509 """
469510 Generates a parser with common options.
470511 This parser can be completed or overridden by its children.
@@ -543,6 +584,7 @@ def _pcap(name, pcapAction="store", filterAction="store"):
543584 # choices=CustomConnectionRolesChoices([e.name for e in ConnectionRoles]),
544585 # TODO check how it works/FilterDest
545586 action = partial (AppendDestination , df_name ),
587+ # action=partial(AppendDestination, df_name),
546588 # type parameter is a function/callable
547589 type = lambda x : ConnectionRoles .from_string (x ),
548590 help = 'Filter flows according to their direction'
@@ -563,32 +605,4 @@ def _pcap(name, pcapAction="store", filterAction="store"):
563605 return parser
564606
565607
566- # argparse_completer.ACArgumentParser
567- class MpTcpAnalyzerParser (argparse_completer .ACArgumentParser ):
568- '''
569- Wrapper around cmd2 argparse completer
570- Should allow to switch backends easily.
571- Also allows to do some postprocessing once the arguments are parsed
572- like loading dataframes etc
573-
574- '''
575-
576- # def _parse_known_args(self, arg_strings, namespace):
577- def parse_known_args (self , args = None , namespace = None ):
578- """
579- override it just to postprocess arguments
580- """
581- res = super ().parse_known_args (args , namespace )
582-
583- # TODO call filter_dataframe ?
584- if getattr (res , "_dataframes" , None ):
585- for name , df in res ._dataframes .items ():
586- # print
587- # so now we can filter the destination ?
588- pass
589-
590- # postprocessing for filtering destination
591-
592- logging .debug ("MpTcpAnalyzerParser parser finished" )
593- return res
594608
0 commit comments