GlobusStatus = {
    '0' : 'unknown',
    '1' : 'pending',
    '2' : 'active',
    '4' : 'failed',
    '8' : 'done',
    '16' : 'suspended',
    '32' : 'unsubmitted',
    '64' : 'stagein',
    '128' : 'stageout',
    }

ErrCodes = {
    0 : { 'desc' : 'Successful completion', 'level' : 'finished', },
    7 : { 'desc' : 'Authentication with remote host failed', 'level' : 'failed', },
    17 : { 'desc' : 'The job failed when the job manager attempted to run it', 'level' : 'failed', },
    122 : { 'desc' : 'Could not read the job state file', 'level' : 'failed', },
    123 : { 'desc' : 'Could not write the job state file', 'level' : 'failed', },
    124 : { 'desc' : 'Old job manager is still alive', 'level' : 'failed', },
    126 : { 'desc' : 'Unknown whether job was submitted', 'level' : 'warn', },
    129 : { 'desc' : 'The standard output/error size is different', 'level' : 'info', },
    43 : { 'desc' : 'Error staging executable', 'level' : 'failed', },
    1001 : { 'desc' : 'No submission job ID - submission failed?', 'level' : 'failed', },
    1002 : { 'desc' : 'Error sending inputs', 'level' : 'failed', },
    1003 : { 'desc' : 'Job was aborted', 'level' : 'aborted', },
    1004 : { 'desc' : 'Unspecified gridmanager error', 'level' : 'failed', },
    1005 : { 'desc' : 'Proxy expired', 'level' : 'failed', },
    1006 : { 'desc' : 'Grid resource is down', 'level' : 'warn', },
    1007 : { 'desc' : 'Error spooling job', 'level' : 'failed', },    # use ficticious error code 
    1008 : { 'desc' : 'Jobmanager failed to cancel job', 'level' : 'warn', },
#    1009 : { 'desc' : 'PeriodicRemove timeout', 'level' : 'failed', },
    1010 : { 'desc' : 'Job was evicted', 'level' : 'failed', },
    1011 : { 'desc' : 'Failed to set Owner', 'level' : 'failed', },  # 1011 implies 1012 
    1012 : { 'desc' : 'Failed to queue job', 'level' : 'failed', },
    1013 : { 'desc' : 'Failed to obtain job script', 'level' : 'failed', },
    1014 : { 'desc' : 'Job was evicted', 'level' : 'failed', },
    1015 : { 'desc' : 'Grid resource back up', 'level' : 'info', },
    1016 : { 'desc' : 'Globus data transfer failed', 'level' : 'failed', },
    1017 : { 'desc' : 'condor_rm by root', 'level' : 'aborted', },
    1018 : { 'desc' : 'condor_rm by sm', 'level' : 'aborted', },
    1019 : { 'desc' : 'Job failed, no reason given by GRAM server', 'level' : 'failed', },
    1020 : { 'desc' : 'Globus error 9: the system cancelled the job', 'level' : 'failed', },
    }

ErrPatterns = [
    { 'file' : 'pilot.log',
      'pat' : '.*Normal termination',
      'errcode' : 0,
      },
    { 'file' : 'pilot.log',
      'pat' : '.*old job manager is still alive',
      'errcode' : 124,
      },
    { 'file' : 'pilot.log',
      'pat' : '.*STARTER failed to receive file(s)',
      'errcode' : 1002,
      },
    { 'file' : 'pilot.log',
      'pat' : '.*Job was aborted',
      'errcode' : 1003,
      },
    { 'file' : 'pilot.log',
      'pat' : '.*Unspecified gridmanager error',
      'errcode' : 1004,
      },
    { 'file' : 'submit.out',
      'pat' : '.*proxy has expired',
      'errcode' : 1005,
      },
    { 'file' : 'pilot.log',
      'pat' : '.*Detected Down',
      'errcode' : 1006,
      },
    { 'file' : 'submit.out', 
      'pat' : '.*Failed to spool job files',
      'errcode': 1007,   
      }, 
    { 'file' : 'pilot.log',
      'pat' : '.*job manager failed to cancel the job',
      'errcode' : 1008,
      },
#    { 'file' : 'pilot.log',
#      'pat' : '.*job attribute PeriodicRemove',
#      'errcode' : 1009,
#      },
    { 'file' : 'pilot.log',
      'pat' : '.*Job was evicted',
      'errcode' : 1010,
      },
    { 'file' : 'submit.out',
      'pat' : '.*Failed to set Owner',
      'errcode' : 1011,
      },
    { 'file' : 'submit.out',
      'pat' : '.*Failed to queue job',
      'errcode' : 1012,
      },
    { 'file' : 'submit.out',
      'pat' : '.*Failed to obtain job script',
      'errcode' : 1013,
      },
    { 'file' : 'submit.out',
      'pat' : '.*Job was evicted',
      'errcode' : 1014,
      },
    { 'file' : 'pilot.log',
      'pat' : '.*Back Up',
      'errcode' : 1015,
      },
    { 'file' : 'pilot.log',
      'pat' : '.*data transfer to the server failed',
      'errcode' : 1016,
      },
    { 'file' : 'pilot.log',
      'pat' : '.*via condor_rm.*by user root',
      'errcode' : 1017,
      },
    { 'file' : 'pilot.log',
      'pat' : '.*via condor_rm.*by user sm',
      'errcode' : 1018,
      },
    { 'file' : 'pilot.log',
      'pat' : '.*Job failed, no reason given by GRAM server',
      'errcode' : 1019,
      },
    { 'file' : 'pilot.log',
      'pat' : '.*Globus error 9.* the system cancelled the job',
      'errcode' : 1020,
      },
    ]

def setError(code):
    return code, ErrCodes[code]['desc'], ErrCodes[code]['level']