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 ]
     1: #line 317 "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 file_source
     8: from interscript.drivers.sources.base import eof
     9: import string
    10: 
    11: class ftp_file_source(file_source):
    12:   def __init__(self,host,remote_filename,**kwds):
    13:     apply(file_source.__init__, (self,), kwds)
    14:     self.name = remote_filename
    15:     self.remote_filename = remote_filename
    16:     self.host = host
    17:     self.g = g
    18:     for k in kwds.keys():
    19:       self.__dict__[k]=kwds[k]
    20:     if not hasattr(self,'local_filename'):
    21:       self.local_filename = self.remote_filename
    22:     self.os = os
    23:     self.fetch()
    24:     self.file = open(self.local_filename,'r')
    25:     self.closed = 0
    26: 
    27:   def transfer(self,data):
    28:     self.file.write(data+'\n')
    29: 
    30:   def fetch(self):
    31:     if not hasattr(self,'refresh_interval'):
    32:       self.refresh_interval = 28
    33:     if self.refresh_interval < 0: self.refresh_interval = 100000
    34:     self.local_file_exists = 1
    35:     try:
    36:       f = open(self.local_filename)
    37:       f.close()
    38:       if verbosity>=4: print 'local file',self.local_filename,'exists'
    39:     except:
    40:       if verbosity>=4: print 'local file',self.local_filename,'does NOT exist'
    41:       self.local_file_exists = 0
    42: 
    43:     if self.local_file_exists:
    44:       self.local_file_modify_time = os.stat(self.local_filename)[stat.ST_MTIME]
    45:       now = time.time()
    46:       age = (now - self.local_file_modify_time)/ (24 * 60 * 60)
    47:       download = age > self.refresh_interval
    48:     else:
    49:       download = 1
    50: 
    51:     if hasattr(self.g,'download'):
    52:       if self.g.download == 'always': download = 1
    53:       if self.g.download == 'never': download = 0
    54: 
    55:     if download:
    56:       try:
    57:         if verbosity>=2: print 'downloading',self.remote_filename
    58:         # create FTP object
    59:         ftp = ftplib.FTP()
    60: 
    61:         # connect to server
    62:         if hasattr(self,'port'):
    63:           ftp.connect(self.host,self.port)
    64:         else:
    65:           ftp.connect(self.host)
    66:         print 'connected to',self.host
    67: 
    68:         # login to server
    69:         if hasattr(self,'user'):
    70:           if hasattr(self,'password'):
    71:             if hasattr(self,'account'):
    72:               ftp.login(self.user,self.password,self.account)
    73:             else: ftp.login(self.user,self.password)
    74:           else: ftp.login(self.user)
    75:         else: ftp.login()
    76:         if verbosity>=4: print 'logged in'
    77: 
    78:         # set remote directory
    79:         if hasattr(self,'remote_directory'):
    80:           ftp.cwd(self.remote_directory)
    81:           print 'changed to remote directory',self.remote_directory
    82: 
    83:         # get file to a temporary
    84:         try:
    85:           tmp_filename = tempfile.mktemp()
    86:           self.file= open(tmp_filename,'w')
    87:           print 'opened',tmp_filename,'for download'
    88:           ftp.retrlines('RETR '+self.remote_filename, self.transfer)
    89:           self.file.close()
    90:           ftp.quit()
    91:           if verbosity>=2: print 'download complete'
    92: 
    93:           file = open(tmp_filename,'r')
    94:           newlines = file.readlines()
    95:           file.close()
    96: 
    97:           if self.local_file_exists:
    98:             file = open(self.local_filename,'r')
    99:             oldlines = file.readlines()
   100:             file.close()
   101: 
   102:             if newlines != oldlines:
   103:               if verbosity>=4: print 'Local file',self.local_filename,'UPDATED from',self.remote_filename
   104:             else:
   105:               if verbosity>=4: print 'Local file',self.local_filename,'unchanged'
   106:           else:
   107:             if verbosity>=4: print 'Writing new local file',self.local_filename
   108: 
   109:           # note that the local file is written even if it isn't changed
   110:           # to update the time stamp
   111:           file = open(self.local_filename,'w')
   112:           file.writelines(newlines)
   113:           file.close()
   114:           self.os.remove(self.tmp_filename)
   115: 
   116:         except:
   117:           print 'Cannot download',self.remote_filename,
   118:           if hasattr(self,'remote_directory'):
   119:             print 'from directory',self.remote_directory
   120:           else: print 'of',self.host
   121:           file.close()
   122:           self.os.remove(tmp_filename)
   123:           ftp.quit()
   124: 
   125:       except:
   126:         pass # ignore errors from ftp attempt
   127:     else:
   128:       print 'Skipping ftp download'
   129: 
End python section to interscript/drivers/sources/ftp.py[1]