From 29bbdba200b6a42b4bdbb663abf42cd96c90e7f6 Mon Sep 17 00:00:00 2001 From: Exvixcity <143428058+Exvixcity@users.noreply.github.com> Date: Tue, 24 Oct 2023 11:33:24 +0100 Subject: [PATCH 01/10] Update ftpretty.py --- ftpretty.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/ftpretty.py b/ftpretty.py index 4fed3d1..7132d3c 100644 --- a/ftpretty.py +++ b/ftpretty.py @@ -8,10 +8,23 @@ f.put(local, remote) f.list(remote) f.cd(remote) + f.pwd() + f.descend(remote, force=True) + f.ascend(steps) f.delete(remote) f.rename(remote_from, remote_to) f.close() - + f.mkdir(remote) + f.touch(remote) + f.is_file(remote) + f.is_folder(remote) + f.abort() + + You should also be able to do this: + + from ftpretty import ftpretty + with ftpretty(host, user, pass, secure=False, timeout=10) as f: + [And all of the listed keywords above] """ from __future__ import print_function import datetime @@ -40,9 +53,8 @@ class ftpretty(object): tmp_output = None relative_paths = set(['.', '..']) - def __init__(self, host, user, password, + def __init__(self, host, user="anonymous", password="", secure=False, passive=True, ftp_conn=None, **kwargs): - if 'port' in kwargs: self.port = kwargs['port'] del kwargs['port'] @@ -62,6 +74,12 @@ def __init__(self, host, user, password, if not passive: self.conn.set_pasv(False) + def __enter__(self): + return self + + def __exit__(self): + self.close() + def __getattr__(self, name): """ Pass anything we don't know about, to underlying ftp connection """ def wrapper(*args, **kwargs): @@ -206,6 +224,20 @@ def list(self, remote='.', extra=False, remove_relative_paths=False): return directory_list + def is_file(self, name): + for item in self.list(extra=True): + if item["name"] == name and item["directory"] == "-": + return True + else: + return False + + def is_folder(self, name): + for item in self.list(extra=True): + if item["name"] == name and item["directory"] == "d": + return True + else: + return False + def is_not_relative_path(self, path): if isinstance(path, dict): return path.get('name') not in self.relative_paths @@ -223,6 +255,10 @@ def descend(self, remote, force=False): self.conn.mkd(directory) self.conn.cwd(directory) return self.conn.pwd() + + def ascend(self, steps=1): + for _ in range(steps): + self.conn.cwd("..") def delete(self, remote): """ Delete a file from server """ @@ -257,6 +293,16 @@ def mkdir(self, new_dir): """ Create directory on the server """ return self.conn.mkd(new_dir) + def touch(self, name): + """ Create a file on the server """ + open(name, mode="x") + self.put(name) + os.remove(name) + + def abort(self): + """ Aborts any ongoing file transfers """ + return self.conn.abort() + def close(self): """ End the session """ try: From 1b217d8f89be896f6fc70bf7d80a80770a967afc Mon Sep 17 00:00:00 2001 From: Exvixcity <143428058+Exvixcity@users.noreply.github.com> Date: Tue, 24 Oct 2023 11:39:57 +0100 Subject: [PATCH 02/10] Update ftpretty.py --- ftpretty.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ftpretty.py b/ftpretty.py index 7132d3c..20bbb71 100644 --- a/ftpretty.py +++ b/ftpretty.py @@ -225,6 +225,7 @@ def list(self, remote='.', extra=False, remove_relative_paths=False): return directory_list def is_file(self, name): + """ Checks whether an item is a file """ for item in self.list(extra=True): if item["name"] == name and item["directory"] == "-": return True @@ -232,6 +233,7 @@ def is_file(self, name): return False def is_folder(self, name): + """ Checks whether an item is a folder """ for item in self.list(extra=True): if item["name"] == name and item["directory"] == "d": return True From c91e007e49edffae63916808ddbc74d60dd1dc63 Mon Sep 17 00:00:00 2001 From: Exvixcity <143428058+Exvixcity@users.noreply.github.com> Date: Thu, 26 Oct 2023 21:04:52 +0100 Subject: [PATCH 03/10] Update ftpretty.py - Made it so that if the ascend() function reaches "/" before "steps" reaches 0, it just breaks from the for loop - ascend() now returns the current working directory --- ftpretty.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ftpretty.py b/ftpretty.py index 20bbb71..1663372 100644 --- a/ftpretty.py +++ b/ftpretty.py @@ -210,7 +210,7 @@ def get_tree(self, remote, local): else: pass - def list(self, remote='.', extra=False, remove_relative_paths=False): + def list(self, remote='.', extra=False, remove_relative_paths=False, supports_mlsd=False): """ Return directory list """ if extra: self.tmp_output = [] @@ -225,7 +225,6 @@ def list(self, remote='.', extra=False, remove_relative_paths=False): return directory_list def is_file(self, name): - """ Checks whether an item is a file """ for item in self.list(extra=True): if item["name"] == name and item["directory"] == "-": return True @@ -233,7 +232,6 @@ def is_file(self, name): return False def is_folder(self, name): - """ Checks whether an item is a folder """ for item in self.list(extra=True): if item["name"] == name and item["directory"] == "d": return True @@ -260,7 +258,10 @@ def descend(self, remote, force=False): def ascend(self, steps=1): for _ in range(steps): + if self.conn.pwd() == "/": + break self.conn.cwd("..") + return self.conn.pwd() def delete(self, remote): """ Delete a file from server """ From 2034a381e2d530073f37a9689f766b2a633e5171 Mon Sep 17 00:00:00 2001 From: Exvixcity <143428058+Exvixcity@users.noreply.github.com> Date: Thu, 26 Oct 2023 21:05:44 +0100 Subject: [PATCH 04/10] Update ftpretty.py --- ftpretty.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ftpretty.py b/ftpretty.py index 1663372..dc214ce 100644 --- a/ftpretty.py +++ b/ftpretty.py @@ -210,7 +210,7 @@ def get_tree(self, remote, local): else: pass - def list(self, remote='.', extra=False, remove_relative_paths=False, supports_mlsd=False): + def list(self, remote='.', extra=False, remove_relative_paths=False): """ Return directory list """ if extra: self.tmp_output = [] From 7186f61c8a602dccfeb9e8389b98751d6dee98f6 Mon Sep 17 00:00:00 2001 From: Exvixcity <143428058+Exvixcity@users.noreply.github.com> Date: Fri, 27 Oct 2023 19:57:49 +0100 Subject: [PATCH 05/10] Update ftpretty.py --- ftpretty.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ftpretty.py b/ftpretty.py index dc214ce..69e77e7 100644 --- a/ftpretty.py +++ b/ftpretty.py @@ -298,9 +298,7 @@ def mkdir(self, new_dir): def touch(self, name): """ Create a file on the server """ - open(name, mode="x") - self.put(name) - os.remove(name) + self.put(None, name, contents=contents) def abort(self): """ Aborts any ongoing file transfers """ From cb4f531b537bfd2b21427248dd62b56b30efce8d Mon Sep 17 00:00:00 2001 From: Exvixcity <143428058+Exvixcity@users.noreply.github.com> Date: Sun, 29 Oct 2023 16:36:22 +0000 Subject: [PATCH 06/10] Update ftpretty.py - Added open() which opens a file on the server --- ftpretty.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ftpretty.py b/ftpretty.py index 69e77e7..c9f802d 100644 --- a/ftpretty.py +++ b/ftpretty.py @@ -263,6 +263,12 @@ def ascend(self, steps=1): self.conn.cwd("..") return self.conn.pwd() + def open(self, remote): + """ Opens and reads a file on the server """ + buffer = buffer_type(self.get(remote)) + buffer.seek(0) + return buffer.read().decode() + def delete(self, remote): """ Delete a file from server """ try: From dfeeb1ceeaa504d4a034be241098403ab21c11d1 Mon Sep 17 00:00:00 2001 From: Exvixcity <143428058+Exvixcity@users.noreply.github.com> Date: Sun, 29 Oct 2023 16:40:14 +0000 Subject: [PATCH 07/10] Update ftpretty.py --- ftpretty.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ftpretty.py b/ftpretty.py index c9f802d..cb63b8e 100644 --- a/ftpretty.py +++ b/ftpretty.py @@ -267,7 +267,11 @@ def open(self, remote): """ Opens and reads a file on the server """ buffer = buffer_type(self.get(remote)) buffer.seek(0) - return buffer.read().decode() + try: + file_contents = buffer.read().decode() + except UnicodeDecodeError: + return [False, "Not a text file"] + return file_contents def delete(self, remote): """ Delete a file from server """ From ae44309eb16fac8b3ffee1350f41546265368512 Mon Sep 17 00:00:00 2001 From: Exvixcity <143428058+Exvixcity@users.noreply.github.com> Date: Sun, 29 Oct 2023 16:40:42 +0000 Subject: [PATCH 08/10] Update ftpretty.py --- ftpretty.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ftpretty.py b/ftpretty.py index cb63b8e..2355f4c 100644 --- a/ftpretty.py +++ b/ftpretty.py @@ -18,6 +18,7 @@ f.touch(remote) f.is_file(remote) f.is_folder(remote) + f.open() f.abort() You should also be able to do this: From b4fd6792b2927a043a058b511f637b2884077d05 Mon Sep 17 00:00:00 2001 From: Exvixcity <143428058+Exvixcity@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:48:47 +0000 Subject: [PATCH 09/10] Update ftpretty.py --- ftpretty.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ftpretty.py b/ftpretty.py index 2355f4c..09f7d64 100644 --- a/ftpretty.py +++ b/ftpretty.py @@ -309,7 +309,7 @@ def mkdir(self, new_dir): def touch(self, name): """ Create a file on the server """ - self.put(None, name, contents=contents) + self.put(None, name, contents="") def abort(self): """ Aborts any ongoing file transfers """ From 0b6d37dc42fa2d6f723410e4da129f8848cb886d Mon Sep 17 00:00:00 2001 From: Exvixcity <143428058+Exvixcity@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:49:59 +0000 Subject: [PATCH 10/10] Update ftpretty.py --- ftpretty.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/ftpretty.py b/ftpretty.py index 09f7d64..fbe1d65 100644 --- a/ftpretty.py +++ b/ftpretty.py @@ -18,7 +18,6 @@ f.touch(remote) f.is_file(remote) f.is_folder(remote) - f.open() f.abort() You should also be able to do this: @@ -263,16 +262,6 @@ def ascend(self, steps=1): break self.conn.cwd("..") return self.conn.pwd() - - def open(self, remote): - """ Opens and reads a file on the server """ - buffer = buffer_type(self.get(remote)) - buffer.seek(0) - try: - file_contents = buffer.read().decode() - except UnicodeDecodeError: - return [False, "Not a text file"] - return file_contents def delete(self, remote): """ Delete a file from server """