Skip to content
Permalink
3877157589
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
68 lines (44 sloc) 2.08 KB
import ftplib
from Modules.Ensembl.FTPHandling.FTPEntry import FTPEntry
class FTPHandler:
"""
Class to browse through ftp folders and download entries to local file
@author: Sebastian Beyvers
@contact: sebastian.beyvers@med.uni-giessen.de
"""
def __init__(self, url, wd):
# Constructor
# input_parameters: wd = woking directory
# url = Url where to browse (in this case ftp.ensembl.org)
self.ftp = ftplib.FTP(url)
self.ftp.login()
self.ftp.cwd(wd)
def change_dir(self, to_dir):
# Change ftp current working directory to parameter to_dir
self.ftp.cwd(to_dir)
def get_all_entries(self):
# Get all ftp entries at current working directory
return self.ftp.nlst()
def get_all_entries_from_dir(self, dire):
# Helper method to get all entries in directory (dire)
# input_parameter: dire = directory where to get all entries
# return value: list of all entries
self.change_dir(dire)
return self.get_all_entries()
def get_all_entries_as_FTPEntry(self):
# Get All Files as FTPEntry
# returns list of FTPEntry objects, these objects are helper objects for easier determination if a
# FTP-Entry is file or Folder
files = self.ftp.nlst()
return [FTPEntry(item, self.ftp, self.ftp.pwd()) for item in files]
def save_entries_to_file(self, origin, target):
# Method to save targeted entries to file
# input_parameter: origin = Origin FTP-Entries
# target = directory, where to save the files.
self.change_dir(origin)
for file in self.get_all_entries_as_FTPEntry():
if file.gettype() == "f":
# Download only Checksum & gff.gz files
if file.getfilename() not in ["README", "manifest.tsv"]:
print("Downloading....... > " + file.getfilename())
self.ftp.retrbinary("RETR " + file.getfilename(), open(target + "/" + file.getfilename(), 'wb').write)