def deleteFile(self, filename):
        """
		Delete a file
		
		@param filename:  the name of the file to delete
		@type filename: string
		"""
        if not self.__logged:
            self.warning("not logged")
            return

        tpl = self.encapsule(sftp_event=templates.delete(filename=filename),
                             cmd=DELETE_FILE)
        tpl.addRaw(filename)
        self.logSentEvent(shortEvt="delete file", tplEvt=tpl)

        if self.cfg['agent-support']:
            agent_cmd = {'cmd': DELETE_FILE, 'filename': filename}
            self.ssh().notifyAgent(cfg=agent_cmd)
        else:
            try:
                self.ssh().channel().remove(filename)
            except Exception as e:
                tpl = self.encapsule(
                    sftp_event=templates.response_error(rsp=str(e)),
                    cmd=DELETE_FILE)
                tpl.addRaw(str(e))
                self.logRecvEvent(shortEvt="response error", tplEvt=tpl)
            else:
                tpl = self.encapsule(sftp_event=templates.response(),
                                     cmd=DELETE_FILE)
                self.logRecvEvent(shortEvt="file deleted", tplEvt=tpl)
    def deleteFolder(self, path):
        """
		Delete a folder
		
		@param path: path folder
		@type path: string
		"""
        if not self.__logged:
            self.warning("not logged")
            return

        tpl = self.encapsule(sftp_event=templates.delete(path=path),
                             cmd=DELETE_FOLDER)
        tpl.addRaw(path)
        self.logSentEvent(shortEvt="delete folder", tplEvt=tpl)

        if self.cfg['agent-support']:
            agent_cmd = {'cmd': DELETE_FOLDER, 'path': path}
            self.ssh().notifyAgent(cfg=agent_cmd)
        else:
            try:
                self.ssh().channel().rmdir(path)
            except Exception as e:
                tpl = self.encapsule(
                    sftp_event=templates.response_error(rsp=str(e)),
                    cmd=DELETE_FOLDER)
                tpl.addRaw(str(e))
                self.logRecvEvent(shortEvt="response error", tplEvt=tpl)
            else:
                tpl = self.encapsule(sftp_event=templates.response(),
                                     cmd=DELETE_FOLDER)
                self.logRecvEvent(shortEvt="folder deleted", tplEvt=tpl)
    def putFolder(self, fromPath, toPath, overwrite=False):
        """
		Put folder content with recursive mode
		
		@param fromPath: local path to upload
		@type fromPath: string
		
		@param toPath: remote destination
		@type toPath: string
		
		@param overwrite: overwrite file/folder if already exists in remote side
		@type overwrite: boolean
		"""
        if not self.__logged:
            self.warning("not logged")
            return

        tpl = self.encapsule(sftp_event=templates.put_folder(
            fromPath=fromPath, toPath=toPath, overwrite=overwrite),
                             cmd=PUT_FOLDER)
        tpl.addRaw(fromPath)
        self.logSentEvent(shortEvt="put folder", tplEvt=tpl)

        if self.cfg['agent-support']:
            agent_cmd = {
                'cmd': PUT_FOLDER,
                'from-path': fromPath,
                'overwrite': overwrite
            }
            if toPath is not None:
                agent_cmd['to-path'] = toPath
            else:
                agent_cmd['to-path'] = ''
            self.ssh().notifyAgent(cfg=agent_cmd)
        else:
            try:
                rsp = 0  # nb files uploaded
                rsp = self.__putFolder(fromPath=fromPath,
                                       toPath=toPath,
                                       overwrite=overwrite)
            except Exception as e:
                tpl = self.encapsule(
                    sftp_event=templates.response_error(rsp=str(e)),
                    cmd=PUT_FOLDER)
                tpl.addRaw(str(e))
                self.logRecvEvent(shortEvt="response error", tplEvt=tpl)
            else:
                tpl = self.encapsule(
                    sftp_event=templates.response(rsp=str(rsp)),
                    cmd=PUT_FOLDER)
                tpl.addRaw(str(rsp))
                self.logRecvEvent(shortEvt="folder uploaded", tplEvt=tpl)
    def listingFolder(self, path, extended=False):
        """
		Listing the folder.
		
		@param path: current path folder
		@type path: string

		@param extended: extended mode (default=False)
		@type extended: boolean
		"""
        if not self.__logged:
            self.warning("not logged")
            return

        tpl = self.encapsule(sftp_event=templates.listing(path=path),
                             cmd=LISTING_FOLDER)
        if path is not None: tpl.addRaw(path)
        self.logSentEvent(shortEvt="listing", tplEvt=tpl)

        if self.cfg['agent-support']:
            agent_cmd = {
                'cmd': LISTING_FOLDER,
                'path': path,
                'extended': extended
            }
            self.ssh().notifyAgent(cfg=agent_cmd)
        else:
            try:
                if extended:
                    ret = []
                    ret_tmp = self.ssh().channel().listdir_attr(path=path)
                    for attr in ret_tmp:
                        ret.append(str(attr))
                else:
                    ret = self.ssh().channel().listdir(path=path)
            except Exception as e:
                tpl = self.encapsule(
                    sftp_event=templates.response_error(rsp=str(e)),
                    cmd=LISTING_FOLDER)
                tpl.addRaw(str(e))
                self.logRecvEvent(shortEvt="response error", tplEvt=tpl)
            else:
                tpl = self.encapsule(
                    sftp_event=templates.response(rsp='\n'.join(ret)),
                    cmd=LISTING_FOLDER)
                tpl.addRaw('\n'.join(ret))
                self.logRecvEvent(shortEvt="folder listing", tplEvt=tpl)
    def getFile(self, filename, toPrivate=False):
        """
		Get file content 
	
		@param filename: file name 
		@type filename: string				
		
		@param toPrivate: save the file in the private area on True (default=False)
		@type toPrivate: boolean			
		"""
        if not self.__logged:
            self.warning("not logged")
            return

        tpl = self.encapsule(sftp_event=templates.get(filename=filename),
                             cmd=GET_FILE)
        tpl.addRaw(filename)
        self.logSentEvent(shortEvt="get file", tplEvt=tpl)
        if self.cfg['agent-support']:
            agent_cmd = {'cmd': GET_FILE, 'filename': filename}
            self.ssh().notifyAgent(cfg=agent_cmd)
        else:
            try:
                myfile = io.BytesIO()
                self.ssh().channel().getfo(filename, myfile)
                read_data = myfile.getvalue()
            except Exception as e:
                tpl = self.encapsule(
                    sftp_event=templates.response_error(rsp=str(e)),
                    cmd=GET_FILE)
                tpl.addRaw(str(e))
                self.logRecvEvent(shortEvt="response error", tplEvt=tpl)
            else:
                if toPrivate:
                    head, tail = os.path.split(filename)
                    self.privateSaveFile(destname=tail, data=read_data)

                # log event
                tpl = self.encapsule(
                    sftp_event=templates.response(content=read_data),
                    cmd=GET_FILE)
                tpl.addRaw(read_data)
                self.logRecvEvent(shortEvt="file downloaded", tplEvt=tpl)
    def hasReceivedError(self, timeout=1.0):
        """
		Wait to receive "error response" event
		
		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float	

		@return: an event matching with the template or None otherwise
		@rtype: templatemessage		
		"""
        TestAdapter.check_timeout(caller=TestAdapter.caller(), timeout=timeout)

        if not self.__logged:
            self.warning("not logged")
            return

        tpl_expected = self.encapsule(sftp_event=templates.response_error())
        evt = self.received(expected=tpl_expected, timeout=timeout)
        return evt
    def renameFolder(self, currentPath, newPath):
        """
		Rename a folder
		
		@param currentPath: current path folder
		@type currentPath: string
		
		@param newPath: new path folder
		@type newPath: string
		"""
        if not self.__logged:
            self.warning("not logged")
            return

        tpl = self.encapsule(sftp_event=templates.rename(fromPath=currentPath,
                                                         toPath=newPath),
                             cmd=RENAME_FOLDER)
        tpl.addRaw(currentPath)
        self.logSentEvent(shortEvt="rename folder", tplEvt=tpl)

        if self.cfg['agent-support']:
            agent_cmd = {
                'cmd': RENAME_FOLDER,
                'current-path': currentPath,
                'new-path': newPath
            }
            self.ssh().notifyAgent(cfg=agent_cmd)
        else:
            try:
                self.ssh().channel().rename(currentPath, newPath)
            except Exception as e:
                tpl = self.encapsule(
                    sftp_event=templates.response_error(rsp=str(e)),
                    cmd=RENAME_FOLDER)
                tpl.addRaw(str(e))
                self.logRecvEvent(shortEvt="response error", tplEvt=tpl)
            else:
                tpl = self.encapsule(sftp_event=templates.response(),
                                     cmd=RENAME_FOLDER)
                self.logRecvEvent(shortEvt="folder renamed", tplEvt=tpl)
    def renameFile(self, currentFilename, newFilename):
        """
		Rename a file
		
		@param currentFilename: current filename
		@type currentFilename: string
		
		@param newFilename: new filename
		@type newFilename: string
		"""
        if not self.__logged:
            self.warning("not logged")
            return

        tpl = self.encapsule(sftp_event=templates.rename(
            fromFilename=currentFilename, toFilename=newFilename),
                             cmd=RENAME_FILE)
        tpl.addRaw(currentFilename)
        self.logSentEvent(shortEvt="rename file", tplEvt=tpl)

        if self.cfg['agent-support']:
            agent_cmd = {
                'cmd': RENAME_FILE,
                'current-filename': currentFilename,
                'new-filename': newFilename
            }
            self.ssh().notifyAgent(cfg=agent_cmd)
        else:
            try:
                self.ssh().channel().rename(currentFilename, newFilename)
            except Exception as e:
                tpl = self.encapsule(
                    sftp_event=templates.response_error(rsp=str(e)),
                    cmd=RENAME_FILE)
                tpl.addRaw(str(e))
                self.logRecvEvent(shortEvt="response error", tplEvt=tpl)
            else:
                tpl = self.encapsule(sftp_event=templates.response(),
                                     cmd=RENAME_FILE)
                self.logRecvEvent(shortEvt="file renamed", tplEvt=tpl)
    def addFolder(self, path, mode=511):
        """
		Add a folder
		
		@param path: path folder
		@type path: string
		
		@param mode: folder mode in octal (default=511 (0o777))
		@type mode: integer
		"""
        if not self.__logged:
            self.warning("not logged")
            return

        tpl = self.encapsule(sftp_event=templates.add(path=path,
                                                      mode="%s" % mode),
                             cmd=ADD_FOLDER)
        tpl.addRaw(path)
        self.logSentEvent(shortEvt="add", tplEvt=tpl)

        if self.cfg['agent-support']:
            agent_cmd = {'cmd': ADD_FOLDER, 'path': path, 'mode': mode}
            self.ssh().notifyAgent(cfg=agent_cmd)
        else:
            try:
                self.ssh().channel().mkdir(path, mode)
            except Exception as e:
                tpl = self.encapsule(
                    sftp_event=templates.response_error(rsp=str(e)),
                    cmd=ADD_FOLDER)
                tpl.addRaw(str(e))
                self.logRecvEvent(shortEvt="response error", tplEvt=tpl)
            else:
                tpl = self.encapsule(sftp_event=templates.response(),
                                     cmd=ADD_FOLDER)
                self.logRecvEvent(shortEvt="folder added", tplEvt=tpl)
    def waitForFolder(self, path, folder, timeout=1.0, watchEvery=0.5):
        """
		Wait for folder, regexp supported on folder name
		
		@param path: current path folder
		@type path: string
		
		@param folder: folder to look up
		@type folder: string		

		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float				
	
		@param watchEvery: watch folder every xx seconds (default=0.5s)
		@type watchEvery: float				
		"""
        TestAdapter.check_timeout(caller=TestAdapter.caller(), timeout=timeout)

        ret = False
        if not self.__logged:
            self.warning("not logged")
            return False

        tpl = self.encapsule(sftp_event=templates.wait_folder(path=path,
                                                              folder=folder),
                             cmd=WAIT_FOLDER)
        if path is not None: tpl.addRaw(path)
        self.logSentEvent(shortEvt="wait for folder (%s sec.)" % timeout,
                          tplEvt=tpl)

        if self.cfg['agent-support']:
            agent_cmd = {
                'cmd': WAIT_FOLDER,
                'path': path,
                'folder': folder,
                'timeout': timeout,
                'watch-every': watchEvery
            }
            self.ssh().notifyAgent(cfg=agent_cmd)
        else:
            timeoutEvent = False
            startTime = time.time()
            try:
                true_folder = ''
                while (not timeoutEvent):
                    if (time.time() - startTime) >= timeout:
                        timeoutEvent = True
                    if not timeoutEvent:
                        # list path
                        ret_tmp = self.ssh().channel().listdir_attr(path=path)
                        for f in ret_tmp:
                            if stat.S_ISDIR(f.st_mode):
                                if re.match(folder, f.filename):
                                    true_folder = f.filename
                                    ret = True
                                    timeoutEvent = True
                    if not timeoutEvent: time.sleep(watchEvery)
            except Exception as e:
                tpl = self.encapsule(
                    sftp_event=templates.response_error(rsp=str(e)),
                    cmd=WAIT_FOLDER)
                tpl.addRaw(str(e))
                self.logRecvEvent(shortEvt="response error", tplEvt=tpl)
            else:
                tpl = self.encapsule(sftp_event=templates.wait_folder(
                    path=path, folder=true_folder, result=ret),
                                     cmd=WAIT_FOLDER)
                self.logRecvEvent(shortEvt="wait folder", tplEvt=tpl)
    def putFile(self, toFilename, fromFilename=None, rawContent=None):
        """
		Put file  in binary transfer mode.
		
		@param toFilename: path of the destination file
		@type toFilename: string			
		
		@param fromFilename: the file to upload
		@type fromFilename: string/None		
		
		@param rawContent: raw content to upload
		@type rawContent: string/None			
		"""
        if not self.__logged:
            self.warning("not logged")
            return

        if fromFilename is None and rawContent is None:
            raise Exception("please to choose a type of upload")

        if fromFilename is not None:
            tpl = self.encapsule(sftp_event=templates.put(
                fromPath=fromFilename, toPath=toFilename),
                                 cmd=PUT_FILE)
            tpl.addRaw(fromFilename)
        if rawContent is not None:
            tpl = self.encapsule(sftp_event=templates.put(content=rawContent,
                                                          toPath=toFilename),
                                 cmd=PUT_FILE)
            tpl.addRaw(rawContent)
        self.logSentEvent(shortEvt="put file", tplEvt=tpl)

        if self.cfg['agent-support']:
            agent_cmd = {
                'cmd': PUT_FILE,
                'to-filename': toFilename,
            }
            if fromFilename is not None:
                agent_cmd['from-filename'] = fromFilename
            else:
                agent_cmd['from-filename'] = ''
            if rawContent is not None:
                agent_cmd['raw-content'] = rawContent
            else:
                agent_cmd['raw-content'] = ''
            self.ssh().notifyAgent(cfg=agent_cmd)
        else:
            try:
                if rawContent is not None:
                    myfile = io.BytesIO(rawContent)

                if fromFilename is not None:
                    myfile = open(fromFilename, 'rb')

                rsp = self.ssh().channel().putfo(myfile, toFilename)
            except Exception as e:
                tpl = self.encapsule(
                    sftp_event=templates.response_error(rsp=str(e)),
                    cmd=PUT_FILE)
                tpl.addRaw(str(e))
                self.logRecvEvent(shortEvt="response error", tplEvt=tpl)
            else:
                tpl = self.encapsule(
                    sftp_event=templates.response(rsp=str(rsp)), cmd=PUT_FILE)
                tpl.addRaw(str(rsp))
                self.logRecvEvent(shortEvt="file uploaded", tplEvt=tpl)
    def onSftpEvent(self, event):
        """
		"""
        if 'sftp-event' in event:

            if event['sftp-event'] == 'response-error':
                tpl = self.encapsule(
                    sftp_event=templates.response_error(rsp=event['err']),
                    cmd=event['cmd'])
                tpl.addRaw(event['err'])
                self.logRecvEvent(shortEvt="response error", tplEvt=tpl)

            if event['sftp-event'] == 'folder-listing':
                tpl = self.encapsule(
                    sftp_event=templates.response(rsp=event['rsp']),
                    cmd=LISTING_FOLDER)
                tpl.addRaw(event['rsp'])
                self.logRecvEvent(shortEvt="folder listing", tplEvt=tpl)

            if event['sftp-event'] == 'folder-added':
                tpl = self.encapsule(sftp_event=templates.response(),
                                     cmd=ADD_FOLDER)
                self.logRecvEvent(shortEvt="folder added", tplEvt=tpl)

            if event['sftp-event'] == 'file-renamed':
                tpl = self.encapsule(sftp_event=templates.response(),
                                     cmd=RENAME_FILE)
                self.logRecvEvent(shortEvt="file renamed", tplEvt=tpl)

            if event['sftp-event'] == 'file-deleted':
                tpl = self.encapsule(sftp_event=templates.response(),
                                     cmd=DELETE_FILE)
                self.logRecvEvent(shortEvt="file deleted", tplEvt=tpl)

            if event['sftp-event'] == 'folder-renamed':
                tpl = self.encapsule(sftp_event=templates.response(),
                                     cmd=RENAME_FOLDER)
                self.logRecvEvent(shortEvt="folder renamed", tplEvt=tpl)

            if event['sftp-event'] == 'folder-deleted':
                tpl = self.encapsule(sftp_event=templates.response(),
                                     cmd=DELETE_FOLDER)
                self.logRecvEvent(shortEvt="folder deleted", tplEvt=tpl)

            if event['sftp-event'] == 'file-downloaded':
                tpl = self.encapsule(
                    sftp_event=templates.response(content=event['content']),
                    cmd=GET_FILE)
                tpl.addRaw(event['content'])
                self.logRecvEvent(shortEvt="file downloaded", tplEvt=tpl)

            if event['sftp-event'] == 'file-uploaded':
                tpl = self.encapsule(
                    sftp_event=templates.response(rsp=event['rsp']),
                    cmd=PUT_FILE)
                tpl.addRaw(event['rsp'])
                self.logRecvEvent(shortEvt="file uploaded", tplEvt=tpl)

            if event['sftp-event'] == 'folder-downloaded':
                tpl = self.encapsule(
                    sftp_event=templates.response(content=event['content']),
                    cmd=GET_FOLDER)
                tpl.addRaw(event['content'])
                self.logRecvEvent(shortEvt="folder downloaded", tplEvt=tpl)

            if event['sftp-event'] == 'folder-uploaded':
                tpl = self.encapsule(
                    sftp_event=templates.response(rsp=event['rsp']),
                    cmd=PUT_FOLDER)
                tpl.addRaw(event['rsp'])
                self.logRecvEvent(shortEvt="folder uploaded", tplEvt=tpl)

            if event['sftp-event'] == 'wait-file':
                tpl = self.encapsule(sftp_event=templates.wait_file(
                    path=event['path'],
                    filename=event['filename'],
                    result=event['result']),
                                     cmd=WAIT_FILE)
                self.logRecvEvent(shortEvt="wait file", tplEvt=tpl)

            if event['sftp-event'] == 'wait-folder':
                tpl = self.encapsule(sftp_event=templates.wait_folder(
                    path=event['path'],
                    folder=event['folder'],
                    result=event['result']),
                                     cmd=WAIT_FOLDER)
                self.logRecvEvent(shortEvt="wait folder", tplEvt=tpl)