Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 2433 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 2440 of file trfArgClasses.py.

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

Member Function Documentation

◆ _parse_list_helper()

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

Definition at line 2545 of file trfArgClasses.py.

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

◆ add_argument()

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

Definition at line 2448 of file trfArgClasses.py.

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

◆ allArgs()

def python.trfArgClasses.trfArgParser.allArgs (   self)

Getter for argument list.

Definition at line 2541 of file trfArgClasses.py.

2541  def allArgs(self):
2542  return list(self._helpString)
2543 

◆ defineArgGroup()

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

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

Definition at line 2517 of file trfArgClasses.py.

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

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

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

◆ getHelpString()

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

Return the help string for a given argument.

Definition at line 2525 of file trfArgClasses.py.

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

◆ getProdsysDesc()

def python.trfArgClasses.trfArgParser.getProdsysDesc (   self)

Definition at line 2504 of file trfArgClasses.py.

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

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

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

Member Data Documentation

◆ _argAlias

python.trfArgClasses.trfArgParser._argAlias
private

Definition at line 2445 of file trfArgClasses.py.

◆ _argClass

python.trfArgClasses.trfArgParser._argClass
private

Definition at line 2442 of file trfArgClasses.py.

◆ _argGroups

python.trfArgClasses.trfArgParser._argGroups
private

Definition at line 2443 of file trfArgClasses.py.

◆ _argKeyGroups

python.trfArgClasses.trfArgParser._argKeyGroups
private

Definition at line 2444 of file trfArgClasses.py.

◆ _helpString

python.trfArgClasses.trfArgParser._helpString
private

Definition at line 2441 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: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: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:2630
confTool.parse_args
def parse_args()
Definition: confTool.py:36
str
Definition: BTagTrackIpAccessor.cxx:11