Changeset 119
- Timestamp:
- 2002-03-30 16:48:07 (7 years ago)
- Files:
-
- trunk/_mock_ftplib.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/_mock_ftplib.py
r112 r119 30 30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 31 32 # $Id: _mock_ftplib.py,v 1. 7 2002/03/30 14:53:01schwa Exp $32 # $Id: _mock_ftplib.py,v 1.8 2002/03/30 16:48:07 schwa Exp $ 33 33 34 34 """ … … 41 41 42 42 import ftplib 43 44 try: 45 import cStringIO as StringIO 46 except ImportError: 47 import StringIO 48 49 DEBUG = 0 50 51 class MockSocket: 52 def __init__(self, mock_socket_file_contents=''): 53 self.mock_socket_file_contents = mock_socket_file_contents 54 55 def makefile(self, mode): 56 return StringIO.StringIO(self.mock_socket_file_contents) 43 import StringIO 57 44 58 45 … … 323 310 324 311 312 DEBUG = 0 313 314 # class MockFileObject(StringIO.StringIO): 315 # """ 316 # Mock class for the file objects _contained in_ _FTPFile 317 # objects (not for _FTPFile objects themselves!). 318 # """ 319 # contents = '' 320 # 321 # def __init__(self, contents, mode='r'): 322 323 324 class MockSocket: 325 """ 326 Mock class which is used to return something from 327 MockSession.transfercmd. 328 """ 329 def __init__(self, mock_file_content=''): 330 self.mock_file_content = mock_file_content 331 332 def makefile(self, mode): 333 return StringIO.StringIO(self.mock_file_content) 334 335 325 336 class MockSession: 326 337 """ 338 Mock class which works like ftplib.FTP for the purpose of the 339 unit tests. 340 """ 327 341 # used by MockSession.cwd and MockSession.pwd 328 342 current_dir = '/home/sschwarzer' … … 344 358 drwxr-sr-x 6 45854 200 512 Sep 20 1999 scios2"""} 345 359 360 # file content to be used (indirectly) with transfercmd 361 mock_file_content = '' 362 346 363 def __init__(self, host='', user='', password=''): 347 self.pwd_result_index = 0 348 364 pass 365 366 def _remove_trailing_slash(self, path): 367 if path.endswith('/'): 368 path = path[:-1] 369 return path 370 349 371 def voidcmd(self, cmd): 372 if DEBUG: 373 print cmd 350 374 if cmd == 'STAT': 351 375 return 'MockSession server awaiting your commands ;-)' 376 elif cmd.startswith('TYPE '): 377 return 352 378 else: 353 379 raise ftplib.error_perm … … 356 382 return self.current_dir 357 383 384 def cwd(self, path): 385 path = self._remove_trailing_slash(path) 386 self.current_dir = path 387 358 388 def dir(self, path, callback=None): 389 "Provide a callback function with each line of a directory listing." 359 390 if DEBUG: 360 391 print 'dir: %s' % path 361 if path.endswith('/'): 362 path = path[:-1] 392 path = self._remove_trailing_slash(path) 363 393 if not self.dir_contents.has_key(path): 364 394 raise ftplib.error_perm … … 370 400 callback(line) 371 401 402 def transfercmd(self, cmd): 403 """ 404 Return a MockSocket object whose makefile method will return 405 a mock file object. 406 """ 407 if DEBUG: 408 print cmd 409 return MockSocket(self.mock_file_content) 410 372 411 373 412 class FailOnLoginSession(MockSession): … … 375 414 raise ftplib.error_perm 376 415 416
