def downloadFile(self, url_to_download, download_location): url = QUrl(url_to_download) fileInfo = QFileInfo(url.path()) #fileName = fileInfo.fileName() fileName = download_location + fileInfo.fileName() self.file_name = fileName print("Filename = " + fileName) if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information(self, 'Error', 'Unable to save the file %s: %s.' % (fileName, self.outFile.errorString())) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '/' # Download the file. self.httpGetId = self.http.get(path, self.outFile) return fileName
def downloadFile(self, url_to_download, fout): " get the file from remote url to local folder " url = QUrl(url_to_download) fileInfo = QFileInfo(url.path()) fileName = path.join(fout, str(fileInfo.fileName())) if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information( self, "ERROR", "Cant save the file %s: %s." % (fileName, self.outFile.errorString()) ) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False tpath = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if tpath: tpath = str(tpath) else: tpath = sep # Download the file. print(" INFO: Blender Download Started ! ") self.httpGetId = self.http.get(tpath, self.outFile)
def downloadFile(self): url = QUrl(self.downloadUrl) fileInfo = QFileInfo(url.path()) #fileName = fileInfo.fileName() fileName = self.targetDirectory + fileInfo.fileName() if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information(self, 'Error', 'Unable to save the file %s: %s.' % (fileName, self.outFile.errorString())) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False print url path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '/' # Right at the end we append the query string that is lost, the original author did not include this unfortunately. parsedUrl = urlparse(self.downloadUrl) if not parsedUrl.query == '': # fix this mess, otherwise leave it alone path = path + "?" + parsedUrl.query print path # Download the file. self.httpGetId = self.http.get(path, self.outFile)
class DownloadFtp(Download): def __init__(self, file_share, local_path, date): Download.__init__(self, file_share, local_path, date) self.ftp = QFtp(self) self.ftp.dataTransferProgress.connect(self.update_progress) self.ftp.done.connect(self.download_finished) self.ftp.stateChanged.connect(self.state_changed) self.url = QUrl(self._file_share.url) self.out_file = QFile(self.local_path) self.read_bytes = self.out_file.size() def start_download(self): self.ftp.connectToHost(self.url.host(), self.url.port(21)) self.ftp.login() if self.out_file.open(QIODevice.WriteOnly): self.ftp.get(self.url.path(), self.out_file) def stop(self): self.ftp def state_changed(self, state): if state == 1 or state == 2: self._state = 1 def download_finished(self, _): print "finished !" def update_progress(self, read_bytes, total_bytes): self.read_bytes = read_bytes
def downloadFile(self, url, archivo_destino): url = QUrl(url) fileName = archivo_destino directorio_usuario_para_pilas = os.path.dirname(archivo_destino) if not os.path.exists(directorio_usuario_para_pilas): os.mkdir(directorio_usuario_para_pilas) if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information(self, 'Error', 'Lo siento, no se puede descargar el archivo desde %s: %s.' % (self.url, self.outFile.errorString())) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '/' self.httpGetId = self.http.get(path, self.outFile)
def search(self, description): if description.help_ref: ref = description.help_ref else: raise KeyError() url = QUrl(self.baseurl).resolved(QUrl(ref)) if url.isLocalFile(): path = url.toLocalFile() fragment = url.fragment() if os.path.isfile(path): return url elif os.path.isfile("{}.html".format(path)): url = QUrl.fromLocalFile("{}.html".format(path)) url.setFragment(fragment) return url elif os.path.isdir(path) and \ os.path.isfile(os.path.join(path, "index.html")): url = QUrl.fromLocalFile(os.path.join(path, "index.html")) url.setFragment(fragment) return url else: raise KeyError() else: if url.scheme() in ["http", "https"]: path = url.path() if not (path.endswith(".html") or path.endswith("/")): url.setPath(path + ".html") return url
def downloadFile(self): url = QUrl(self.url_to_download) fileName = QFileDialog.getSaveFileName(self, "Enregistrer la liste des dossiers", self.nameFile, "conf") if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information(self, 'Erreur','Impossible d\'enregistrer le fichier %s: %s.' % (fileName, self.outFile.errorString())) self.outFile = None return #Création que la connexion HTTPS mode = QHttp.ConnectionModeHttps port = 0 self.http.setHost(url.host(), mode, port) self.requeteHTTPannule = False path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '/' #Concaténation des paramètres à l'URL path = path+("?") for item in url.queryItems(): path = path + item[0] + "=" + item[1] + "&" self.http.setUser(self.login, self.password) self.httpGetId = self.http.get(path, self.outFile)
def data(self): """Return all the data for this model (including dependencies) in a structured dict """ empty_template = '{}' param_map = {PATH: 'Path', GET: 'GET', POST: 'POST', COOKIE: 'Cookie'} url = QUrl(self.transition.url) path_dict = path_to_dict(url.path()) get_keys, post_keys = set(), set() output = {} if self.override: output['variables'] = [] for param_type, key, template, parent_model in self.override: parent = parent_model if isinstance( parent_model, basestring) else parent_model.data() output['variables'].append({ 'key': key, 'origin': param_map[param_type], 'source': parent, 'template': template, }) if param_type == PATH: path_dict[key] = empty_template elif param_type == GET: get_keys.add(key) elif param_type == POST: post_keys.add(key) url.setPath(dict_to_path(path_dict)) qs_items = [(key, empty_template if key in get_keys else value) for (key, value) in url.queryItems() if (GET, key) not in self.ignored] url.setEncodedQueryItems(qs_items) output['url'] = url.toString() data = dict([(key, empty_template if key in post_keys else value) for (key, value) in self.transition.data if (POST, key) not in self.ignored]) if data: output['data'] = data #if self.ignored: # output['ignored'] = [(param_map[param_type], value) for (param_type, value) in self.ignored] if self.selector is not None: output['columns'] = {'data': str(self.selector)} elif self.columns is not None: output['columns'] = { field: str(selector) for (field, selector) in self.columns.items() } output['headers'] = [(str(key), str(value)) for (key, value) in self.transition.request_headers if str(key).lower() not in ('content-length', )] output['verb'] = self.transition.verb return output
def downloadFile(self): url = QUrl(self.url_text.text()) if self.txt_location.text() == "": QDir.setCurrent("$HOME/Downloads") else: QDir.setCurrent(self.loc) self.statusbar.showMessage("Downloading") fileInfo = QFileInfo(url.path()) fileName = fileInfo.fileName() if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information( self, 'Error', 'Unable to save the file %s: %s.' % (fileName, self.outFile.errorString())) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '$HOME/Downloads' # Download the file. self.httpGetId = self.http.get(path, self.outFile)
def downloadFile(self): url = QUrl(self.url_text.text()) if self.txt_location.text()=="": QDir.setCurrent("$HOME/Downloads") else: QDir.setCurrent(self.loc) self.statusbar.showMessage("Downloading") fileInfo = QFileInfo(url.path()) fileName = fileInfo.fileName() if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information(self, 'Error', 'Unable to save the file %s: %s.' % (fileName, self.outFile.errorString())) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '$HOME/Downloads' # Download the file. self.httpGetId = self.http.get(path, self.outFile)
def urlFromValue(self, value): variable = value.variable origin = variable.attributes.get("origin", "") if origin and QDir(origin).exists(): origin = QUrl.fromLocalFile(origin) elif origin: origin = QUrl(origin) if not origin.scheme(): origin.setScheme("file") else: origin = QUrl("") base = origin.path() if base.strip() and not base.endswith("/"): origin.setPath(base + "/") name = QUrl(str(value)) url = origin.resolved(name) if not url.scheme(): url.setScheme("file") return url
class FTP_Handler( QFtp ): # ======================================================================= def __init__( self, parent=None ): # ------------------------------------------------------------------- QFtp.__init__( self, parent ); # ------------------------------------------------------------------- self.PARENT = parent; self.DEBUG = False; self.LOG_TAG = str(self.__class__.__name__).upper(); # ------------------------------------------------------------------- self.stateChanged.connect( self.ON_STATE_CHANGED ); self.listInfo.connect( self.ON_LIST_INFO_AVAILABLE ); self.commandFinished.connect( self.ON_COMMAND_FINISHED ); self.commandStarted.connect( self.ON_COMMAND_STARTED ); self.readyRead.connect( self.ON_READY_READ ); self.done.connect( self.ON_DONE ); self.dataTransferProgress.connect( self.ON_DATA_TRANSFER_PROGRESS ); self.rawCommandReply.connect( self.ON_RAW_COMMAND_REPLY ); # ------------------------------------------------------------------- self.HOST = ""; self.PATH = ""; self.USER = ""; self.PWD = ""; self.PORT = 21; # ------------------------------------------------------------------- self.FILE_INFO_LIST = []; self.LOGIN_SUCCESS = False; self.ALLOW_TO_RUN = True; self.LAST_ERROR = QFtp.NoError; self.NEW_URL = ""; self.SHOW_HIDDEN_FILES = False; self.SHOW_FILES_FIRST = False; self.PRINT_CONN_INFO = False; # ------------------------------------------------------------------- self.REQ_UIDS = { "connect" : 0, "login" : 0, "cd" : 0, "list" : 0, } # ------------------------------------------------------------------- self.PARENT.SPLASH.STATUS( self.LOG_TAG+": [INIT]" ); # ------------------------------------------------------------------- # ======================================================================= def MK_CONNECT( self, _url, _user="******", _pwd="", _port=21 ): # ------------------------------------------------------------------- try: req = urllib2.Request( _url, headers=self.PARENT.WEB_PAGE.HEADERS ); resp = urllib2.urlopen( req ); t = resp.headers["content-type"]; self.PARENT.DOWNLOAD_MANAGER.REQUEST( _url, False ); return; except Exception as _err: pass; # ------------------------------------------------------------------- try: # ----------------------------------------------------------- # Name, isFile, date, size, owner for _f_info in self.FILE_INFO_LIST: if _url.split("/")[-1] == _f_info[0] and _f_info[1]: self.PARENT.DOWNLOAD_MANAGER.REQUEST( "ftp://"+self.HOST+self.PATH+_f_info[0], False ); return; # ----------------------------------------------------------- self.FILE_INFO_LIST = []; self.Q_URL = QUrl( _url ); self.USER = str(_user); self.PWD = str(_pwd); self.PORT = int(_port); self.HOST = self.HOST if str(self.Q_URL.host()) == "" else str(self.Q_URL.host()); self.PATH = "/" if str(self.Q_URL.path()) == "" else str(self.Q_URL.path()); # ----------------------------------------------------------- if self.PRINT_CONN_INFO: print(" --------------------------------------------------------- " ); print("USER: ["+self.USER+"]" ); print("PWD: ["+self.PWD+"]" ); print("PORT: ["+str(self.PORT)+"]" ); print("HOST: ["+self.HOST+"]" ); print("PATH: ["+self.PATH+"]" ); print(" --------------------------------------------------------- " ); # ----------------------------------------------------------- self.CONNECT(); except Exception as _err: self.LOCAL_ERROR_LOG( "MK_CONNECT[1]: "+str(_err) ); # ------------------------------------------------------------------- # ======================================================================= def CONNECT( self ): # ------------------------------------------------------------------- try: self.REQ_UIDS["connect"] = self.connectToHost( self.HOST, self.PORT ); self.REQ_UIDS["login"] = self.login( self.USER, self.PWD ); except Exception as _err: self.LOCAL_ERROR_LOG( "CONNECT: "+str(_err) ); # ------------------------------------------------------------------- # ======================================================================= def ON_COMMAND_FINISHED( self, _response, _bool ): # ------------------------------------------------------------------- try: if _response == self.REQ_UIDS["connect"]: print("Connected.") elif _response == self.REQ_UIDS["login"]: if self.state() == QFtp.LoggedIn: print(" LoggedIn: 200"); self.LOGIN_SUCCESS = True; print(" cd: ["+self.PATH+"]"); self.REQ_UIDS["cd"] = self.cd( self.PATH ); else: print(" NOT-LoggedIn: 500"); elif _response == self.REQ_UIDS["cd"]: self.REQ_UIDS["list"] = self.list(); elif _response == self.REQ_UIDS["list"]: self.CREATE_TREE( self.PATH ); self.close(); except Exception as _err: self.LOCAL_ERROR_LOG( "ON_COMMAND_FINISHED: "+str(_err) ); # ------------------------------------------------------------------- # ======================================================================= def CREATE_TREE( self, _ADDR ): # ------------------------------------------------------------------- try: LEVEL_UP = ""; if _ADDR == "/": LEVEL_UP = "/"; else: dirs = _ADDR.split("/"); for i in xrange(0, len(dirs)-2): LEVEL_UP += dirs[i]+"/"; except Exception as _err: self.LOCAL_ERROR_LOG( str(_err), "CREATE_TREE:0" ); return False; # ----------------------------------------------------------------- try: # GENERATE HTML-BASED-FILE-EXPLORER _HTML = ""; _HR_ = '<div class="_hr_div"></div>'+"\n"; with open( self.PARENT.DATA_DIR+"css/file_browser.css", 'r' ) as FS: for _l in FS: _HTML += _l; _HTML += _HR_; _HTML += ' ["<a class="_nav_path" href="ftp://'+self.HOST+LEVEL_UP+'">level up</a>"] => '; # ----------------------------------------------------------------- _ADDR = _ADDR.replace("//","/"); QUICK_NAV_PATH = ""; QUICK_NAV_NAME = '["'; QUICK_NAV_DATA = _ADDR.split( "/" ); for quick_nav in QUICK_NAV_DATA: QUICK_NAV_PATH += quick_nav.strip()+"/"; QUICK_NAV_NAME += '<a class="_nav_path" href="ftp://'+self.HOST+QUICK_NAV_PATH+'">'+quick_nav.strip()+"</a>/"; _HTML += (QUICK_NAV_NAME[ 0: len(QUICK_NAV_NAME)-1 ])+'"]'; _HTML += _HR_; except Exception as _err: self.LOCAL_ERROR_LOG( str(_err), "CREATE_TREE:1" ); return False; # ----------------------------------------------------------------- try: _F = []; # files _D = []; # directoris host_path = 'ftp://'+self.HOST+self.PATH; # Name, isFile, date, size, owner for _f_info in self.FILE_INFO_LIST: try: mod_date = "["+strftime("%d-%b-%Y %H:%M:%S", localtime( _f_info[2] ))+"]"; except: print( ":::::::::::::::: "+str(_f_info[2]) ); mod_date = "[out of range]"; if _f_info[1]: _sizeByte = _f_info[3]; _sizeKByte = float(_sizeByte/1024); _sizeMByte = float(_sizeKByte/1024); if _sizeMByte > 0.99999: _size = "{0:.2f} Mb".format( _sizeMByte ); elif _sizeKByte > 0.99999: _size = "{0:.2f} KiB".format( _sizeKByte ); else: _size = str(_sizeByte)+" Byte"; _f = '<div class="_line">'; if _f_info[0:1] == ".": _f += '<div class="_long_name"><a class="_file_hidden" href="'+host_path+_f_info[0]+'"> '+_f_info[0]+'</a></div>'; else: _f += '<div class="_long_name"><a class="_file" href="'+host_path+_f_info[0]+'"> '+_f_info[0]+'</a></div>'; _f += '<span class="_size">'+_size+'</span>'; _f += '<span class="_mod_date">'+mod_date+'</span>'; _f += '<span class="_descr_F">(F)</span>'; _f += '</div>\n'; _F.append( _f ); else: _d = '<div class="_line">'; if _f_info[0:1] == ".": _d += '<div class="_long_name"><a class="_dir_hidden" href="'+host_path+_f_info[0]+'/"> '+_f_info[0]+'/</a></div>'; else: #print(host_path, _f_info[0], [self.HOST,self.PATH]) _d += '<div class="_long_name"><a class="_dir" href="'+host_path+_f_info[0]+'/"> '+_f_info[0]+'/</a></div>'; _d += '<span class="_size">...</span>'; _d += '<span class="_mod_date">'+mod_date+'</span>'; _d += '<span class="_descr_D">(D)</span>'; _d += '</div>\n'; _D.append( _d ); except Exception as _err: self.LOCAL_ERROR_LOG( str(_err), "CREATE_TREE:2" ); return False; # ------------------------------------------------------------------------- for _d in _D: _HTML += _d; for _f in _F: _HTML += _f; """ try: if self.SHOW_FILES_FIRST: #if _file_first: for _f in _F: _HTML += _f; for _d in _D: _HTML += _d; else: for _d in _D: _HTML += _d; for _f in _F: _HTML += _f; except Exception as _err: self.LOCAL_ERROR_LOG( str(_err), "CREATE_TREE:3" ); return False; """ # ------------------------------------------------------------------------- try: new_name = self.PARENT.TMP_DIR+str(int(time()))+".html"; FS = open(new_name, "w"); FS.write( _HTML+_HR_ ); FS.close(); self.NEW_URL = new_name; except Exception as _err: self.LOCAL_ERROR_LOG( str(_err), "CREATE_TREE:4" ); return False; # ------------------------------------------------------------------- try: self.PARENT.WEB_VIEW.load( QUrl(self.NEW_URL) ); except Exception as _err: self.LOCAL_ERROR_LOG( str(_err), "CREATE_TREE:5" ); return False; # ------------------------------------------------------------------- return True; # ------------------------------------------------------------------- # ======================================================================= def ON_LIST_INFO_AVAILABLE( self, _QUrlInfo ): # ------------------------------------------------------------------- try: _f_info = QUrlInfo( _QUrlInfo ); self.FILE_INFO_LIST.append([ str(_f_info.name()), bool( _f_info.isFile() ), long( _f_info.lastModified().toMSecsSinceEpoch()/1000 ), int(_f_info.size()), str(_f_info.owner()) ]); except Exception as _err: self.LOCAL_ERROR_LOG( "LIST_INFO: "+str(_err) ); # ------------------------------------------------------------------- # ======================================================================= def ON_STATE_CHANGED ( self, _state ): # ------------------------------------------------------------------- try: if _state == QFtp.Unconnected: # 0 - There is no connection to the host. if self.DEBUG: print("DocFTPHandler: [QFtp.Unconnected]"); self.close(); elif _state == QFtp.HostLookup: # 1 - A host name lookup is in progress. if self.DEBUG: print("DocFTPHandler: [QFtp.HostLookup]"); elif _state == QFtp.Connecting: # 2 - An attempt to connect to the host is in progress. if self.DEBUG: print("DocFTPHandler: [QFtp.Connecting]"); elif _state == QFtp.Connected: # 3 - Connection to the host has been achieved. if self.DEBUG: print("DocFTPHandler: [QFtp.Connected]"); elif _state == QFtp.LoggedIn: # 4 - Connection and user login have been achieved. if self.DEBUG: print("DocFTPHandler: [QFtp.LoggedIn]"); elif _state == QFtp.Closing: # 5 - The connection is closing down, but it is not yet closed. (The state will be Unconnected when the connection is closed.) if self.DEBUG: print("DocFTPHandler: [QFtp.Closing]"); self.close(); else: print("DocFTPHandler: [QFtp.STATE: 'Unknown'"); except Exception as _err: self.LOCAL_ERROR_LOG( "ON_STATE_CHANGED: "+str(_err) ); # ------------------------------------------------------------------- # ======================================================================= def ON_DATA_TRANSFER_PROGRESS( self, _qint64_A, _qint64_B ): # ------------------------------------------------------------------- try: pass; #print(" ON_DATA_TRANSFER_PROGRESS: ["+str(_qint64_A)+", "+str(_qint64_B)+"]" ); except Exception as _err: self.LOCAL_ERROR_LOG( "ON_DATA_TRANSFER_PROGRESS: "+str(_err) ); # ------------------------------------------------------------------- # ======================================================================= def ON_RAW_COMMAND_REPLY ( self, _int, _str ): # ------------------------------------------------------------------- try: pass; #print(" ON_RAW_COMMAND_REPLY: ["+str(_int)+", "+str(_str)+"]" ); except Exception as _err: self.LOCAL_ERROR_LOG( "ON_RAW_COMMAND_REPLY: "+str(_err) ); # ------------------------------------------------------------------- # ======================================================================= def ON_DONE( self, _bool ): # ------------------------------------------------------------------- try: pass; except Exception as _err: self.LOCAL_ERROR_LOG( "ON_DONE: "+str(_err) ); # ------------------------------------------------------------------- # ======================================================================= def ON_READY_READ( self ): # ------------------------------------------------------------------- try: pass; except Exception as _err: self.LOCAL_ERROR_LOG( "ON_READY_READ: "+str(_err) ); # ------------------------------------------------------------------- # ======================================================================= def ON_COMMAND_STARTED ( self, _int ): # ------------------------------------------------------------------- try: pass; except Exception as _err: self.LOCAL_ERROR_LOG( "ON_COMMAND_STARTED: "+str(_err) ); # ------------------------------------------------------------------- # ======================================================================= def LOCAL_INFO_LOG( self, _msg, METHOD=None ): # ------------------------------------------------------------------- if METHOD is None: self.PARENT.LOCAL_INFO_LOG( "['"+self.LOG_TAG+"']: ["+_msg+"]" ); else: self.PARENT.LOCAL_INFO_LOG( "['"+self.LOG_TAG+"."+METHOD+"']: ["+_msg+"]" ); # ------------------------------------------------------------------- # ======================================================================= def LOCAL_ERROR_LOG( self, _msg, METHOD=None ): # ------------------------------------------------------------------- if self.DEBUG or self.PARENT.DEBUG_GLOBAL: self.PARENT.DEBUGGER.DEBUG(); # ------------------------------------------------------------------- if METHOD is None: self.PARENT.LOCAL_ERROR_LOG( "['"+self.LOG_TAG+"']: ["+_msg+"]" ); else: self.PARENT.LOCAL_ERROR_LOG( "['"+self.LOG_TAG+"."+METHOD+"']: ["+_msg+"]" ); # ------------------------------------------------------------------- # ======================================================================= def LOCAL_WARNING_LOG( self, _msg, METHOD=None ): # ------------------------------------------------------------------- #if self.DEBUG or self.PARENT.DEBUG_GLOBAL: self.PARENT.DEBUGGER.DEBUG(); # ------------------------------------------------------------------- if METHOD is None: self.PARENT.LOCAL_WARNING_LOG( "['"+self.LOG_TAG+"']: ["+_msg+"]" ); else: self.PARENT.LOCAL_WARNING_LOG( "['"+self.LOG_TAG+"."+METHOD+"']: ["+_msg+"]" );
class FileDownloader(object): """The blueprint for downloading file from url.""" def __init__(self, url, output_path, progress_dialog=None): """Constructor of the class. .. versionchanged:: 3.3 removed manager parameter. :param url: URL of file. :type url: str :param output_path: Output path. :type output_path: str :param progress_dialog: Progress dialog widget. :type progress_dialog: QWidget """ # noinspection PyArgumentList self.manager = qgis.core.QgsNetworkAccessManager.instance() self.url = QUrl(url) self.output_path = output_path self.progress_dialog = progress_dialog if self.progress_dialog: self.prefix_text = self.progress_dialog.labelText() self.output_file = None self.reply = None self.downloaded_file_buffer = None self.finished_flag = False def download(self): """Downloading the file. :returns: True if success, otherwise returns a tuple with format like this (QNetworkReply.NetworkError, error_message) :raises: IOError - when cannot create output_path """ # Prepare output path self.output_file = QFile(self.output_path) if not self.output_file.open(QFile.WriteOnly): raise IOError(self.output_file.errorString()) # Prepare downloaded buffer self.downloaded_file_buffer = QByteArray() # Request the url request = QNetworkRequest(self.url) self.reply = self.manager.get(request) self.reply.readyRead.connect(self.get_buffer) self.reply.finished.connect(self.write_data) self.manager.requestTimedOut.connect(self.request_timeout) if self.progress_dialog: # progress bar def progress_event(received, total): """Update progress. :param received: Data received so far. :type received: int :param total: Total expected data. :type total: int """ # noinspection PyArgumentList QCoreApplication.processEvents() self.progress_dialog.adjustSize() human_received = humanize_file_size(received) human_total = humanize_file_size(total) label_text = tr("%s : %s of %s" % ( self.prefix_text, human_received, human_total)) self.progress_dialog.setLabelText(label_text) self.progress_dialog.setMaximum(total) self.progress_dialog.setValue(received) # cancel def cancel_action(): """Cancel download.""" self.reply.abort() self.reply.deleteLater() self.reply.downloadProgress.connect(progress_event) self.progress_dialog.canceled.connect(cancel_action) # Wait until finished # On Windows 32bit AND QGIS 2.2, self.reply.isFinished() always # returns False even after finished slot is called. So, that's why we # are adding self.finished_flag (see #864) while not self.reply.isFinished() and not self.finished_flag: # noinspection PyArgumentList QCoreApplication.processEvents() result = self.reply.error() try: http_code = int(self.reply.attribute( QNetworkRequest.HttpStatusCodeAttribute)) except TypeError: # If the user cancels the request, the HTTP response will be None. http_code = None self.reply.abort() self.reply.deleteLater() if result == QNetworkReply.NoError: return True, None elif result == QNetworkReply.UnknownNetworkError: return False, tr( 'The network is unreachable. Please check your internet ' 'connection.') elif http_code == 408: msg = tr( 'Sorry, the server aborted your request. ' 'Please try a smaller area.') LOGGER.debug(msg) return False, msg elif http_code == 509: msg = tr( 'Sorry, the server is currently busy with another request. ' 'Please try again in a few minutes.') LOGGER.debug(msg) return False, msg elif result == QNetworkReply.ProtocolUnknownError or \ result == QNetworkReply.HostNotFoundError: LOGGER.exception('Host not found : %s' % self.url.encodedHost()) return False, tr( 'Sorry, the server is unreachable. Please try again later.') elif result == QNetworkReply.ContentNotFoundError: LOGGER.exception('Path not found : %s' % self.url.path()) return False, tr('Sorry, the layer was not found on the server.') else: return result, self.reply.errorString() def get_buffer(self): """Get buffer from self.reply and store it to our buffer container.""" buffer_size = self.reply.size() data = self.reply.read(buffer_size) self.downloaded_file_buffer.append(data) def write_data(self): """Write data to a file.""" self.output_file.write(self.downloaded_file_buffer) self.output_file.close() self.finished_flag = True def request_timeout(self): """The request timed out.""" if self.progress_dialog: self.progress_dialog.hide()
class DownloadFtp(Download): progressModified = pyqtSignal(object) stateChanged = pyqtSignal(object) downloadFinished = pyqtSignal(object) speedModified = pyqtSignal(object) def __init__(self, file_share, local_path, date, state): Download.__init__(self, file_share, local_path, date, state) self.ftp = QFtp(self) # Signaux self.ftp.dataTransferProgress.connect(self.update_progress) self.ftp.done.connect(self.download_finished) self.ftp.stateChanged.connect(self.state_changed) # Ouverture de fichiers self.url = QUrl(self._file_share.url) self.out_file = QFile(self.local_path) # Vars self.read_bytes = self.out_file.size() # Timer self.timer = QTimer() self.timer.start(500) self.timer.timeout.connect(self.update_speed) def start_download(self): self.ftp.connectToHost(self.url.host(), self.url.port(21)) self.ftp.login() if self.out_file.open(QIODevice.WriteOnly): self.ftp.get(self.url.path(), self.out_file) def stop(self): self._state = 5 self.ftp.abort() self.timer.stop() self.ftp.close() def state_changed(self, state): if state == 1 or state == 2: self._state = 1 elif state == 3 or state == 4: self._state = 3 self.stateChanged.emit(self) def download_finished(self, _): print "finished !" if self.read_bytes != self.file_share.size: self._state = 7 self._speed = 0 self.timer.stop() self.ftp.abort() self.ftp.close() self._state = 4 self.stateChanged.emit(self) self.ftp.done.disconnect(self.download_finished) self.ftp.stateChanged.disconnect(self.state_changed) self.ftp.dataTransferProgress.disconnect(self.update_progress) self.downloadFinished.emit(self) def update_speed(self): delta = time.time() - self._last_time self._speed = float(self.read_bytes - self._last_size) / delta self.last_time = time.time() self.last_size = self.read_bytes self.speedModified.emit(self) def update_progress(self, read_bytes, total_bytes): self.read_bytes = read_bytes self.progressModified.emit(self)
def setContentList(self): url = QUrl(self.url()) if not url.path().endsWith(u"/"): url.setPath(url.path() + u"/") base_url = self.url().toString() base_path = (url.path()) self.open(self.ReadOnly | self.Unbuffered) content = (u"<html>\n" u"<head>\n" u" <title>" + self.html(base_url) + u"</title>\n" u' <style type="text/css">\n' u" th { background-color: #aaaaaa;\n" u" color: black }\n" u" table { border: solid 1px #aaaaaa }\n" u" tr.odd { background-color: #dddddd;\n" u" color: black\n }\n" u" tr.even { background-color: white;\n" u" color: black\n }\n" u" </style>\n" u"</head>\n\n" u"<body>\n" u"<h1>Listing for " + base_path + u"</h1>\n\n") lines = self.input.splitlines() for line in lines: pieces = line.split("\t") if pieces == ["."]: break try: type, path, host, port = pieces[:4] except ValueError: # This isn't a listing. Let's try returning data instead. self.setContentData() return if type[0] == "i": content += u"<p>" + self.html(type[1:]) + u"</p>" elif type[0] == "h" and path.startswith(u"URL:"): content += u"<p><a href=\"" + path[4:] + u"\">" + self.html( type[1:]) + u"</a></p>" elif type[0] == "0": content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"?type=text\">" + self.html( type[1:]) + u"</a></p>" elif type[0] == "1": content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"\">" + self.html( type[1:]) + u"</a></p>" elif type[0] == "4": content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"?type=binhex\">" + self.html( type[1:]) + u"</a></p>" elif type[0] == "5": content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"?type=dos\">" + self.html( type[1:]) + u"</a></p>" elif type[0] == "6": content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"?type=uuencoded\">" + self.html( type[1:]) + u"</a></p>" elif type[0] == "9": content += u"<p><a href=\"gopher://" + host + u":" + port + path + u"?type=binary\">" + self.html( type[1:]) + u"</a></p>" elif type[0] == "g": content += u"<img src=\"gopher://" + host + u":" + port + path + u"?type=binary\">" + self.html( type[1:]) + u"</img>" elif type[0] == "I": content += u"<img src=\"gopher://" + host + u":" + port + path + u"?type=binary\">" + self.html( type[1:]) + u"</img>" content += (u"</body>\n" u"</html>\n") self.content = content.encode("utf-8") self.setHeader(QNetworkRequest.ContentTypeHeader, QVariant("text/html; charset=UTF-8")) self.setHeader(QNetworkRequest.ContentLengthHeader, QVariant(len(self.content))) self.readyRead.emit() self.finished.emit()
class FileDownloader(object): """The blueprint for downloading file from url.""" def __init__(self, url, output_path, progress_dialog=None): """Constructor of the class. .. versionchanged:: 3.3 removed manager parameter. :param url: URL of file. :type url: str :param output_path: Output path. :type output_path: str :param progress_dialog: Progress dialog widget. :type progress_dialog: QWidget """ # noinspection PyArgumentList self.manager = qgis.core.QgsNetworkAccessManager.instance() self.url = QUrl(url) self.output_path = output_path self.progress_dialog = progress_dialog if self.progress_dialog: self.prefix_text = self.progress_dialog.labelText() self.output_file = None self.reply = None self.downloaded_file_buffer = None self.finished_flag = False def download(self): """Downloading the file. :returns: True if success, otherwise returns a tuple with format like this (QNetworkReply.NetworkError, error_message) :raises: IOError - when cannot create output_path """ # Prepare output path self.output_file = QFile(self.output_path) if not self.output_file.open(QFile.WriteOnly): raise IOError(self.output_file.errorString()) # Prepare downloaded buffer self.downloaded_file_buffer = QByteArray() # Request the url request = QNetworkRequest(self.url) self.reply = self.manager.get(request) self.reply.readyRead.connect(self.get_buffer) self.reply.finished.connect(self.write_data) self.manager.requestTimedOut.connect(self.request_timeout) if self.progress_dialog: # progress bar def progress_event(received, total): """Update progress. :param received: Data received so far. :type received: int :param total: Total expected data. :type total: int """ # noinspection PyArgumentList QCoreApplication.processEvents() self.progress_dialog.adjustSize() human_received = humanize_file_size(received) human_total = humanize_file_size(total) label_text = tr( "%s : %s of %s" % (self.prefix_text, human_received, human_total)) self.progress_dialog.setLabelText(label_text) self.progress_dialog.setMaximum(total) self.progress_dialog.setValue(received) # cancel def cancel_action(): """Cancel download.""" self.reply.abort() self.reply.deleteLater() self.reply.downloadProgress.connect(progress_event) self.progress_dialog.canceled.connect(cancel_action) # Wait until finished # On Windows 32bit AND QGIS 2.2, self.reply.isFinished() always # returns False even after finished slot is called. So, that's why we # are adding self.finished_flag (see #864) while not self.reply.isFinished() and not self.finished_flag: # noinspection PyArgumentList QCoreApplication.processEvents() result = self.reply.error() try: http_code = int( self.reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)) except TypeError: # If the user cancels the request, the HTTP response will be None. http_code = None self.reply.abort() self.reply.deleteLater() if result == QNetworkReply.NoError: return True, None elif result == QNetworkReply.UnknownNetworkError: return False, tr( 'The network is unreachable. Please check your internet ' 'connection.') elif http_code == 408: msg = tr('Sorry, the server aborted your request. ' 'Please try a smaller area.') LOGGER.debug(msg) return False, msg elif http_code == 509: msg = tr( 'Sorry, the server is currently busy with another request. ' 'Please try again in a few minutes.') LOGGER.debug(msg) return False, msg elif result == QNetworkReply.ProtocolUnknownError or \ result == QNetworkReply.HostNotFoundError: LOGGER.exception('Host not found : %s' % self.url.encodedHost()) return False, tr( 'Sorry, the server is unreachable. Please try again later.') elif result == QNetworkReply.ContentNotFoundError: LOGGER.exception('Path not found : %s' % self.url.path()) return False, tr('Sorry, the layer was not found on the server.') else: return result, self.reply.errorString() def get_buffer(self): """Get buffer from self.reply and store it to our buffer container.""" buffer_size = self.reply.size() data = self.reply.read(buffer_size) self.downloaded_file_buffer.append(data) def write_data(self): """Write data to a file.""" self.output_file.write(self.downloaded_file_buffer) self.output_file.close() self.finished_flag = True def request_timeout(self): """The request timed out.""" if self.progress_dialog: self.progress_dialog.hide()
class Downloader(QDialog): " MultiThreaded GUI Downloader Dialog " def __init__(self, parent=None, url_to_download=None, fout=None): " init class " super(Downloader, self).__init__(parent) self.timer = time() self.httpGetId = 0 self.httpRequestAborted = False self.url = QUrl(url_to_download) self.fileInfo = QFileInfo(self.url.path()) self.fout = fout self.fileName = path.join(fout, str(self.fileInfo.fileName())) self.statusLabel = QLabel( "<center><h3>Downloading from:</h3>%s<h3>Saving to:</h3>%s</center>" % (url_to_download, fout) ) self.bytes = QLabel(" ??? ") self.closeButton = QPushButton(" Cancel ") self.closeButton.setAutoDefault(False) self.progressBar = QProgressBar() buttonBox = QDialogButtonBox() buttonBox.addButton(self.closeButton, QDialogButtonBox.RejectRole) self.http = QHttp(self) self.http.requestFinished.connect(self.httpRequestFinished) self.http.dataReadProgress.connect(self.updateDataReadProgress) self.http.responseHeaderReceived.connect(self.readResponseHeader) self.closeButton.clicked.connect(self.cancelDownload) mainLayout = QVBoxLayout() mainLayout.addWidget(self.statusLabel) mainLayout.addWidget(self.progressBar) mainLayout.addWidget(self.bytes) mainLayout.addWidget(buttonBox) self.setLayout(mainLayout) self.setWindowTitle(__doc__ + " - MultiThreaded Downloader ") self.resize(parent.size().width(), self.size().height()) self.downloadFile(url_to_download, fout) def downloadFile(self, url_to_download, fout): " get the file from remote url to local folder " url = QUrl(url_to_download) fileInfo = QFileInfo(url.path()) fileName = path.join(fout, str(fileInfo.fileName())) if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information( self, "ERROR", "Cant save the file %s: %s." % (fileName, self.outFile.errorString()) ) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False tpath = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if tpath: tpath = str(tpath) else: tpath = sep # Download the file. print(" INFO: Blender Download Started ! ") self.httpGetId = self.http.get(tpath, self.outFile) def cancelDownload(self): " abort the download " self.statusLabel.setText(" INFO: Download Canceled ! ") self.httpRequestAborted = True self.http.abort() self.close() def httpRequestFinished(self, requestId, error): " what to do when download is finised " if requestId != self.httpGetId: return if self.httpRequestAborted: if self.outFile is not None: self.outFile.close() self.outFile.remove() self.outFile = None return self.outFile.close() if error: self.outFile.remove() QMessageBox.information(self, "ERROR", "Download Failed: %s." % self.http.errorString()) self.statusLabel.setText(" <center> <h1> ✔ OK! ʘ‿ʘ </h1> <center> ") print(" INFO: Blender Download Finished ! ") if self.fileName.lower().endswith((".tar.gz", ".tgz", ".tar.bz2", ".tbz")): self.extract_file(self.fileName) def readResponseHeader(self, responseHeader): " Check for genuine error conditions " print(" INFO: HTTP Response is " + responseHeader.reasonPhrase()) print(" INFO: HTTP Status Code is " + responseHeader.statusCode()) if responseHeader.statusCode() not in (200, 300, 301, 302, 303, 307): QMessageBox.information(self, "ERROR", "Download Failed: %s." % responseHeader.reasonPhrase()) self.httpRequestAborted = True self.http.abort() def updateDataReadProgress(self, bytesRead, totalBytes): " update the GUI Dialog " if self.httpRequestAborted: return self.progressBar.setMaximum(totalBytes) self.progressBar.setValue(bytesRead) self.bytes.setText(" %s of %s Bytes on %s Minutes " % (bytesRead, totalBytes, int(time() - self.timer) / 60)) def extract_file(self, filepath): " extract the file " tarfile.debug = 3 zipfile.debug = 3 if filepath is None: return elif filepath.endswith(".zip"): opener, mode = zipfile.ZipFile, "r" print((" INFO: Extraction Mode is zipfile.ZipFile " + mode)) elif filepath.endswith(".tar.gz") or filepath.endswith(".tgz"): opener, mode = tarfile.open, "r:gz" print((" INFO: Extraction Mode is tarfile.open " + mode)) elif filepath.endswith(".tar.bz2") or filepath.endswith(".tbz"): opener, mode = tarfile.open, "r:bz2" print((" INFO: Extraction Mode is tarfile.open " + mode)) else: raise ValueError("Could not extract %s " % filepath) cwd = getcwd() chdir(self.fout) try: print((" INFO: Extracting Compressed file " + filepath)) filez = opener(filepath, mode) filez.extractall() filez.close() except: print((" ERROR: Could not extract %s " + filepath)) raise ValueError("Could not extract %s " % filepath) finally: print((" INFO: Finished Extraction of Compressed file " + filepath)) chdir(cwd) print(" INFO: Done. ")