Changeset 451:1bac6c89ddf7
- Timestamp:
- Feb 3, 2006, 12:55:57 AM (15 years ago)
- Branch:
- default
- Convert:
- svn:778c30c8-61e0-0310-89d4-fe2f97a467b2/trunk@468
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
_test_real_ftp.py
r439 r451 42 42 import ftputil 43 43 from ftputil import ftp_error 44 44 from ftputil import ftp_stat 45 45 46 46 # difference between local times of server and client; if 0.0, server … … 182 182 host.rmdir(dir_name) 183 183 184 def test_autodetect_directory_format(self): 185 host = self.host 186 host.auto_set_directory_format() 187 self.failUnless(isinstance(host._stat, ftp_stat._UnixStat)) 188 184 189 def make_local_file(self): 185 190 fobj = file("_localfile_", "wb") -
ftp_error.py
r396 r451 1 # Copyright (C) 2003-200 4, Stefan Schwarzer <sschwarzer@sschwarzer.net>1 # Copyright (C) 2003-2006, Stefan Schwarzer <sschwarzer@sschwarzer.net> 2 2 # All rights reserved. 3 3 # … … 34 34 """ 35 35 36 # $Id : ftp_error.py,v 1.4 2004/07/09 21:48:34 schwa Exp$36 # $Id$ 37 37 38 38 import ftplib … … 57 57 class RootDirError(InternalError): pass 58 58 class InaccessibleLoginDirError(InternalError): pass 59 class FormatDetectionError(InternalError): pass 59 60 60 61 class TimeShiftError(FTPError): pass -
ftp_stat.py
r447 r451 1 # Copyright (C) 2002-200 4, Stefan Schwarzer1 # Copyright (C) 2002-2006, Stefan Schwarzer 2 2 # All rights reserved. 3 3 # … … 376 376 377 377 378 _stat_classes = { 379 "unix": _UnixStat, 380 "ms" : _MSStat, 381 } 382 383 378 384 # Unix format 379 385 # total 14 -
ftputil.py
r448 r451 187 187 return False 188 188 189 def set_directory_format(self, server_platform): 189 def auto_set_directory_format(self): 190 """ 191 Try to find out the directory format used by the FTP server 192 automatically. 193 194 This is _not_ the default applied by ftputil because the 195 method requires write access to the current directory which 196 can't be taken for granted. 197 198 If the auto-detection fails for any reason, a 199 `FormatDetectionError` is raised. The error conditions include 200 the impossibility to find an appropriate parser for the 201 directory format but in fact there are many possible reasons, 202 e. g. no write access to the current directory. If such an 203 exception occurs, the stat functionality of the `FTPHost` 204 instance won't work but other functionality might. 205 """ 206 helper_file_name = "_ftputil_format_" 207 # open a dummy file for writing in the current directory 208 # on the FTP host, then close it 209 try: 210 file_ = self.file(helper_file_name, 'w') 211 file_.close() 212 except ftp_error.FTPIOError: 213 # couldn't write the file; since errors should never pass 214 # silently, raise an exception; using `getcwd` here should 215 # be safe because it doesn't need `stat` itself 216 raise ftp_error.FormatDetectionError( 217 "couldn't detect directory format using directory '%s'" % 218 self.getcwd()) 219 # try to parse the directory with the available parsers until 220 # one works 221 try: 222 for format in ftp_stat._stat_classes.keys(): 223 self.set_directory_format(format) 224 try: 225 stat_result = self.stat(helper_file_name) 226 except FTPOSError: 227 # parser doen't work; try the next 228 continue 229 else: 230 if (stat_result._st_name, stat_result.st_size) == \ 231 (helper_file_name, 0): 232 break 233 else: 234 raise ftp_error.FormatDetectionError( 235 "no usable directory parser found") 236 finally: 237 # remove the helper file 238 try: 239 self.unlink(helper_file_name) 240 except ftp_error.FTPOSError: 241 raise ftp_error.FormatDetectionError( 242 "couldn't remove helper file") 243 244 def set_directory_format(self, directory_format): 190 245 """ 191 246 Tell this `FTPHost` object the directory format of the remote … … 194 249 work as it should. 195 250 196 ` server_platform` is one of the following strings:251 `directory_format` is one of the following strings: 197 252 198 253 "unix": Use this if the directory listing from the server … … 211 266 is raised. 212 267 """ 213 parsers = {"unix" : ftp_stat._UnixStat,214 "ms" : ftp_stat._MSStat}215 if parsers.has_key(server_platform):216 self._stat = parsers[server_platform](self)268 try: 269 stat_class = ftp_stat._stat_classes[directory_format] 270 except KeyError: 271 raise ValueError("invalid directory format '%s'" % directory_format) 217 272 else: 218 raise ValueError("invalid server platform '%s'" % server_platform)273 self._stat = stat_class(self) 219 274 220 275 #
Note: See TracChangeset
for help on using the changeset viewer.