Source code for pyquickhelper.filehelper.ftp_mock

"""
Mock FTP classes.


:githublink:`%|py|5`
"""
from ..loghelper.flog import noLOG
from .ftp_transfer import TransferFTP


[docs]class MockTransferFTP(TransferFTP): """ mock :class:`TransferFTP <pyquickhelper.filehelper.ftp_transfer.TransferFTP>` :githublink:`%|py|12` """
[docs] def __init__(self, fLOG=noLOG): """ constructor :param fLOG: logging function :githublink:`%|py|19` """ TransferFTP.__init__(self, None, "login", "password") self._store = {}
[docs] def run_command(self, command, *args): """ Mock method :meth:`run_command <pyquickhelper.filehelper.ftp_transfer.TransferFTP.run_commnad>` :githublink:`%|py|26` """ if command == self._ftp.mlsd and args == ('.',): return [('setup.py', {'name': 'setup.py'})] elif command == self._ftp.cwd and args == ('.',): return None elif command == self._ftp.pwd and len(args) == 0: return "." elif command == self._ftp.cwd and args == ('..',): return None elif command == self._ftp.storbinary and args[0] == 'STOR test_transfer_ftp.py': self._store[args[0]] = args return None elif command == self._ftp.retrbinary and args[0] == 'RETR test_transfer_ftp.py': b = self._store[args[0].replace("RETR", "STOR")][1] return b'ee' elif command == self._ftp.cwd and args == ('backup',): self._store[args[0]] = args return None elif command == self._ftp.storbinary and args[0] == 'STOR setup.py': self._store[args[0]] = args return None elif command == self._ftp.retrbinary and args[0] == 'RETR setup.py': b = self._store[args[0].replace("RETR", "STOR")][1] s = b.getbuffer() args[1](s) return len(s) else: raise Exception("command='{0}'\nargs={1}".format(command, args))