Skip to content

PR #2 #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions ftpretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -223,7 +255,14 @@ 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):
if self.conn.pwd() == "/":
break
self.conn.cwd("..")
return self.conn.pwd()

def delete(self, remote):
""" Delete a file from server """
try:
Expand Down Expand Up @@ -257,6 +296,14 @@ 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 """
self.put(None, name, contents="")

def abort(self):
""" Aborts any ongoing file transfers """
return self.conn.abort()

def close(self):
""" End the session """
try:
Expand Down