def jsonformatter(arguments):
    # Read the string from the file
    fo = open("json_file", "r")
    strglist = fo.readlines()
    fo.close()
    # Empty the file
    open("json_file", 'w').close()
    # Format the string
    # Each element should of the form:
    #
    path = []
    endpoint = []
    strglistlength = len(strglist)
    elementnumber = 0
    for strg in strglist:
        elementnumber = elementnumber + 1
        if arguments.verbose:
            if elementnumber % 25:
                print "Element " + str(elementnumber) + " of " + str(
                    strglistlength)
        lista = re.split(r'\s* \s*', strg.rstrip())
        if lista[1] == "None":
            print "An argument(pid, url...) does not exist. Continuing anyway!"
            continue
        sublista = re.split(r'\s*\:\s*', lista[1])
        if arguments.verbose: print "lista: ", lista
        #print "sublista : ", sublista
        url = re.split("//", sublista[1])[1]
        real_path = re.split("^\d\d\d\d", sublista[2])[1].rstrip()
        #print "url : ", url
        #print "real_path : ", prepath
        path.append(real_path)
        #print path
        #print arguments.user
        urlendpoint = datamover.defineurlendpoint(str(arguments.user))
        try:
            endpoint.append(urlendpoint[url])
        except:
            print "The server " + url + " is not mapped to a GO enpoint in datastagerconfig"
            full_exit("Done!")
        #print url
        #print path
        #print endpoint
# Write path to the file
    json_results = json.dumps(path)
    fo = open("json_file", "w")
    fo.write(json_results)
    fo.close()
    #print json_results
    return endpoint
def jsonformatter(arguments):
# Read the string from the file 
    fo = open("json_file", "r")
    strglist = fo.readlines();
    fo.close()
# Empty the file    
    open("json_file", 'w').close()
# Format the string    
# Each element should of the form:
# 
    path=[]
    endpoint=[]
    strglistlength=len(strglist)
    elementnumber=0
    for strg in strglist:
        elementnumber=elementnumber+1
        if arguments.verbose: 
            if elementnumber%25: 
                print "Element "+str(elementnumber)+" of "+str(strglistlength)
        lista = re.split(r'\s* \s*', strg.rstrip())
        if lista[1] == "None":
            print "An argument(pid, url...) does not exist. Continuing anyway!" 
            continue
        sublista = re.split(r'\s*\:\s*', lista[1])
        if arguments.verbose: print "lista: ", lista
        #print "sublista : ", sublista
        url=re.split("//",sublista[1])[1]
        real_path=re.split("^\d\d\d\d",sublista[2])[1].rstrip()
        #print "url : ", url
        #print "real_path : ", prepath
        path.append(real_path)
        #print path
        #print arguments.user
        urlendpoint = datamover.defineurlendpoint(str(arguments.user))
        try:
            endpoint.append(urlendpoint[url])
        except:
            print "The server "+url+" is not mapped to a GO enpoint in datastagerconfig" 
            full_exit("Done!")
        #print url
        #print path
        #print endpoint
# Write path to the file    
    json_results=json.dumps(path)
    fo = open("json_file", "w")
    fo.write(json_results);
    fo.close()
    #print json_results
    return endpoint
def argument_parser(arguments):
# Cancel the transfer
    global stop
    if arguments.action=="cancel":
        try: 
            if arguments.taskid: pass
        except: full_exit("You did not provide the taskid!")
        if str(arguments.taskid)=="None": full_exit("You did not provide the taskid!")
        print "The transfer activity corresponding to task %s is going to be cancelled." % arguments.taskid
        api = None
        stop = True
        time.sleep(0.2)
        inurllist, outurllist, destendpoint = datamover.canceltask(str(arguments.user), str(arguments.taskid))
# Details of the transfer
    if arguments.action=="details":
        try: 
            if arguments.taskid: pass
        except: full_exit("You did not provide the taskid!")
        if str(arguments.taskid)=="None": full_exit("You did not provide the taskid!")
        print "The transfer activity corresponding to task %s follows." % arguments.taskid 
        api = None
        stop = True
        time.sleep(0.2)
        #urlendpoint = datamover.defineurlendpoint(str(arguments.user))
        #print urlendpoint
        datamover.detailsoftask(str(arguments.user), str(arguments.taskid))
# Stage out
    if arguments.direction=="out":
        if arguments.sub_action == "irods":
            if arguments.path and arguments.pathfile:
                full_exit("Only one between -p and -pF is allowed!")
            irodssource(arguments)
            if arguments.verbose: print "Source end-point: "+arguments.src_site
        elif arguments.sub_action == "url":
            if arguments.url and arguments.urlfile:
                print "Only one between -U and -UF is allowed!"
            if arguments.rmode == "DSSfile":
                if arguments.verbose: print "Using .DSSfile"
                arguments.src_site=DSSfile_urlsource(arguments)
            elif arguments.rmode == "icommands":
                arguments.src_site=urlsource(arguments)
            if arguments.verbose: print "Source end-point: "+arguments.src_site
        elif arguments.sub_action == "pid":
            if arguments.pid and arguments.pidfile:
                print "Only one between -P and -PF is allowed!"
                full_exit("Done!")
            if not arguments.rmode:
                print "The rmode (-m) is mandatory!"
                sys.exit(1)
            if arguments.rmode == "DSSfile":
                if arguments.verbose: print "Using .DSSfile"
                stop = True
                getDSSfile(arguments,arguments.dssfiledefserver,[arguments.dssfiledefserver])
                arguments.src_site=DSSfile_pidsource(arguments)
            elif arguments.rmode == "icommands":
                if arguments.verbose: print "Using icommands"
                arguments.src_site=pidsource(arguments)
                if arguments.verbose: print "Source end-point: "+arguments.src_site
            else:
                print "You are staging out so you can only specify iRODS or PID or URL!"
                full_exit("Done!")
# Stage in 
    if arguments.direction == "in":
        if arguments.action == "issue":
            if arguments.path:
                if arguments.pathfile:
                    print "Only one between -p and -pF is allowed!"
                    full_exit("Done!")
                print "You are staging in so save the taskID in order to know the PID(s)."
                file_list=[]
                file_list.append(arguments.src_dir+"/"+arguments.path)
                json_results=json.dumps(file_list)
                fo = open("json_file", "w")
                fo.write(json_results);
                fo.close()
            elif arguments.pathfile:
                strglist=[]
                fo = open(arguments.pathfile, "r")
                strglist = fo.readlines();
                fo.close()
                file_list=[]
                for filename in strglist:
                    file_list.append(arguments.src_dir+"/"+filename.rstrip())
                json_results=json.dumps(file_list)
                fo = open("json_file", "w")
                fo.write(json_results);
                fo.close()
            else:
                full_exit("One between -p and -pF is mandatory.")
        elif arguments.action == "pid":
            if not arguments.rmode:
                full_exit("The rmode (-m) is mandatory!")
            if arguments.taskid:
                api = None
                inurllist, outurllist, destendpoint = datamover.lookforurl(
                        str(arguments.user), 
                        str(arguments.taskid))
                #print inurllist
                #print outurllist
                #print destendpoint
                if not all_same(destendpoint):
                    print "All the pid should be mapped to the same GO endpoint."
                    full_exit("Done!")
                urlendpoint = datamover.defineurlendpoint(str(arguments.user))
                #print urlendpoint
                for url, ep in urlendpoint.items():
                    #print url,ep,arguments.user+"#"+destendpoint[0]
                    if ep == arguments.user+"#"+destendpoint[0]:
                        endpoint=url
                if endpoint=="":
                    full_exit("The server "
                              +destendpoint[0]+" is not mapped to a GO enpoint in datastagerconfig.") 
                fo = open("pid.file", "w").close
# Create and start the thread list to call iPIDfromURL in parallel
                if arguments.rmode == "DSSfile":
                    getDSSfile(arguments,endpoint,destendpoint)
                    with open('.DSSfile', mode='r') as infile:
                        reader = csv.reader(infile,)
                        DSSlist = {rows[0]:rows[1] for rows in reader if len(rows) == 2}
                        #print DSSlist
                    full_exit("")
                elif arguments.rmode == "icommands":
                    if arguments.verbose: 
                        print "The list of the corresponding PID is going to be saved in pid.file."
                    threadlist=[]
                    for url in outurllist:
                        plainurl = url.replace("//","/")
                        #argument = formatter("url","irods://"+endpoint+":1247"+plainurl)
                        argument = formatter("url","\*"+plainurl)
                        #print plainurl
                        #print argument
                        T=Thread(target=iPIDtoPIDFILE,args=(arguments,argument))
                        T.start()
                        threadlist.append(T)
                    for t in threadlist:
                        t.join()
                    full_exit("All (available) pid(s) wrote in pid.file.")
            else:
                full_exit("You did not provide the taskid!")
def argument_parser(arguments):
    # Cancel the transfer
    global stop
    if arguments.action == "cancel":
        try:
            if arguments.taskid: pass
        except:
            full_exit("You did not provide the taskid!")
        if str(arguments.taskid) == "None":
            full_exit("You did not provide the taskid!")
        print "The transfer activity corresponding to task %s is going to be cancelled." % arguments.taskid
        api = None
        stop = True
        time.sleep(0.2)
        inurllist, outurllist, destendpoint = datamover.canceltask(
            str(arguments.user), str(arguments.taskid))
# Details of the transfer
    if arguments.action == "details":
        try:
            if arguments.taskid: pass
        except:
            full_exit("You did not provide the taskid!")
        if str(arguments.taskid) == "None":
            full_exit("You did not provide the taskid!")
        print "The transfer activity corresponding to task %s follows." % arguments.taskid
        api = None
        stop = True
        time.sleep(0.2)
        #urlendpoint = datamover.defineurlendpoint(str(arguments.user))
        #print urlendpoint
        datamover.detailsoftask(str(arguments.user), str(arguments.taskid))
# Stage out
    if arguments.direction == "out":
        if arguments.sub_action == "irods":
            if arguments.path and arguments.pathfile:
                full_exit("Only one between -p and -pF is allowed!")
            irodssource(arguments)
            if arguments.verbose:
                print "Source end-point: " + arguments.src_site
        elif arguments.sub_action == "url":
            if arguments.url and arguments.urlfile:
                print "Only one between -U and -UF is allowed!"
            if arguments.rmode == "DSSfile":
                if arguments.verbose: print "Using .DSSfile"
                arguments.src_site = DSSfile_urlsource(arguments)
            elif arguments.rmode == "icommands":
                arguments.src_site = urlsource(arguments)
            if arguments.verbose:
                print "Source end-point: " + arguments.src_site
        elif arguments.sub_action == "pid":
            if arguments.pid and arguments.pidfile:
                print "Only one between -P and -PF is allowed!"
                full_exit("Done!")
            if not arguments.rmode:
                print "The rmode (-m) is mandatory!"
                sys.exit(1)
            if arguments.rmode == "DSSfile":
                if arguments.verbose: print "Using .DSSfile"
                stop = True
                getDSSfile(arguments, arguments.dssfiledefserver,
                           [arguments.dssfiledefserver])
                arguments.src_site = DSSfile_pidsource(arguments)
            elif arguments.rmode == "icommands":
                if arguments.verbose: print "Using icommands"
                arguments.src_site = pidsource(arguments)
                if arguments.verbose:
                    print "Source end-point: " + arguments.src_site
            else:
                print "You are staging out so you can only specify iRODS or PID or URL!"
                full_exit("Done!")
# Stage in
    if arguments.direction == "in":
        if arguments.action == "issue":
            if arguments.path:
                if arguments.pathfile:
                    print "Only one between -p and -pF is allowed!"
                    full_exit("Done!")
                print "You are staging in so save the taskID in order to know the PID(s)."
                file_list = []
                file_list.append(arguments.src_dir + "/" + arguments.path)
                json_results = json.dumps(file_list)
                fo = open("json_file", "w")
                fo.write(json_results)
                fo.close()
            elif arguments.pathfile:
                strglist = []
                fo = open(arguments.pathfile, "r")
                strglist = fo.readlines()
                fo.close()
                file_list = []
                for filename in strglist:
                    file_list.append(arguments.src_dir + "/" +
                                     filename.rstrip())
                json_results = json.dumps(file_list)
                fo = open("json_file", "w")
                fo.write(json_results)
                fo.close()
            else:
                full_exit("One between -p and -pF is mandatory.")
        elif arguments.action == "pid":
            if not arguments.rmode:
                full_exit("The rmode (-m) is mandatory!")
            if arguments.taskid:
                api = None
                inurllist, outurllist, destendpoint = datamover.lookforurl(
                    str(arguments.user), str(arguments.taskid))
                #print inurllist
                #print outurllist
                #print destendpoint
                if not all_same(destendpoint):
                    print "All the pid should be mapped to the same GO endpoint."
                    full_exit("Done!")
                urlendpoint = datamover.defineurlendpoint(str(arguments.user))
                #print urlendpoint
                for url, ep in urlendpoint.items():
                    #print url,ep,arguments.user+"#"+destendpoint[0]
                    if ep == arguments.user + "#" + destendpoint[0]:
                        endpoint = url
                if endpoint == "":
                    full_exit(
                        "The server " + destendpoint[0] +
                        " is not mapped to a GO enpoint in datastagerconfig.")
                fo = open("pid.file", "w").close
                # Create and start the thread list to call iPIDfromURL in parallel
                if arguments.rmode == "DSSfile":
                    getDSSfile(arguments, endpoint, destendpoint)
                    with open('.DSSfile', mode='r') as infile:
                        reader = csv.reader(infile, )
                        DSSlist = {
                            rows[0]: rows[1]
                            for rows in reader if len(rows) == 2
                        }
                        #print DSSlist
                    full_exit("")
                elif arguments.rmode == "icommands":
                    if arguments.verbose:
                        print "The list of the corresponding PID is going to be saved in pid.file."
                    threadlist = []
                    for url in outurllist:
                        plainurl = url.replace("//", "/")
                        #argument = formatter("url","irods://"+endpoint+":1247"+plainurl)
                        argument = formatter("url", "\*" + plainurl)
                        #print plainurl
                        #print argument
                        T = Thread(target=iPIDtoPIDFILE,
                                   args=(arguments, argument))
                        T.start()
                        threadlist.append(T)
                    for t in threadlist:
                        t.join()
                    full_exit("All (available) pid(s) wrote in pid.file.")
            else:
                full_exit("You did not provide the taskid!")