6.3.1.5. FTP input

6.3.1.5. FTP input

This object fetches a file by ftp on construction. The file is then read from the local copy. The host name and remote filename arguments are mandatory. Optional arguments given by keywords are:
port
The port number of the host to use for FTP. Defaults to 21.
user
User name. Defaults to 'anonymous'.
account
User's account name.
password
User's password. If user is anonymous, the default password is `realuser@host' where realuser is the real user name (glanced from the `LOGNAME' or `USER' environment variable) and host is the hostname as returned by socket.gethostname().
remote_directory
The directory to 'cd' to to get the nominated file. If not specified, no 'cd' is done and the file is expected in the FTP root.
local_filename
The name of the file to which the download should be written. Defaults to 'remote_filename'.
refresh_interval
The is the number of days old the local file can be before ftp is done. The default is 28. If this value is set to 0, a download is always done. If the value is set to -1, a download is done only if the local file does not exist; delete the file to force another download.
An exception is thrown if there is no local file, and the attempt to ftp the remote file fails. No exception is thrown if the local file exists, even if it is out of date.
Start python section to interscript/drivers/sources/ftp.py[1]
     1: #line 252 "source_drivers.ipk"
     2: #---------------------------------------------------------
     3: # gets input by FTP
     4: import ftplib
     5: import time
     6: import os
     7: from interscript.drivers.sources.base import source
     8: from interscript.drivers.sources.base import eof
     9: 
    10: class ftp_file_source(source):
    11:   def __init__(self,host,remote_filename,**kwds):
    12:     source.__init__(self)
    13:     self.name = remote_filename
    14:     self.remote_filename = remote_filename
    15:     self.host = host
    16:     self.g = g
    17:     for k in kwds.keys():
    18:       self.__dict__[k]=kwds[k]
    19:     if not hasattr(self,'local_filename'):
    20:       self.local_filename = self.remote_filename
    21:     self.os = os
    22:     self.fetch()
    23:     self.file = open(self.local_filename,'r')
    24:     self.closed = 0
    25: 
    26:   def transfer(self,data):
    27:     self.file.write(data+'\n')
    28: 
    29:   def fetch(self):
    30:     if not hasattr(self,'refresh_interval'):
    31:       self.refresh_interval = 28
    32:     if self.refresh_interval < 0: self.refresh_interval = 100000
    33:     self.local_file_exists = 1
    34:     try:
    35:       f = open(self.local_filename)
    36:       f.close()
    37:       if verbosity>=4: print 'local file',self.local_filename,'exists'
    38:     except:
    39:       if verbosity>=4: print 'local file',self.local_filename,'does NOT exist'
    40:       self.local_file_exists = 0
    41: 
    42:     if self.local_file_exists:
    43:       self.local_file_modify_time = os.stat(self.local_filename)[stat.ST_MTIME]
    44:       now = time.time()
    45:       age = (now - self.local_file_modify_time)/ (24 * 60 * 60)
    46:       download = age > self.refresh_interval
    47:     else:
    48:       download = 1
    49: 
    50:     if hasattr(self.g,'download'):
    51:       if self.g.download == 'always': download = 1
    52:       if self.g.download == 'never': download = 0
    53: 
    54:     if download:
    55:       try:
    56:         if verbosity>=2: print 'downloading',self.remote_filename
    57:         # create FTP object
    58:         ftp = ftplib.FTP()
    59: 
    60:         # connect to server
    61:         if hasattr(self,'port'):
    62:           ftp.connect(self.host,self.port)
    63:         else:
    64:           ftp.connect(self.host)
    65:         print 'connected to',self.host
    66: 
    67:         # login to server
    68:         if hasattr(self,'user'):
    69:           if hasattr(self,'password'):
    70:             if hasattr(self,'account'):
    71:               ftp.login(self.user,self.password,self.account)
    72:             else: ftp.login(self.user,self.password)
    73:           else: ftp.login(self.user)
    74:         else: ftp.login()
    75:         if verbosity>=4: print 'logged in'
    76: 
    77:         # set remote directory
    78:         if hasattr(self,'remote_directory'):
    79:           ftp.cwd(self.remote_directory)
    80:           print 'changed to remote directory',self.remote_directory
    81: 
    82:         # get file to a temporary
    83:         try:
    84:           tmp_filename = tempfile.mktemp()
    85:           self.file= open(tmp_filename,'w')
    86:           print 'opened',tmp_filename,'for download'
    87:           ftp.retrlines('RETR '+self.remote_filename, self.transfer)
    88:           self.file.close()
    89:           ftp.quit()
    90:           if verbosity>=2: print 'download complete'
    91: 
    92:           file = open(tmp_filename,'r')
    93:           newlines = file.readlines()
    94:           file.close()
    95: 
    96:           if self.local_file_exists:
    97:             file = open(self.local_filename,'r')
    98:             oldlines = file.readlines()
    99:             file.close()
   100: 
   101:             if newlines != oldlines:
   102:               if verbosity>=4: print 'Local file',self.local_filename,'UPDATED from',self.remote_filename
   103:             else:
   104:               if verbosity>=4: print 'Local file',self.local_filename,'unchanged'
   105:           else:
   106:             if verbosity>=4: print 'Writing new local file',self.local_filename
   107: 
   108:           # note that the local file is written even if it isn't changed
   109:           # to update the time stamp
   110:           file = open(self.local_filename,'w')
   111:           file.writelines(newlines)
   112:           file.close()
   113:           self.os.remove(self.tmp_filename)
   114: 
   115:         except:
   116:           print 'Cannot download',self.remote_filename,
   117:           if hasattr(self,'remote_directory'):
   118:             print 'from directory',self.remote_directory
   119:           else: print 'of',self.host
   120:           file.close()
   121:           self.os.remove(tmp_filename)
   122:           ftp.quit()
   123: 
   124:       except:
   125:         pass # ignore errors from ftp attempt
   126:     else:
   127:       print 'Skipping ftp download'
   128: 
   129:   def __del__(self):
   130:     self.file.close()
   131: 
   132:   def readline(self):
   133:     line = self.file.readline()
   134:     if len(line)==0: raise eof
   135:     self.lines_read = self.lines_read + 1
   136:     return line
   137: 
   138:   def get_filename(self):
   139:     return self.name
   140: 
End python section to interscript/drivers/sources/ftp.py[1]