import os, os.path, warnings
import time
from ftplib import FTP,all_errors
from sets import Set

# vvvv USER SETTINGS vvvv #
enableDelete = True     #safety to control deletion of files after processing
sleeptime = 600         #time in seconds between loops
loop = True             #set to 'True' to loop forever, 'False' to run once
#localpath below is the destination of all csv files tranferred
localpath = '/var/testdata/csv'
#remotepath below is an array of:
#   (IP address,logon name,logon password,remote dir,system name)
remotepaths = [
    ('192.168.1.1','username','userpass','d:/riapps/testdata/csv','Roos01'),
    ('192.168.1.2','username','userpass','d:/riapps/testdata/csv','Roos02'),
    ('192.168.1.3','username','userpass','d:/riapps/testdata/csv','Roos03')]
# ^^^^ USER SETTINGS ^^^^ #

def main():
    # disables the warning from the call to tempnam()
    warnings.filterwarnings('ignore', 'tempnam .*', RuntimeWarning)
    os.chdir(localpath)
    for (server, user, password, path, risys) in remotepaths:
        print 'Connecting to %s' % risys
        connection = FTP(server)
        connection.set_debuglevel(0)
        try:
            connection.login(user, password)
            connection.cwd(path)
            ftpfilelist = connection.nlst()
        except all_errors:
            print 'Connection failed'
            ftpfilelist = []
        localfilelist = Set(os.listdir('.'))
        localbaselist = Set([os.path.splitext(x)[0] for x in localfilelist])
        for remotefile in ftpfilelist:
            if remotefile in localfilelist:
                continue
            basename = os.path.splitext(remotefile)[0]
            if basename in localbaselist:
                if enableDelete:
                    connection.delete(remotefile)
                    os.unlink(basename+'.X')
                    print 'Deleting %s' % remotefile
                else:
                    print 'Skipping %s' % remotefile
            else:
                tempname = os.tempnam('.')
                newfile = file(tempname, 'wb')
                print 'Downloading %s' % remotefile
                connection.retrbinary('RETR ' + remotefile, newfile.write, 1024)
                newfile.close()
                os.rename(tempname,remotefile)
        connection.quit()

if __name__ == '__main__':
    if loop:
        while 1:
            print '\nProcessing CSV files on %s'% time.asctime()
            main()
            print ''
            time.sleep(sleeptime) #seconds
    else:
        main()
