def connect(self): """ Tente de se connecter """ try: self.ftp = Ftp(self.address, self.user, self.port, self.timeout) self.ftp.connect(self.address, self.port) self.ftp.login(self.user, self.password) if Config.is_prot_d() is True: self.ftp.prot_p() self.test_prot_d() return ("connected", self.ftp) except ftplib.all_errors as e: e = str(e) print("\n") error(e + "\n") if 'TLS' in e or 'plain' in e: cprint( "[b]Solution[/b]: Change the \"[b][blue]ftps[/blue][/b]\" options to " "\"[b][green]true[/green][/b]\" in \"[warning]{}[/warning]\"\n" .format(Config.get_config_path_for_print_only())) elif 'incorrect' in e or 'password' in e: cprint( "[b]Solution[/b]: Be sure to have the right \"[b][blue]address[/blue][/b]\"," " \"[b][blue]address[/blue][/b]\"," " \"[b][blue]user[/blue][/b]\" and" " \"[b][blue]port[/blue][/b]\"" " in \"[warning]{}[/warning]\"\n".format( Config.get_config_path_for_print_only())) sys.exit(1)
def print_ls_wihout_options(self, ls_info_dic): output = "" for filename, info in ls_info_dic.items(): if info["type"] == "dir": output += "[blue]{}[/blue]/ ".format(filename) else: output += "{} ".format(filename) cprint(output)
def welcome(self): """ Affiche le message de bienvenue du serveur """ try: success("You are now connected to server") cprint("\n[b]Welcome {}![/b] I am:\n{}\n".format( self.user, self.ftp.getwelcome())) except: fatal("Cannot go further") sys.exit(1)
def colorize(self, list, nlst): for idx, el in enumerate(list): file = nlst[idx].split("/")[-1] line = el[0:(len(file) * -1)] if file[0] == ".": if line[0] == "d": file = "[b][blue]{}[/endc]/".format(file.replace("/", "")) else: file = "[b][header]{}[/endc]".format(file.replace("/", "")) elif line[0] == "d": file = "[b][blue]{}[/endc]/".format(file) cprint("{} {}".format(line, file))
def test_prot_d(self): try: sys.stdout = open(os.devnull, 'w') self.ftp.retrlines('LIST') sys.stdout = sys.__stdout__ except: print("\n") error("PROT P required\n") cprint( "[b]Solution[/b]: Change the \"[b][blue]prot_d[/blue][/b]\" options to \"[b][green]true[/green][/b]\" in \"[warning]{}[/warning]\"\n" .format(Config.get_config_path_for_print_only())) sys.exit(1)
def del_recursive(self, path): if self.ftp.is_file(path): self.del_file(path) else: cnt = self.ftp.ls_info(path) if len(cnt): for key, el in cnt.items(): if el["type"] == "file": srcpath = self.ftp.abspath(path, el["name"]) self.del_file(srcpath) cprint("{}...[green]OK[/green]".format(srcpath)) else: self.del_recursive(self.ftp.abspath(path, el["name"])) self.del_dir(path)
def attempt(self): """ Lance l'interface de connexion manuelle """ state = "idle" Config.display_config(show=['user', 'address', 'port', 'timeout']) while state != "connected": if state == "failed": error("FTP failed to connect: {}".format(res)) try: tmp = getpass("FTP Password: "******"" else self.password except KeyboardInterrupt: cprint("\n[b]Good Bye {}![/b]".format(self.user)) sys.exit(1) state, res = self.connect() self.config() self.welcome()
def print_ls_with_options(self, path): path = self.ftp.sabspath(path) with Capture() as nlst: if "a" in self.argv[1] or "A" in self.argv[1]: self.ftp.retrlines("NLST -a " + path) else: self.ftp.retrlines("NLST " + path) if "l" in self.argv[1] or "L" in self.argv[1]: with Capture() as output: if "a" in self.argv[1] or "A" in self.argv[1]: self.ftp.retrlines("LIST -a {}".format(path)) else: self.ftp.retrlines("LIST {}".format(path)) self.colorize(output, nlst) elif "a" in self.argv[1] or "A" in self.argv[1]: with Capture() as output: self.ftp.retrlines("LIST -a {}".format(path)) for idx, el in enumerate(output): file = nlst[idx] output[idx] = "[b][blue]{}[/endc]/".format( file.replace("/", "")) if el[0] == "d" else file cprint(" ".join(output)) else: warning("invalid options")
def display_config(cls, file="ftp", show=[], hide=[], prefix_filename=True, keep_show_order=True): cprint(file.upper() + " config file: [warning]{}[/warning]".format(cls.get_config_path_for_print_only(file))) data = cls.load() if keep_show_order and len(show): for k in show: if k in data and not k in hide: output = "{}: [green]{}[/green]" if prefix_filename: output = "{} {}".format(file.upper(), output) cprint(output.format(k.capitalize(), data[k])) else: for k, v in data.iteritems(): if (k in show or not len(show)) and k not in hide: output = "{}: [green]{}[/green]" if prefix_filename: output = "{} {}".format(file.upper(), output) cprint(output.format(k.capitalize(), v))
def del_dir(self, path): path = self.ftp.sabspath(path) self.ftp.rmd(path) cprint("{}...[green]OK[/green]".format(path))
def help(self): if self.__doc__ != None: cprint(self.__doc__) else: warning("Unavailable documentation")