# Path: pUtil.py
# def httpConnect(data, url, mode="UPDATE", sendproxy=False, path=None, experiment=""):
#     """ function to handle the http connection """
# 
#     # check if a job should be downloaded or if it's a server update
#     if mode == "GETJOB":
#         cmd = 'getJob'
#     elif mode == "ISTHEREANALYSISJOB":
#         cmd = 'isThereAnalysisJob'
#     elif mode == "GETSTATUS":
#         cmd = 'getStatus'
#     elif mode == "GETEVENTRANGES":
#         cmd = 'getEventRanges'
#     elif mode == "UPDATEEVENTRANGE":
#         cmd = 'updateEventRange'
#     elif mode == "UPDATEEVENTRANGES":
#         cmd = 'updateEventRanges'
#     elif mode == "GETKEYPAIR":
#         cmd = 'getKeyPair'
#     else:
#         cmd = 'updateJob'
# 
#     # only send attemptNr with updateJob
#     if cmd != 'updateJob' and data.has_key('attemptNr'):
#         tolog("Removing attemptNr from node structure since it is not needed for command %s" % (cmd))
#         del data['attemptNr']
#     else:
#         if data.has_key('attemptNr'):
#             tolog("Sending attemptNr=%d for cmd=%s" % (data['attemptNr'], cmd))
#         else:
#             tolog("Will not send attemptNr for cmd=%s" % (cmd))
# 
#     # send the data dictionary to the dispatcher using command cmd
#     # return format: status, parsed data, response
#     return toServer(url, cmd, data, path, experiment)
# 
# def tolog(msg, tofile=True, label='INFO', essential=False):
#     """ Write date+msg to pilot log and to stdout """
# 
#     try:
#         import inspect
# 
#         MAXLENGTH = 12
#         # getting the name of the module that is invoking tolog() and adjust the length
#         try:
#             module_name = os.path.basename(inspect.stack()[1][1])
#         except Exception, e:
#             module_name = "unknown"
#             #print "Exception caught by tolog(): ", e,
#         module_name_cut = module_name[0:MAXLENGTH].ljust(MAXLENGTH)
#         msg = "%i|%s| %s" % (os.getpid(),module_name_cut, msg)
# 
#         t = timeStampUTC(format='%Y-%m-%d %H:%M:%S')
#         if tofile:
#             appendToLog("%s|%s\n" % (t, msg))
# 
#         # remove backquotes from the msg since they cause problems with batch submission of pilot
#         # (might be present in error messages from the OS)
#         msg = msg.replace("`","'")
#         msg = msg.replace('"','\\"')
#         print "%s| %s" % (t, msg)
# 
#         # write any FAILED messages to stderr
#         if "!!FAILED!!" in msg:
#             try:
#                 print >> sys.stderr, "%s| %s" % (t, msg)
#             except:
#                 print "Failed to print to sys.stderr: %s" % (t, msg)
#     except:
#         print "!!WARNING!!4000!! %s" % traceback.format_exc()


import json
import os
import cgi
    import requests
from pUtil import httpConnect, tolog
from Configuration import Configuration
    return pandaServerURL


def downloadEventRangesPandaProxy(jobId, jobsetId, pandaProxySecretKey):
    """ Download event ranges from the Panda proxy """

    tolog("pp: start to download new event ranges for jobId=%s, jobsetId=%s" % (jobId, jobsetId))

            
    data = {
        'pandaID' : jobId, 
        'jobsetID':jobsetId,
        'secretKey':pandaProxySecretKey,
        'baseURL':envPandaServerURL()
        }
    postURL = envPandaProxyURL()+'/getEventRanges'
    tolog( "pp: POST url: '%s', request data=%s" % (postURL,data))
    res = requests.post(postURL, data)
    if res.status_code != 200:
        tolog('pp: downloadEventRangesPandaProxy request failed, ret status: %s' % res.status_code )
        message = "Failed to download event range - error code = %d" % (res.status_code)
    else:
        tolog("pp: res=%s" % res)
        tolog("pp: res.text.encode('ascii') = %s" % res.text.encode('ascii'))
        tmpDict =  cgi.parse_qs(res.text.encode('ascii'))
        tolog("pp: tmpDict = %s" % tmpDict)
        #resCgiParsed = {'eventRanges': ['[{\"eventRangeID\": \"10204500-3123350504-7783643170-1-10\", \"LFN\": \"EVNT.01416937._000001.pool.root.1\", \"lastEvent\": 1, \"startEvent\": 1, \"scope\": \"valid1\", \"GUID\": \"1141217E-433C-3E47-8898-615B968DA5E5\"}, {\"eventRangeID\": \"10204500-3123350504-7783643170-2-10\", \"LFN\": \"EVNT.01416937._000001.pool.root.1\", \"lastEvent\": 2, \"startEvent\": 2, \"scope\": \"valid1\", \"GUID\": \"1141217E-433C-3E47-8898-615B968DA5E5\"}, {\"eventRangeID\": \"10204500-3123350504-7783643170-3-10\", \"LFN\": \"EVNT.01416937._000001.pool.root.1\", \"lastEvent\": 3, \"startEvent\": 3, \"scope\": \"valid1\", \"GUID\": \"1141217E-433C-3E47-8898-615B968DA5E5\"}, {\"eventRangeID\": \"10204500-3123350504-7783643170-4-10\", \"LFN\": \"EVNT.01416937._000001.pool.root.1\", \"lastEvent\": 4, \"startEvent\": 4, \"scope\": \"valid1\", \"GUID\": \"1141217E-433C-3E47-8898-615B968DA5E5\"}]'], 'StatusCode': ['0']}
        retStatus = int(tmpDict['StatusCode'][0])
        if  retStatus== 0:
            message = tmpDict['eventRanges'][0]

# Path: pUtil.py
# def httpConnect(data, url, mode="UPDATE", sendproxy=False, path=None, experiment=""):
#     """ function to handle the http connection """
# 
#     # check if a job should be downloaded or if it's a server update
#     if mode == "GETJOB":
#         cmd = 'getJob'
#     elif mode == "ISTHEREANALYSISJOB":
#         cmd = 'isThereAnalysisJob'
#     elif mode == "GETSTATUS":
#         cmd = 'getStatus'
#     elif mode == "GETEVENTRANGES":
#         cmd = 'getEventRanges'
#     elif mode == "UPDATEEVENTRANGE":
#         cmd = 'updateEventRange'
#     elif mode == "UPDATEEVENTRANGES":
#         cmd = 'updateEventRanges'
#     elif mode == "GETKEYPAIR":
#         cmd = 'getKeyPair'
#     else:
#         cmd = 'updateJob'
# 
#     # only send attemptNr with updateJob
#     if cmd != 'updateJob' and data.has_key('attemptNr'):
#         tolog("Removing attemptNr from node structure since it is not needed for command %s" % (cmd))
#         del data['attemptNr']
#     else:
#         if data.has_key('attemptNr'):
#             tolog("Sending attemptNr=%d for cmd=%s" % (data['attemptNr'], cmd))
#         else:
#             tolog("Will not send attemptNr for cmd=%s" % (cmd))
# 
#     # send the data dictionary to the dispatcher using command cmd
#     # return format: status, parsed data, response
#     return toServer(url, cmd, data, path, experiment)
# 
# def tolog(msg, tofile=True, label='INFO', essential=False):
#     """ Write date+msg to pilot log and to stdout """
# 
#     try:
#         import inspect
# 
#         MAXLENGTH = 12
#         # getting the name of the module that is invoking tolog() and adjust the length
#         try:
#             module_name = os.path.basename(inspect.stack()[1][1])
#         except Exception, e:
#             module_name = "unknown"
#             #print "Exception caught by tolog(): ", e,
#         module_name_cut = module_name[0:MAXLENGTH].ljust(MAXLENGTH)
#         msg = "%i|%s| %s" % (os.getpid(),module_name_cut, msg)
# 
#         t = timeStampUTC(format='%Y-%m-%d %H:%M:%S')
#         if tofile:
#             appendToLog("%s|%s\n" % (t, msg))
# 
#         # remove backquotes from the msg since they cause problems with batch submission of pilot
#         # (might be present in error messages from the OS)
#         msg = msg.replace("`","'")
#         msg = msg.replace('"','\\"')
#         print "%s| %s" % (t, msg)
# 
#         # write any FAILED messages to stderr
#         if "!!FAILED!!" in msg:
#             try:
#                 print >> sys.stderr, "%s| %s" % (t, msg)
#             except:
#                 print "Failed to print to sys.stderr: %s" % (t, msg)
#     except:
#         print "!!WARNING!!4000!! %s" % traceback.format_exc()


# Path: EventRangesPandaProxy.py
import json
import os
import cgi
    import requests
from pUtil import httpConnect, tolog
from Configuration import Configuration

    return pandaServerURL


def downloadEventRangesPandaProxy(jobId, jobsetId, pandaProxySecretKey):
    """ Download event ranges from the Panda proxy """

    tolog("pp: start to download new event ranges for jobId=%s, jobsetId=%s" % (jobId, jobsetId))

            
    data = {
        'pandaID' : jobId, 
        'jobsetID':jobsetId,
        'secretKey':pandaProxySecretKey,
        'baseURL':envPandaServerURL()
        }
    postURL = envPandaProxyURL()+'/getEventRanges'
    tolog( "pp: POST url: '%s', request data=%s" % (postURL,data))
    res = requests.post(postURL, data)
    if res.status_code != 200:
        tolog('pp: downloadEventRangesPandaProxy request failed, ret status: %s' % res.status_code )
        message = "Failed to download event range - error code = %d" % (res.status_code)
    else:
        tolog("pp: res=%s" % res)
        tolog("pp: res.text.encode('ascii') = %s" % res.text.encode('ascii'))
        tmpDict =  cgi.parse_qs(res.text.encode('ascii'))
        tolog("pp: tmpDict = %s" % tmpDict)
        #resCgiParsed = {'eventRanges': ['[{\"eventRangeID\": \"10204500-3123350504-7783643170-1-10\", \"LFN\": \"EVNT.01416937._000001.pool.root.1\", \"lastEvent\": 1, \"startEvent\": 1, \"scope\": \"valid1\", \"GUID\": \"1141217E-433C-3E47-8898-615B968DA5E5\"}, {\"eventRangeID\": \"10204500-3123350504-7783643170-2-10\", \"LFN\": \"EVNT.01416937._000001.pool.root.1\", \"lastEvent\": 2, \"startEvent\": 2, \"scope\": \"valid1\", \"GUID\": \"1141217E-433C-3E47-8898-615B968DA5E5\"}, {\"eventRangeID\": \"10204500-3123350504-7783643170-3-10\", \"LFN\": \"EVNT.01416937._000001.pool.root.1\", \"lastEvent\": 3, \"startEvent\": 3, \"scope\": \"valid1\", \"GUID\": \"1141217E-433C-3E47-8898-615B968DA5E5\"}, {\"eventRangeID\": \"10204500-3123350504-7783643170-4-10\", \"LFN\": \"EVNT.01416937._000001.pool.root.1\", \"lastEvent\": 4, \"startEvent\": 4, \"scope\": \"valid1\", \"GUID\": \"1141217E-433C-3E47-8898-615B968DA5E5\"}]'], 'StatusCode': ['0']}
        retStatus = int(tmpDict['StatusCode'][0])
        if  retStatus== 0:
            message = tmpDict['eventRanges'][0]

            tolog('pp: downloadEventRangesPandaProxy ranges: %s' % message)