"""
utilities routines associated with globus

"""
import sys
import inspect
import traceback
from globus_sdk import GlobusAPIError
from globus_sdk import TransferAPIError
from globus_sdk import NetworkError
from globus_sdk import GlobusError
from globus_sdk import GlobusConnectionError
from globus_sdk import GlobusTimeoutError

from pandaharvester.harvestercore import core_utils
from pandalogger.PandaLogger import PandaLogger
from pandalogger.LogWrapper import LogWrapper

# handle exception from globus software
def handle_globus_exception(tmp_log):
    if not isinstance(tmp_log, LogWrapper):
        methodName = '{0} : '.format(inspect.stack()[1][3])
    else:
        methodName = ''
    # extract errtype and check if it a GlobusError Class
    errtype, errvalue = sys.exc_info()[:2]
    err_str = "{0} {1} ".format(methodName, errtype.__name__)
    if isinstance(errvalue, GlobusAPIError):
        # Error response from the REST service, check the code and message for
        # details.
        err_str += "Error Code: {0} Error Message: {1} ".format(errvalue.code, errvalue.message)
    elif isinstance(errvalue, TransferAPIError):
        err_args = list(errvalue._get_args())
        err_str += " http_status: {0} code: {1} message: {2} requestID: {3} ".format(err_args[0],
                                                                                     err_args[1],
                                                                                     err_args[2],
                                                                                     err_args[3])
    elif isinstance(errvalue, NetworkError):
        err_str += "Network Failure. Possibly a firewall or connectivity issue "
    elif isinstance(errvalue, GlobusConnectionError):
        err_str += "A connection error occured while making a REST request. "
    elif isinstance(errvalue, GlobusTimeoutError):
        err_str += "A REST request timeout. "
    elif isinstance(errvalue, GlobusError):
        err_str += "Totally unexpected GlobusError! "
    else:  # some other error
        err_str = "{0} ".format(errvalue)
    #err_str += traceback.format_exc()
    tmp_log.error(err_str)
    return err_str

@@ -791,28 +791,46 @@
 %5B:2%5D%0A    err
-_str
+Stat = None%0A    errMsg
  = %22%7B0%7D %7B1%7D 
@@ -1022,25 +1022,69 @@
      err
-_str += %22
+Stat = None%0A        errMsg += %22HTTP status code: %7B0%7D 
 Error Co
@@ -1084,25 +1084,25 @@
 rror Code: %7B
-0
+1
 %7D Error Mess
@@ -1107,17 +1107,17 @@
 ssage: %7B
-1
+2
 %7D %22.form
@@ -1132,13 +1132,208 @@
 lue.
-code,
+http_status,%0A                                                                                      errvalue.code, %0A                                                                                     
  err
@@ -1449,28 +1449,54 @@
 %0A        err
-_str
+Stat = None    %0A        errMsg
  += %22 http_s
@@ -1912,28 +1912,54 @@
 %0A        err
-_str
+Stat = None    %0A        errMsg
  += %22Network
@@ -2073,28 +2073,54 @@
 %0A        err
-_str
+Stat = None    %0A        errMsg
  += %22A conne
@@ -2228,28 +2228,54 @@
 %0A        err
-_str
+Stat = None    %0A        errMsg
  += %22A REST 
@@ -2348,20 +2348,43 @@
      err
-_str
+Stat = False%0A        errMsg
  += %22Tot
@@ -2455,20 +2455,43 @@
      err
-_str
+Stat = False%0A        errMsg
  = %22%7B0%7D 
@@ -2517,20 +2517,19 @@
     #err
-_str
+Msg
  += trac
@@ -2568,20 +2568,19 @@
 rror(err
-_str
+Msg
 )%0A    re
@@ -2588,12 +2588,21 @@
 urn 
+(
 err
-_str
+Stat,errMsg)
 %0A

return status and error message for Globus errors