Package pilas :: Module descargar
[hide private]
[frames] | no frames]

Source Code for Module pilas.descargar

  1  # -*- encoding: utf-8 -*- 
  2  # pilas engine - a video game framework. 
  3  # 
  4  # copyright 2010 - hugo ruscitti 
  5  # license: lgplv3 (see http://www.gnu.org/licenses/lgpl.html) 
  6  # 
  7  # website - http://www.pilas-engine.com.ar 
  8  # 
  9  # Creditos: http://stackoverflow.com/questions/6852038/threading-in-pyqt4 
 10   
 11  from PyQt4.QtCore import QUrl, QFile, QIODevice 
 12  from PyQt4.QtGui import QDialog, QProgressBar 
 13  from PyQt4.QtGui import QLabel, QPushButton, QDialogButtonBox, QVBoxLayout, QMessageBox 
 14  from PyQt4.QtNetwork import QHttp 
 15  import os 
 16   
 17   
18 -class Descargar(QDialog):
19
20 - def __init__(self, parent, url, archivo_destino):
21 super(Descargar, self).__init__(parent) 22 23 self.url = url 24 self.httpGetId = 0 25 self.httpRequestAborted = False 26 self.statusLabel = QLabel('Descargando el manual completo ...') 27 self.closeButton = QPushButton("Cerrar") 28 self.closeButton.setAutoDefault(False) 29 self.barra = QProgressBar() 30 31 buttonBox = QDialogButtonBox() 32 buttonBox.addButton(self.closeButton, QDialogButtonBox.RejectRole) 33 34 self.http = QHttp(self) 35 self.http.requestFinished.connect(self.cuando_finalizar_request) 36 self.http.dataReadProgress.connect(self.cuando_actualiza_descarga) 37 self.http.responseHeaderReceived.connect(self.cuando_responder_header) 38 self.closeButton.clicked.connect(self.cuando_cancela) 39 40 mainLayout = QVBoxLayout() 41 mainLayout.addWidget(self.statusLabel) 42 mainLayout.addWidget(self.barra) 43 mainLayout.addWidget(buttonBox) 44 self.setLayout(mainLayout) 45 46 self.setWindowTitle('Descargando manual') 47 self.downloadFile(url, archivo_destino)
48
49 - def downloadFile(self, url, archivo_destino):
50 url = QUrl(url) 51 fileName = archivo_destino 52 directorio_usuario_para_pilas = os.path.dirname(archivo_destino) 53 54 if not os.path.exists(directorio_usuario_para_pilas): 55 os.mkdir(directorio_usuario_para_pilas) 56 57 if QFile.exists(fileName): 58 QFile.remove(fileName) 59 60 self.outFile = QFile(fileName) 61 62 if not self.outFile.open(QIODevice.WriteOnly): 63 QMessageBox.information(self, 'Error', 'Lo siento, no se puede descargar el archivo desde %s: %s.' % (self.url, self.outFile.errorString())) 64 self.outFile = None 65 return 66 67 mode = QHttp.ConnectionModeHttp 68 port = url.port() 69 if port == -1: 70 port = 0 71 self.http.setHost(url.host(), mode, port) 72 self.httpRequestAborted = False 73 74 path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") 75 if path: 76 path = str(path) 77 else: 78 path = '/' 79 80 self.httpGetId = self.http.get(path, self.outFile)
81
82 - def cuando_cancela(self):
83 self.statusLabel.setText("Descarga cancelada") 84 self.httpRequestAborted = True 85 self.http.abort() 86 self.close()
87
88 - def cuando_finalizar_request(self, request_id, error):
89 if request_id != self.httpGetId: 90 return 91 92 if self.httpRequestAborted: 93 if self.outFile is not None: 94 self.outFile.close() 95 self.outFile.remove() 96 self.outFile = None 97 return 98 99 self.outFile.close() 100 101 if error: 102 self.outFile.remove() 103 QMessageBox.information(self, 'Error', u'Hay un error de conexión: %s.' % self.http.errorString()) 104 105 self.statusLabel.setText(u'Perfecto, ahora podrás explorar el manual.')
106
107 - def cuando_responder_header(self, responseHeader):
108 if responseHeader.statusCode() not in (200, 300, 301, 302, 303, 307): 109 mensaje = 'Ha fallado la descarga del archivo: \n"%s" \n%s.' %(self.url, responseHeader.reasonPhrase()) 110 QMessageBox.information(self, 'Error', mensaje) 111 112 self.httpRequestAborted = True 113 self.http.abort() 114 self.close()
115
116 - def cuando_actualiza_descarga(self, bytes_leidos, total_bytes):
117 if self.httpRequestAborted: 118 return 119 120 self.barra.setMaximum(total_bytes) 121 self.barra.setValue(bytes_leidos)
122