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 2427 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 2434 of file trfArgClasses.py.

2434  def __init__(self, *args, **kwargs):
2435  self._helpString = {}
2436  self._argClass = {}
2437  self._argGroups = {}
2438  self._argKeyGroups = {}
2439  self._argAlias = {}
2440  super(trfArgParser, self).__init__(*args, **kwargs)
2441 

Member Function Documentation

◆ _parse_list_helper()

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

Definition at line 2539 of file trfArgClasses.py.

2539  def _parse_list_helper(self, value):
2540  # We build on the value[0] instance as this contains the correct metadata
2541  # and object references for this instance (shallow copying can
2542  # mess up object references and deepcopy thows exceptions!)
2543  newValueObj = value[0]
2544  msg.debug('Started with: %s = %s', type(newValueObj), newValueObj)
2545  if isinstance(value[0], argSubstep):
2546  # Make sure you do not have a reference to the original value - this is a deeper copy
2547  newValues = dictSubstepMerge(value[0].value, {})
2548  elif isinstance(value[0], list):
2549  if len(value) == 1:
2550  return self._parse_list_helper(value[0])
2551  msg.debug('Handling a list of arguments for key')
2552  newValues = []
2553  for v in value:
2554  processedValueObj, processedValues = self._parse_list_helper(v)
2555  processedValueObj.value = processedValues
2556  newValues.append(processedValueObj)
2557  newValueObj = newValues
2558  return newValueObj, newValues
2559  elif isinstance(value[0].value, list):
2560  newValues = value[0].value
2561  elif isinstance(value[0].value, dict):
2562  newValues = value[0].value
2563  else:
2564  newValues = [value[0].value,]
2565  for valueObj in value[1:]:
2566  msg.debug('Value Object: %s = %s', type(valueObj), valueObj)
2567  if isinstance(value[0], argSubstep):
2568  # Special merger for lists attached to substeps
2569  newValues = dictSubstepMerge(newValues, valueObj.value)
2570  elif isinstance(valueObj.value, list):
2571  # General lists are concatenated
2572  newValues.extend(valueObj.value)
2573  elif isinstance(valueObj.value, dict):
2574  # General dictionaries are merged
2575  newValues.update(valueObj.value)
2576  else:
2577  newValues.append(valueObj.value)
2578  return newValueObj, newValues
2579 

◆ add_argument()

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

Definition at line 2442 of file trfArgClasses.py.

2442  def add_argument(self, *args, **kwargs):
2443  argName = args[0].lstrip('-')
2444  msg.debug('Found arg name {0}'.format(argName))
2445 
2446  # Ban arguments with hyphens as they cause trouble in signature files and then
2447  # AMI tag definitions because of the auto-translation to underscores in argparse
2448  if '-' in argName:
2449  raise trfExceptions.TransformArgException(trfExit.nameToCode('TRF_ARG_ERROR'),
2450  'Transform arguments may not use hyphens (use camelCase or underscore')
2451 
2452  # Prevent a crash if this argument already exists (there are valid use cases for 'grabbing' an
2453  # argument, so this is DEBUG, not WARNING)
2454  if argName in self._argClass:
2455  msg.debug('Double definition of argument {0} - ignored'.format(argName))
2456  return
2457 
2458  # if there is a help function defined for the argument then populate the helpString dict
2459  if 'help' in kwargs:
2460  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
2461  else:
2462  self._helpString[argName] = None
2463  if 'action' in kwargs and 'factory' in dir(kwargs['action']):
2464  self._argClass[argName] = kwargs['action'].factory
2465  elif 'type' in kwargs:
2466  self._argClass[argName] = kwargs['type']
2467  else:
2468  self._argClass[argName] = None
2469 
2470  # Remove kwargs which are not understood by ArgumentParser.add_argument()
2471  strippedArgs = {}
2472  for arg in ('group',):
2473  if arg in kwargs:
2474  strippedArgs[arg] = kwargs.pop(arg)
2475 
2476  # Setup aliases
2477  if len(args) > 1:
2478  for i in range(1, len(args)):
2479  argAlias = args[i].lstrip('-')
2480  msg.debug('Adding an alias of {0}: {1}'.format(argName, argAlias))
2481  self._argAlias[argAlias] = argName
2482 
2483  # Optinally add an argument to an argparse argument group
2484  if 'group' in strippedArgs:
2485  if strippedArgs['group'] in self._argGroups:
2486  msg.debug('Adding argument to group {0}: ({1}; {2})'.format(strippedArgs['group'], args, kwargs))
2487  self._argGroups[strippedArgs['group']].add_argument(*args, **kwargs)
2488  self._argKeyGroups[argName] = strippedArgs['group']
2489  else:
2490  msg.warning('Argument group {0} not defined - adding argument to main parser'.format(strippedArgs['group']))
2491  msg.debug('Adding argument: ({0}; {1})'.format(args, kwargs))
2492  super(trfArgParser, self).add_argument(*args, **kwargs)
2493  else:
2494  msg.debug('Adding argument: ({0}; {1})'.format(args, kwargs))
2495  super(trfArgParser, self).add_argument(*args, **kwargs)
2496 

◆ allArgs()

def python.trfArgClasses.trfArgParser.allArgs (   self)

Getter for argument list.

Definition at line 2535 of file trfArgClasses.py.

2535  def allArgs(self):
2536  return list(self._helpString)
2537 

◆ defineArgGroup()

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

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

Definition at line 2511 of file trfArgClasses.py.

2511  def defineArgGroup(self, *args):
2512  # Get an argparse group
2513  if args[0] in self._argGroups:
2514  msg.warning('Argument group %s already exists', args[0])
2515  return
2516  self._argGroups[args[0]] = self.add_argument_group(*args)
2517 

◆ 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 2528 of file trfArgClasses.py.

2528  def dumpArgs(self):
2529  keyArray = [ '--' + str(key) for key in self._helpString if key not in ('h', 'verbose', 'loglevel', 'dumpargs', 'argdict') ]
2530  keyArray.sort()
2531  print('ListOfDefaultPositionalKeys={0}'.format(keyArray))
2532 

◆ getHelpString()

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

Return the help string for a given argument.

Definition at line 2519 of file trfArgClasses.py.

2519  def getHelpString(self, argument):
2520  try:
2521  return(self._helpString[argument])
2522  except KeyError:
2523  raise trfExceptions.TransformArgException(trfExit.nameToCode('TRF_KEY_ERROR'), 'no help string available for argument %s' %argument)
2524  return None
2525 

◆ getProdsysDesc()

def python.trfArgClasses.trfArgParser.getProdsysDesc (   self)

Definition at line 2498 of file trfArgClasses.py.

2498  def getProdsysDesc(self):
2499  desc = {}
2500  for name, argClass in self._argClass.items():
2501  msg.debug('Detected the local variable {0}'.format(name))
2502  if argClass is not None:
2503  desc[name] = argClass().prodsysDescription
2504  if name in self._helpString:
2505  desc[name].update({'help': self._helpString[name]})
2506  if name in self._argKeyGroups:
2507  desc[name].update({'group':self._argKeyGroups[name]})
2508  return desc
2509 

◆ 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 2585 of file trfArgClasses.py.

2585  def parse_args(self, args = None, namespace = None):
2586  if namespace:
2587  super(trfArgParser, self).parse_args(args = args, namespace = namespace)
2588  else:
2589  namespace = super(trfArgParser, self).parse_args(args = args)
2590  for k, v in namespace.__dict__.items():
2591  msg.debug('Treating key %s (%s)', k, v)
2592  if isinstance(v, list):
2593  newValueObj, newValues = self._parse_list_helper(v)
2594  if not isinstance(newValueObj, list):
2595  newValueObj.value = newValues
2596  namespace.__dict__[k] = newValueObj
2597  msg.debug('Set to %s', newValues)
2598 
2599  return namespace
2600 
2601 

Member Data Documentation

◆ _argAlias

python.trfArgClasses.trfArgParser._argAlias
private

Definition at line 2439 of file trfArgClasses.py.

◆ _argClass

python.trfArgClasses.trfArgParser._argClass
private

Definition at line 2436 of file trfArgClasses.py.

◆ _argGroups

python.trfArgClasses.trfArgParser._argGroups
private

Definition at line 2437 of file trfArgClasses.py.

◆ _argKeyGroups

python.trfArgClasses.trfArgParser._argKeyGroups
private

Definition at line 2438 of file trfArgClasses.py.

◆ _helpString

python.trfArgClasses.trfArgParser._helpString
private

Definition at line 2435 of file trfArgClasses.py.


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