Пример #1
0
def printfile(filename,data,sortData=True,append=False):
    ''' Writes the output from getData (or an arbitrary list) to a file of your choice. Takes 
        a filename, data, and an optional "sortData" argument. File will be placed in a 
        directory called "crnscript-data" in your home directory (typically your network drive 
        on Windows boxen, or ~ on *nix) unless a directory is specified.
    '''
    openmode = "a" if append else "w"

    if not os.path.dirname(filename): # ie if a raw filename is given rather than a directory+filename 
        rootpath = _userDataDirectory()
        file = open(os.path.join(rootpath,filename),openmode)
    else:
        file = open(filename,openmode)
            
    if isinstance(data,Map): # Special case -- for maps, print key : value by zipping them into tuples
        data = zip(data.keySet(),data.values())
        
    if isinstance(data,dict): # Similar special case but for python dicts
        data = zip(data.keys(), data.values())
    
    try:
        if sortData: data = sorted(data)
        for n in data:
            file.write(str(n).rstrip('\r\n'))
            file.write("\n")
    except TypeError: # In case the user accidentally passes in something besides a list
        file.write(str(data))

    file.close()
Пример #2
0
def uploadDataToFtp(source,target,server="ftp0"):
    ''' Uploads data (typically a FactCollection) to the ftp server as a target filename of your
    choice. You can specify a directory (relative paths are presumed to begin with /pub/data/uscrn);
    if you do not, data are placed in /pub/data/uscrn/temp/. Remember that, as with all
    NCDC data, data are placed on the ftp0 server and then automatically moved over to the ftp
    server a little while later. If you upload data to the FTP server, you are encouraged to
    delete it when it is no longer needed. '''
    from ftplib import FTP
    from random import randint
    from java.io import File
    
    tempfilename = "crnscript_tempfile_" + str(randint(100000,999999)) + ".txt"
    printfile(tempfilename,source,sortData=False)
    uploadFileToFtp(tempfilename,target,server)
    os.remove(os.path.join(_userDataDirectory(),tempfilename))
Пример #3
0
def uploadFileToFtp(source,target,server="ftp0"):
    ''' Uploads a file to the ftp server as a file of your choice. Target can be a path 
    (relative paths are treated as starting with /pub/data/uscrn) or path+filename. If you 
    give a path only, it is presumed that you wish to preserve the source filename. 
    Remember that, as with all NCDC data, data are placed on the ftp0 server and then 
    automatically moved over to the ftp server a little while later. '''
    from ftplib import FTP
    target = _createAbsoluteFilenameForFtp(target)
    
    (targethead,targettail) = os.path.split(target)
    (sourcehead,sourcetail) = os.path.split(source)
    
    # If a filename was not given, use the source filename
    if targettail == "":
        targettail = sourcetail
        target = "%s/%s"%(targethead,targettail)

    # Figure out location of source file
    if not os.path.dirname(source) or not os.path.isabs(source): # if target is a relative path: # ie if a raw filename is given rather than a directory+filename 
        rootpath = _userDataDirectory()
        source = os.path.join(rootpath,source)

    try:
        sourcefile = open(source)
    except IOError:
        print "Could not open %s -- are you sure the file exists?"
        return
    ftp = FTP()
    ftp.connect(server)
    ftp.login()
    try:
        ftp.cwd(targethead)
    except:
        ftp.mkd(targethead) # Will only create the leafmost directory.
        ftp.cwd(targethead)
    try:
        ftp.delete(target) # Permissions on NCDC FTP servers mean you have to delete instead of overwriting
    except:
        pass # Can't delete it if it's not there.
    ftp.storlines("STOR "+target,sourcefile)
    sourcefile.close()
    try:
        ftp.quit()
    except: # quit() can fail if the server behaves impolitely
        ftp.close()