ATLAS Offline Software
Loading...
Searching...
No Matches
python.trfArgClasses.trfArgParser Class Reference
Inheritance diagram for python.trfArgClasses.trfArgParser:
Collaboration diagram for python.trfArgClasses.trfArgParser:

Public Member Functions

 __init__ (self, *args, **kwargs)
 Subclassing argparse.
 add_argument (self, *args, **kwargs)
 getProdsysDesc (self)
 defineArgGroup (self, *args)
 Define an argparse argument group for the main parser to use.
 getHelpString (self, argument)
 Return the help string for a given argument.
 dumpArgs (self)
 Return a list of all arguments understood by this transform in prodsys style.
 allArgs (self)
 Getter for argument list.
 parse_args (self, args=None, namespace=None)
 Call argument_parser parse_args, then concatenate values.

Protected Member Functions

 _parse_list_helper (self, value)

Protected Attributes

dict _helpString = {}
dict _argClass = {}
dict _argGroups = {}
dict _argKeyGroups = {}
dict _argAlias = {}

Detailed Description

Definition at line 2463 of file trfArgClasses.py.

Constructor & Destructor Documentation

◆ __init__()

python.trfArgClasses.trfArgParser.__init__ ( self,
* args,
** kwargs )

Subclassing argparse.


Changed the defualt methods for add_arguments and parse_args. This is so that the help functions and the namespace object can be more useful.

Note
self._helpString. Is a dictionary with the where the key-value pairs are the argument name and the help text.
self._argClass. Is a dictionary so the 'type' of argument, i.e. the class that it uses can be extracted.

Definition at line 2470 of file trfArgClasses.py.

2470 def __init__(self, *args, **kwargs):
2471 self._helpString = {}
2472 self._argClass = {}
2473 self._argGroups = {}
2474 self._argKeyGroups = {}
2475 self._argAlias = {}
2476 super(trfArgParser, self).__init__(*args, **kwargs)
2477

Member Function Documentation

◆ _parse_list_helper()

python.trfArgClasses.trfArgParser._parse_list_helper ( self,
value )
protected

Definition at line 2575 of file trfArgClasses.py.

2575 def _parse_list_helper(self, value):
2576 # We build on the value[0] instance as this contains the correct metadata
2577 # and object references for this instance (shallow copying can
2578 # mess up object references and deepcopy thows exceptions!)
2579 newValueObj = value[0]
2580 msg.debug('Started with: %s = %s', type(newValueObj), newValueObj)
2581 if isinstance(value[0], argSubstep):
2582 # Make sure you do not have a reference to the original value - this is a deeper copy
2583 newValues = dictSubstepMerge(value[0].value, {})
2584 elif isinstance(value[0], list):
2585 if len(value) == 1:
2586 return self._parse_list_helper(value[0])
2587 msg.debug('Handling a list of arguments for key')
2588 newValues = []
2589 for v in value:
2590 processedValueObj, processedValues = self._parse_list_helper(v)
2591 processedValueObj.value = processedValues
2592 newValues.append(processedValueObj)
2593 newValueObj = newValues
2594 return newValueObj, newValues
2595 elif isinstance(value[0].value, list):
2596 newValues = value[0].value
2597 elif isinstance(value[0].value, dict):
2598 newValues = value[0].value
2599 else:
2600 newValues = [value[0].value,]
2601 for valueObj in value[1:]:
2602 msg.debug('Value Object: %s = %s', type(valueObj), valueObj)
2603 if isinstance(value[0], argSubstep):
2604 # Special merger for lists attached to substeps
2605 newValues = dictSubstepMerge(newValues, valueObj.value)
2606 elif isinstance(valueObj.value, list):
2607 # General lists are concatenated
2608 newValues.extend(valueObj.value)
2609 elif isinstance(valueObj.value, dict):
2610 # General dictionaries are merged
2611 newValues.update(valueObj.value)
2612 else:
2613 newValues.append(valueObj.value)
2614 return newValueObj, newValues
2615

◆ add_argument()

python.trfArgClasses.trfArgParser.add_argument ( self,
* args,
** kwargs )

Definition at line 2478 of file trfArgClasses.py.

2478 def add_argument(self, *args, **kwargs):
2479 argName = args[0].lstrip('-')
2480 msg.debug('Found arg name {0}'.format(argName))
2481
2482 # Ban arguments with hyphens as they cause trouble in signature files and then
2483 # AMI tag definitions because of the auto-translation to underscores in argparse
2484 if '-' in argName:
2485 raise trfExceptions.TransformArgException(trfExit.nameToCode('TRF_ARG_ERROR'),
2486 'Transform arguments may not use hyphens (use camelCase or underscore')
2487
2488 # Prevent a crash if this argument already exists (there are valid use cases for 'grabbing' an
2489 # argument, so this is DEBUG, not WARNING)
2490 if argName in self._argClass:
2491 msg.debug('Double definition of argument {0} - ignored'.format(argName))
2492 return
2493
2494 # if there is a help function defined for the argument then populate the helpString dict
2495 if 'help' in kwargs:
2496 self._helpString[argName] = kwargs['help'] # if the help option is present for the argument then put it into the helpString dict key = argument name, value = help
2497 else:
2498 self._helpString[argName] = None
2499 if 'action' in kwargs and 'factory' in dir(kwargs['action']):
2500 self._argClass[argName] = kwargs['action'].factory
2501 elif 'type' in kwargs:
2502 self._argClass[argName] = kwargs['type']
2503 else:
2504 self._argClass[argName] = None
2505
2506 # Remove kwargs which are not understood by ArgumentParser.add_argument()
2507 strippedArgs = {}
2508 for arg in ('group',):
2509 if arg in kwargs:
2510 strippedArgs[arg] = kwargs.pop(arg)
2511
2512 # Setup aliases
2513 if len(args) > 1:
2514 for i in range(1, len(args)):
2515 argAlias = args[i].lstrip('-')
2516 msg.debug('Adding an alias of {0}: {1}'.format(argName, argAlias))
2517 self._argAlias[argAlias] = argName
2518
2519 # Optinally add an argument to an argparse argument group
2520 if 'group' in strippedArgs:
2521 if strippedArgs['group'] in self._argGroups:
2522 msg.debug('Adding argument to group {0}: ({1}; {2})'.format(strippedArgs['group'], args, kwargs))
2523 self._argGroups[strippedArgs['group']].add_argument(*args, **kwargs)
2524 self._argKeyGroups[argName] = strippedArgs['group']
2525 else:
2526 msg.warning('Argument group {0} not defined - adding argument to main parser'.format(strippedArgs['group']))
2527 msg.debug('Adding argument: ({0}; {1})'.format(args, kwargs))
2528 super(trfArgParser, self).add_argument(*args, **kwargs)
2529 else:
2530 msg.debug('Adding argument: ({0}; {1})'.format(args, kwargs))
2531 super(trfArgParser, self).add_argument(*args, **kwargs)
2532

◆ allArgs()

python.trfArgClasses.trfArgParser.allArgs ( self)

Getter for argument list.

Definition at line 2571 of file trfArgClasses.py.

2571 def allArgs(self):
2572 return list(self._helpString)
2573

◆ defineArgGroup()

python.trfArgClasses.trfArgParser.defineArgGroup ( self,
* args )

Define an argparse argument group for the main parser to use.

Definition at line 2547 of file trfArgClasses.py.

2547 def defineArgGroup(self, *args):
2548 # Get an argparse group
2549 if args[0] in self._argGroups:
2550 msg.warning('Argument group %s already exists', args[0])
2551 return
2552 self._argGroups[args[0]] = self.add_argument_group(*args)
2553

◆ dumpArgs()

python.trfArgClasses.trfArgParser.dumpArgs ( self)

Return a list of all arguments understood by this transform in prodsys style.

Arguments which are irrelevant for production are removed and the '–' is added back on

Definition at line 2564 of file trfArgClasses.py.

2564 def dumpArgs(self):
2565 keyArray = [ '--' + str(key) for key in self._helpString if key not in ('h', 'verbose', 'loglevel', 'dumpargs', 'argdict') ]
2566 keyArray.sort()
2567 print('ListOfDefaultPositionalKeys={0}'.format(keyArray))
2568
void print(char *figname, TCanvas *c1)

◆ getHelpString()

python.trfArgClasses.trfArgParser.getHelpString ( self,
argument )

Return the help string for a given argument.

Definition at line 2555 of file trfArgClasses.py.

2555 def getHelpString(self, argument):
2556 try:
2557 return(self._helpString[argument])
2558 except KeyError:
2559 raise trfExceptions.TransformArgException(trfExit.nameToCode('TRF_KEY_ERROR'), 'no help string available for argument %s' %argument)
2560 return None
2561

◆ getProdsysDesc()

python.trfArgClasses.trfArgParser.getProdsysDesc ( self)

Definition at line 2534 of file trfArgClasses.py.

2534 def getProdsysDesc(self):
2535 desc = {}
2536 for name, argClass in self._argClass.items():
2537 msg.debug('Detected the local variable {0}'.format(name))
2538 if argClass is not None:
2539 desc[name] = argClass().prodsysDescription
2540 if name in self._helpString:
2541 desc[name].update({'help': self._helpString[name]})
2542 if name in self._argKeyGroups:
2543 desc[name].update({'group':self._argKeyGroups[name]})
2544 return desc
2545

◆ parse_args()

python.trfArgClasses.trfArgParser.parse_args ( self,
args = None,
namespace = None )

Call argument_parser parse_args, then concatenate values.

Sets-up the standard argparse namespace, then use a special treatment for lists (arising from nargs='+'), where values are appropriately concatenated and a single object is returned

Returns
argument_parser namespace object

Definition at line 2621 of file trfArgClasses.py.

2621 def parse_args(self, args = None, namespace = None):
2622 if namespace:
2623 super(trfArgParser, self).parse_args(args = args, namespace = namespace)
2624 else:
2625 namespace = super(trfArgParser, self).parse_args(args = args)
2626 for k, v in namespace.__dict__.items():
2627 msg.debug('Treating key %s (%s)', k, v)
2628 if isinstance(v, list):
2629 newValueObj, newValues = self._parse_list_helper(v)
2630 if not isinstance(newValueObj, list):
2631 newValueObj.value = newValues
2632 namespace.__dict__[k] = newValueObj
2633 msg.debug('Set to %s', newValues)
2634
2635 return namespace
2636
2637

Member Data Documentation

◆ _argAlias

dict python.trfArgClasses.trfArgParser._argAlias = {}
protected

Definition at line 2475 of file trfArgClasses.py.

◆ _argClass

dict python.trfArgClasses.trfArgParser._argClass = {}
protected

Definition at line 2472 of file trfArgClasses.py.

◆ _argGroups

dict python.trfArgClasses.trfArgParser._argGroups = {}
protected

Definition at line 2473 of file trfArgClasses.py.

◆ _argKeyGroups

dict python.trfArgClasses.trfArgParser._argKeyGroups = {}
protected

Definition at line 2474 of file trfArgClasses.py.

◆ _helpString

python.trfArgClasses.trfArgParser._helpString = {}
protected

Definition at line 2471 of file trfArgClasses.py.


The documentation for this class was generated from the following file: