Permalink
Cannot retrieve contributors at this time
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?
master_project_JLU2018/bin/3.1_create_gtf/Modules/Ensembl/FTPHandling/URLRetrieve.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
68 lines (44 sloc)
2.08 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |