Beispiel #1
0
 def _checkType(self, variableName, value):
     """Allowed types:
     For option not taking argument: bool (True,False) or string (\'True\' or \'False\')
     For option taking argument: string or bool
     """
     valType = type(value).__name__
     if not self.__takesArgument:
         # convert certain strings to bool
         if valType == 'str':
             # empty string is given by getopt (when option is set)
             # convert to boolean if possible.
             try:
                 value = {
                     'TRUE': True,
                     'FALSE': False,
                     '': True
                 }[value.upper()]
             except:
                 raise TransformArgumentError(
                     '%s value of %s not recognised.' %
                     (variableName, value))
         elif not isinstance(value, bool):
             raise TransformArgumentError(
                 '%s should be boolean. Got %s (%s) instead.' %
                 (variableName, value, valType))
     elif valType not in ('str', 'bool'):
         raise TransformArgumentError(
             '%s should be a string or bool. Got %s (%s) instead.' %
             (variableName, value, valType))
     return value
Beispiel #2
0
 def _checkAllowedValues(self,variableName,allowedValues):
     # Check all allowed values
     if not self.__takesArgument:
         for val in allowedValues:
             if val is not True and val is not False:
                 raise TransformArgumentError( '%s should be True or False. Got %s (%s) instead.' % (variableName, val, type(val).__name__) )
     else:
         for val in allowedValues:
             valType = type(val).__name__
             if valType not in [ 'str', 'bool' ]:
                 raise TransformArgumentError( '%s should be a string or boolean. Got %s (%s) instead.' % (variableName, val, valType) )
     return allowedValues
Beispiel #3
0
 def _checkValue(self,variableName,value):
     if self.__takesArgument:
         # handle the argument (always of the string type)
         choices = self.allowedValues()
         try:
             valueList = value.split( ',' )
         except Exception:
             valueList = [ value ]
         # Convert value to the correct case, if there is a list of string choices
         if choices:
             newValueList = []
             for v in valueList:
                 for c in choices:
                     try:
                         x = v.upper() == c.upper()
                     except AttributeError:
                         if v == c:
                             newValueList.append( v )
                             break
                     else:
                         if x:
                             if v != c:
                                 v = c
                             newValueList.append( v )
                             break
                 else:
                     raise TransformArgumentError( '%s value %r is not in %s' % ( variableName, v, choices ) )
             try: # string list
                 return ','.join( newValueList )
             except Exception: # assume boolean
                 return newValueList[0]
     # check against list of possible values
     return Descriptor._checkValue(self,variableName,value)
Beispiel #4
0
 def toPython(self,valIn):
     from PyJobTransformsCore.trfutil import StringToExec
     try:
         valOut=StringToExec(valIn)
         return valOut
     except Exception:
         raise TransformArgumentError( '%s=%s: syntax error in BasicExec' % (self.name(),valIn) )
     return None
Beispiel #5
0
 def toPython(self,valIn):
     # if already a list, nothing to be done
     if isinstance(valIn,list):
         return valIn
     # make a list of python types out of the string separated either by , or ,, 
     else:
         try:
             valTmp=valIn.replace(',,',',')
             valList=valTmp.split(',')
         except Exception:
             raise TransformArgumentError( '%s=%s: syntax error in list of strings' % (self.name(),valIn) )
         return valList
Beispiel #6
0
from AthenaCommon.AthenaCommonFlags import athenaCommonFlags
if hasattr(runArgs, "skipEvents"):
    athenaCommonFlags.SkipEvents.set_Value_and_Lock(runArgs.skipEvents)
else:
    athenaCommonFlags.SkipEvents = 0
if hasattr(runArgs, "maxEvents"):
    athenaCommonFlags.EvtMax.set_Value_and_Lock(runArgs.maxEvents)
else:
    athenaCommonFlags.EvtMax = -1

## TreeName
from NTUPtoNTUPCore.SkimNTUP_ProdFlags import prodFlags
if hasattr(runArgs, "tree_name"):
    prodFlags.TreeName.set_Value_and_Lock(runArgs.tree_name)
else:
    raise TransformArgumentError(message='tree_name is not defined!')

## Input
from PATJobTransforms.Configuration import ConfigDic
from PyJobTransformsCore.trferr import TransformArgumentError
from PyJobTransformsCore.full_trfarg import InputNtupleFileArg
inFileArgs = 0
for arg in dir(runArgs):
    if arg.startswith('inputNTUP') and arg.endswith('File'):
        inFile = getattr(runArgs, arg)
        athenaCommonFlags.FilesInput.set_Value_and_Lock(inFile)
        inFileArgs += 1
        print "Using argument ", arg, " = ", inFile, ", tree name = ", prodFlags.TreeName(
        )
if inFileArgs != 1:
    raise TransformArgumentError(
    if key.startswith('outputNTUP') and key.endswith('File') and hasattr(
            runArgs, key):
        outFileArgs += 1
        outKey = key
        outFile = getattr(runArgs, key)
        print "Using argument ", key, " = ", outFile
    if key.startswith('inputNTUP') and key.endswith('File') and hasattr(
            runArgs, key):
        inFileArgs += 1
        inKey = key
        inFile = getattr(runArgs, key)
        print "Using argument ", key, " = ", inFile

if outFileArgs != 1:
    raise TransformArgumentError(
        message=
        'Wrong number of outputNTUPXXXFile arguments: {0:d} instead of 1. Stopping!'
        .format(outFileArgs))

if inFileArgs != 1:
    raise TransformArgumentError(
        message=
        'Wrong number of inputNTUPXXXFile arguments: {0:d} instead of 1. Stopping!'
        .format(inFileArgs))

if inKey.lstrip('input') != outKey.lstrip('output'):
    raise TransformArgumentError(
        message=
        'Using different input and output types: {0:s} and {0:s}. Stopping!'.
        format(inKey, outKey))

if not (hasattr(runArgs, "sortInputFiles") and not runArgs.sortInputFiles):
Beispiel #8
0
    def extractOptions(self, argList):
        """Scan the command line argument list given in <argList>. It returns a new
        list where all the options have been removed from <argList>.
        It considers an argument a valid option if it is:
        -<shortname>
        -<shortname> <value>
        --<longname>
        --<longname>=<value>
        <longname>=<value>
        Any argument starting with '-' or '--' will be considered an option, and
        will be checked against the list of known options. If no match is found,
        it is considered an error unless the argument is a negative number
        (integer or float), in which case it will be included in the returned list.
        If an argument equals '--', then all following arguments are considered not options,
        and will be included in the returned list.
        If there is any problems with the options, an TransformArgumentError exception is raised.
        """
        cleanList = []
        nArgs = len(argList)
        iArg = 0
        while iArg < nArgs:
            arg = argList[iArg]
            if arg == '--':
                # no more options. Add all remaining arguments
                iArg += 1
                while iArg < nArgs:
                    cleanList.append( argList[iArg] )
                    iArg += 1
                return cleanList
            # get argument name
            equal=arg.find('=')
            value = None
            if equal == -1:
                name = arg
            else:
                name = arg[:equal]
                if equal+1 < len(arg):
                    value=arg[equal+1:]
                    # convert to boolean if possible.
                    try:
                        value = { 'TRUE' : True, 'FALSE' : False }[ value.upper() ]
                    except Exception:
                        pass
                else:
                    value=''
            if name.startswith('--'):
                # cases --<longname> and --<longname>=<value>
                longName = name[2:]
                if not self.isLongOption(longName):
                    raise TransformArgumentError('option %s not supported' % name)
                option = self.getProperty(longName) # gets the option descriptor
                if option.takesArgument():
                    if equal == -1:
                        value = option.default()
#                        raise TransformArgumentError('option %s value missing. Syntax: %s=<value>' % (name,name) )
                    # set the value
                    setattr(self,longName,value)
                else:
                    if equal != -1:
                        raise TransformArgumentError('option %s does not take a value (Found =%s)' % (name,value) )
                    # set value to True
                    setattr(self,longName,True)
            elif name.startswith('-'):
                # cases -<shortname>, -<shortname> <value> or a negative number
                if not self.isShortOption(name):
                    try:
                        # test if it is a negative number
                        float(name)  # covers both float and int
                    except Exception:
                        raise TransformArgumentError('option %s not supported' % name)
                    else:
                        # it is a negative number: add it to the list, and go to next
                        cleanList.append( arg )
                        iArg += 1
                        continue
                longName = self.toLongName(name)
                option = self.getProperty(longName) # gets the option descriptor
                if option.takesArgument():
                    if equal != -1:
                        raise TransformArgumentError('option %s syntax error: no \'=\' allowed. Value should be specified as next argument: %s <value>' % (name,name) )
                    # value is next argument
                    iArg += 1
                    if iArg >= nArgs:
                        raise TransformArgumentError('option %s missing value. Syntax: %s <value>' % (name,name) )
                    value = argList[iArg]
                    # convert to boolean if possible.
                    try:
                        value = { 'TRUE' : True, 'FALSE' : False }[ value.upper() ]
                    except Exception:
                        pass
                    setattr(self,longName,value)
                else:
                    if equal != -1:
                        raise TransformArgumentError('option %s syntax error: no \'=\' allowed in name (option takes no value.)' % (name,) )
                    # set value to True
                    setattr(self,longName,True)                    
            elif equal != -1 and self.isLongOption(name):
                option = self.getProperty(name)
                if not option.takesArgument():
                    raise TransformArgumentError('option %s takes no value' % (name,))
                # case <longname>=<value>
                setattr(self,name,value)
            else:
                # not an option
                cleanList.append( arg )
            # move to next argument    
            iArg += 1
            # end of while iArg < nArgs
        return cleanList