Changeset 170

Show
Ignore:
Timestamp:
2002-04-01 18:29:30 (7 years ago)
Author:
schwa
Message:
Documentation mostly complete.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ftputil.txt

    r168 r170  
    7171    def func(path, file): 
    7272        ... 
    73      
     73 
    7474which works on the local file system and catches OSErrors. If you 
    7575change the parameter list to 
     
    129129FTPHost OBJECTS 
    130130 
     131Construction 
     132------------ 
     133 
     134FTPHost instances may be generated with the following call: 
     135 
     136    host = ftputil.FTPHost(host, user, password, account, 
     137                           session_factory=ftplib.FTP) 
     138 
     139The first four parameters are strings with the same meaning as for the 
     140FTP class in the ftplib module. The keyword argument session_factory 
     141may be used to generate FTP connections with other factories than the 
     142default ftplib.FTP. For example, the M2Crypto distribution uses a 
     143secure FTP class which is derived from ftplib.FTP. 
     144 
     145In fact, all positional and keyword arguments other than 
     146session_factory are passed to the factory to generate a new background 
     147session (which happens for every remote file that is opened; see 
     148below). 
     149 
     150FTPHost attributes and methods 
     151------------------------------ 
     152 
     153curdir, pardir, sep 
     154    are strings which denote the current and the parent directory on 
     155    the remote server. sep identifies the path separator. Though RFC 
     156    959 (File Transfer Protocol) notes that these values may be server 
     157    dependent, the Unix counterparts seem to work well in practice, 
     158    even for non-Unix servers. 
     159 
     160file(path, mode='r') 
     161    returns a file-like object that is connected to the path on the 
     162    remote host. This path may be absolute or relative to current 
     163    directory on the remote host (this directory can be determined 
     164    with the getcwd method). As with local file objects the default 
     165    mode is 'r', i. e. reading text files. Valid modes are 'r', 'rb', 
     166    'w', and 'wb'. 
     167 
     168open(path, mode='r') 
     169    is an alias for file (see above). 
     170 
     171copyfileobj(source, target, length=64*1024) 
     172    copies the contents from the file-like object source to the 
     173    file-like object target. The only difference to shutil.copyfileobj 
     174    is the default buffer size. 
     175 
     176upload(source, target, mode='') 
     177    copies a local source file (given by a filename, i. e. a string) 
     178    to the remote host under the name target. Both source and target 
     179    may be absolute paths or relative to their corresponding current 
     180    directory (on the local or the remote host, respectively). The 
     181    mode may be '' or 'a' for ASCII uploads or 'b' for binary uploads. 
     182    ASCII mode is the default (again, similar to regular local file 
     183    objects). 
     184 
     185download(source, target, mode='') 
     186    performs a download from the remote source to a target file. Both 
     187    source and target are strings. Additionally, the description of 
     188    the upload method applies here, too. 
     189 
     190upload_if_newer(source, target, mode='') 
     191    is similar to the upload method. The only difference is that the 
     192    upload is only invoked if the time of the last modification for 
     193    the source file is more recent than that of the target file, or 
     194    the target doesn't exist at all. 
     195 
     196download_if_newer(source, target, mode='') 
     197    corresponds to upload_if_newer but performs a download from the 
     198    server to the local host. Read the descriptions of download and 
     199    upload_if_newer for more. 
     200 
     201close() 
     202    closes the connection to the remote host. After this, no more 
     203    interaction with the FTP server is possible without using a new 
     204    FTPHost object. 
     205 
     206getcwd() 
     207    returns the absolute current directory on the remote host. This 
     208    method acts similar to os.getcwd. 
     209 
     210chdir(directory) 
     211    sets the current directory on the FTP server. This resembles 
     212    os.chdir, as you may have expected. :-) 
     213 
     214mkdir(path, [mode]) 
     215    makes the given directory on the remote host. In the current 
     216    implementation, this doesn't construct "intermediate" directories 
     217    which don't already exist. The mode parameter is ignored. This is 
     218    for compatibilty with os.mkdir if an FTPHost object is passed into 
     219    a function instead of the os module (see the subsection on Python 
     220    exceptions above for an explanation). 
     221 
     222rmdir(path) 
     223    removes the given remote directory. Currently, "intermediate" 
     224    directories can't be deleted (as with os.rmdir). 
     225 
     226remove(path) 
     227    removes a file on the remote host (similar to os.remove). 
     228 
     229unlink(path) 
     230    is an alias for remove. 
     231 
     232rename(source, target) 
     233    renames the source file (or directory) on the FTP server. 
     234 
     235listdir(path) 
     236    returns a list containing the names of the files and directories 
     237    in the given path; similar to os.listdir. 
     238 
     239lstat(path) 
     240    returns an object similar that from os.lstat (a "tuple" with 
     241    additional attributes; see the documentation of the os module for 
     242    details). However, due to the nature of the application, there are 
     243    some important aspects to keep in mind: 
     244 
     245    - The result is derived by parsing the output of a DIR command on 
     246      the server. Therefore, the result from FTPHost.lstat can not 
     247      contain nore information than the received text. In particular: 
     248 
     249    - User and group ids can only be determined as strings, not as 
     250      numbers, and that only, if the server supplies them. This is 
     251      usually the case with Unix servers but may not be for other FTP 
     252      server programs. 
     253 
     254    - Values for the time of the last modification may be rough, 
     255      depending on the information from the server. For timestamps 
     256      older than a year, this usually means that the precision of the 
     257      modification timestamp value is not better than days. For newer 
     258      files, the information may be accurate to a minute. 
     259 
     260    - Links can only be recognized on servers that provide this 
     261      information in the DIR output. 
     262       
     263    - Items that can't be determined at all, are set to None. 
     264 
     265    Currently, ftputil recognizes the MS Robin FTP server. Otherwise, 
     266    a format commonly used by Unix servers is assumed. If you need to 
     267    parse output from another server type, please contact me under the 
     268    email address at the end of this text. 
     269 
     270stat(path) 
     271    return stat information also for files which are pointed to by a 
     272    link. This method follows multiple links until a regular file or 
     273    directory is found. If an infinite link chain is encountered, a 
     274    PermanentError is raised. 
     275 
     276FTPHost.path methods 
     277-------------------- 
     278 
     279FTPHost objects contain an attribute named path, similar to os.path. 
     280(See there for documentation.) The following methods can be applied 
     281to the remote host with the same semantics as for os.path: abspath, 
     282basename, commonprefix, dirname, exists, getmtime, getsize, isabs, 
     283isdir, isfile, islink, join, normcase, normpath, split, splitdrive, 
     284splitext, and even walk. See the section on the os.path module in the 
     285Library Reference, 
     286http://www.python.org/doc/current/lib/module-os.path.html 
    131287 
    132288---------------------------------------------------------------------- 
    133289FTPFile OBJECTS 
    134290 
     291FTPFile objects as returned by a call to FTPHost.file (or 
     292FTPHost.open) have, like "normal" file objects, the following methods. 
     293Note that ftputil supports both binary and text mode (with the 
     294appropriate line ending conversions). The following methods - with the 
     295same arguments and semantics as for local files - can be used: close, 
     296read, readline, readlines, write, writelines, and xreadlines. See 
     297the section "File objects" in the Library Reference, 
     298http://www.python.org/doc/current/lib/bltin-file-objects.html 
    135299 
    136300---------------------------------------------------------------------- 
    137301BUGS AND LIMITATIONS 
    138302 
     303- ftputil needs at least Python 2.1 to run. 
     304 
     305- Depending on local time and/or daylight saving time settings 
     306  different local hosts may interpret the modification times in the 
     307  FTP server output differently. 
     308 
     309- Timeouts of individual child sessions currently are not handled. 
     310 
     311- Until now, I haven't paid attention to thread safety. In principle, 
     312  different FTPFile objects should be usable in different threads. 
     313 
     314- FTPFile objects in text mode _may not_ support charsets with more 
     315  than one byte per character. Please email me your experiences 
     316  (address below), if you deal with multibyte text streams. 
     317 
     318---------------------------------------------------------------------- 
     319SEE ALSO 
     320 
     321Postel J, Reynolds J. 1985. 
     322    RFC 959 - File Transfer Protocol (FTP). 
     323    http://www.ietf.org/rfc/rfc959.txt 
     324 
     325Van Rossum G, Drake Jr FL. 2001. 
     326    Python Library Reference. 
     327    http://www.python.org/doc/current/lib/lib.html 
     328 
    139329---------------------------------------------------------------------- 
    140330AUTHOR 
     
    142332Stefan Schwarzer <s.schwarzer@ndh.net> 
    143333 
    144 ---------------------------------------------------------------------- 
    145