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 2430 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 2437 of file trfArgClasses.py.

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

Member Function Documentation

◆ _parse_list_helper()

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

Definition at line 2542 of file trfArgClasses.py.

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

◆ add_argument()

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

Definition at line 2445 of file trfArgClasses.py.

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

◆ allArgs()

def python.trfArgClasses.trfArgParser.allArgs (   self)

Getter for argument list.

Definition at line 2538 of file trfArgClasses.py.

2538  def allArgs(self):
2539  return list(self._helpString)
2540 

◆ defineArgGroup()

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

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

Definition at line 2514 of file trfArgClasses.py.

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

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

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

◆ getHelpString()

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

Return the help string for a given argument.

Definition at line 2522 of file trfArgClasses.py.

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

◆ getProdsysDesc()

def python.trfArgClasses.trfArgParser.getProdsysDesc (   self)

Definition at line 2501 of file trfArgClasses.py.

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

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

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

Member Data Documentation

◆ _argAlias

python.trfArgClasses.trfArgParser._argAlias
private

Definition at line 2442 of file trfArgClasses.py.

◆ _argClass

python.trfArgClasses.trfArgParser._argClass
private

Definition at line 2439 of file trfArgClasses.py.

◆ _argGroups

python.trfArgClasses.trfArgParser._argGroups
private

Definition at line 2440 of file trfArgClasses.py.

◆ _argKeyGroups

python.trfArgClasses.trfArgParser._argKeyGroups
private

Definition at line 2441 of file trfArgClasses.py.

◆ _helpString

python.trfArgClasses.trfArgParser._helpString
private

Definition at line 2438 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
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
dqt_zlumi_pandas.update
update
Definition: dqt_zlumi_pandas.py:42
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:2627
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
confTool.parse_args
def parse_args()
Definition: confTool.py:35
str
Definition: BTagTrackIpAccessor.cxx:11