Skip to content

Commit

Permalink
Add ftp downloader example
Browse files Browse the repository at this point in the history
  • Loading branch information
avylove committed Aug 10, 2018
1 parent 9914a94 commit 1ff9c60
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Additional Examples
* :download:`context manager <../examples/context_manager.py>` - Managers and counters as context managers
* :download:`floats <../examples/floats.py>` - Support totals and counts that are :py:class:`floats<float>`
* :download:`multiple with logging <../examples/multiple_logging.py>` - Nested progress bars and logging
* :download:`FTP downloader <../examples/ftp_downloader.py>` - Show progress downloading files from FTP

Customization
-------------
Expand Down
82 changes: 82 additions & 0 deletions examples/ftp_downloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright 2018 Avram Lubkin, All Rights Reserved

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

"""
Example FTP downloader
"""

import ftplib
import os

import enlighten


SITE = 'test.rebex.net'
USER = 'demo'
PASSWD = 'password'
DIR = 'pub/example'
DEST = '/tmp'
DEBUG = 0 # 0, 1, 2 are valid
MANAGER = enlighten.get_manager()


class Writer(object):
"""
Context manager for handling download writes
"""

def __init__(self, filename, size, directory=None):
self.filename = filename
self.size = size
if directory:
self.dest = os.path.join(directory, filename)
else:
self.dest = filename
self.status = self.fileobj = None

def __enter__(self):
self.status = MANAGER.counter(total=self.size, desc=self.filename,
unit='bytes', leave=False)
self.fileobj = open(self.dest, 'wb')
return self

def __exit__(self, *args):
self.fileobj.close()
self.status.close()

def write(self, block):
"""
Write to local file and update progress bar
"""
self.fileobj.write(block)
self.status.update(len(block))


def download():
"""
Download all files from an FTP share
"""

ftp = ftplib.FTP(SITE)
ftp.set_debuglevel(DEBUG)
ftp.login(USER, PASSWD)
ftp.cwd(DIR)
filelist = ftp.nlst()
filecounter = MANAGER.counter(total=len(filelist), desc='Downloading',
unit='files')

for filename in filelist:

with Writer(filename, ftp.size(filename), DEST) as writer:
ftp.retrbinary('RETR %s' % filename, writer.write)
print(filename)
filecounter.update()

ftp.close()


if __name__ == '__main__':
download()

0 comments on commit 1ff9c60

Please sign in to comment.