ATLAS Offline Software
Public Member Functions | Private Member Functions | Private Attributes | List of all members
python.trfArgClasses.trfArgParser Class Reference
Inheritance diagram for python.trfArgClasses.trfArgParser:
Collaboration diagram for python.trfArgClasses.trfArgParser:

Public Member Functions

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

Private Member Functions

def _parse_list_helper (self, value)
 

Private Attributes

 _helpString
 
 _argClass
 
 _argGroups
 
 _argKeyGroups
 
 _argAlias
 

Detailed Description

Definition at line 2449 of file trfArgClasses.py.

Constructor & Destructor Documentation

◆ __init__()

def 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 2456 of file trfArgClasses.py.

2456  def __init__(self, *args, **kwargs):
2457  self._helpString = {}
2458  self._argClass = {}
2459  self._argGroups = {}
2460  self._argKeyGroups = {}
2461  self._argAlias = {}
2462  super(trfArgParser, self).__init__(*args, **kwargs)
2463 

Member Function Documentation

◆ _parse_list_helper()

def python.trfArgClasses.trfArgParser._parse_list_helper (   self,
  value 
)
private

Definition at line 2561 of file trfArgClasses.py.

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

◆ add_argument()

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

Definition at line 2464 of file trfArgClasses.py.

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

◆ allArgs()

def python.trfArgClasses.trfArgParser.allArgs (   self)

Getter for argument list.

Definition at line 2557 of file trfArgClasses.py.

2557  def allArgs(self):
2558  return list(self._helpString)
2559 

◆ defineArgGroup()

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

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

Definition at line 2533 of file trfArgClasses.py.

2533  def defineArgGroup(self, *args):
2534  # Get an argparse group
2535  if args[0] in self._argGroups:
2536  msg.warning('Argument group %s already exists', args[0])
2537  return
2538  self._argGroups[args[0]] = self.add_argument_group(*args)
2539 

◆ dumpArgs()

def 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 2550 of file trfArgClasses.py.

2550  def dumpArgs(self):
2551  keyArray = [ '--' + str(key) for key in self._helpString if key not in ('h', 'verbose', 'loglevel', 'dumpargs', 'argdict') ]
2552  keyArray.sort()
2553  print('ListOfDefaultPositionalKeys={0}'.format(keyArray))
2554 

◆ getHelpString()

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

Return the help string for a given argument.

Definition at line 2541 of file trfArgClasses.py.

2541  def getHelpString(self, argument):
2542  try:
2543  return(self._helpString[argument])
2544  except KeyError:
2545  raise trfExceptions.TransformArgException(trfExit.nameToCode('TRF_KEY_ERROR'), 'no help string available for argument %s' %argument)
2546  return None
2547 

◆ getProdsysDesc()

def python.trfArgClasses.trfArgParser.getProdsysDesc (   self)

Definition at line 2520 of file trfArgClasses.py.

2520  def getProdsysDesc(self):
2521  desc = {}
2522  for name, argClass in self._argClass.items():
2523  msg.debug('Detected the local variable {0}'.format(name))
2524  if argClass is not None:
2525  desc[name] = argClass().prodsysDescription
2526  if name in self._helpString:
2527  desc[name].update({'help': self._helpString[name]})
2528  if name in self._argKeyGroups:
2529  desc[name].update({'group':self._argKeyGroups[name]})
2530  return desc
2531 

◆ parse_args()

def 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 2607 of file trfArgClasses.py.

2607  def parse_args(self, args = None, namespace = None):
2608  if namespace:
2609  super(trfArgParser, self).parse_args(args = args, namespace = namespace)
2610  else:
2611  namespace = super(trfArgParser, self).parse_args(args = args)
2612  for k, v in namespace.__dict__.items():
2613  msg.debug('Treating key %s (%s)', k, v)
2614  if isinstance(v, list):
2615  newValueObj, newValues = self._parse_list_helper(v)
2616  if not isinstance(newValueObj, list):
2617  newValueObj.value = newValues
2618  namespace.__dict__[k] = newValueObj
2619  msg.debug('Set to %s', newValues)
2620 
2621  return namespace
2622 
2623 

Member Data Documentation

◆ _argAlias

python.trfArgClasses.trfArgParser._argAlias
private

Definition at line 2461 of file trfArgClasses.py.

◆ _argClass

python.trfArgClasses.trfArgParser._argClass
private

Definition at line 2458 of file trfArgClasses.py.

◆ _argGroups

python.trfArgClasses.trfArgParser._argGroups
private

Definition at line 2459 of file trfArgClasses.py.

◆ _argKeyGroups

python.trfArgClasses.trfArgParser._argKeyGroups
private

Definition at line 2460 of file trfArgClasses.py.

◆ _helpString

python.trfArgClasses.trfArgParser._helpString
private

Definition at line 2457 of file trfArgClasses.py.


The documentation for this class was generated from the following file:
vtune_athena.format
format
Definition: vtune_athena.py:14
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
beamspotman.dir
string dir
Definition: beamspotman.py:621
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:71
python.processes.powheg.ZZ.ZZ.__init__
def __init__(self, base_directory, **kwargs)
Constructor: all process options are set here.
Definition: ZZ.py:18
python.trfArgClasses.dictSubstepMerge
def dictSubstepMerge(dict1, dict2)
special dictionary merger which is used for substep type arguments
Definition: trfArgClasses.py:2646
confTool.parse_args
def parse_args()
Definition: confTool.py:36
str
Definition: BTagTrackIpAccessor.cxx:11