class TrickplayDeviceManager(QWidget): def __init__(self, main=None, parent = None): QWidget.__init__(self, parent) self.main = main self.inspector = main._inspector self.editorManager = main._editorManager self.debugWindow = main._debug self.backtraceWindow = main.backtrace self.ui = Ui_DeviceManager() self.ui.setupUi(self) self.addLocalComboItem() self.discovery = TrickplayDiscovery(self) QObject.connect(self.ui.comboBox, SIGNAL('currentIndexChanged(int)'), self.service_selected) QObject.connect(self.ui.run, SIGNAL("clicked()"), self.run) self._path = '' self.trickplay = QProcess() QObject.connect(self.trickplay, SIGNAL('started()'), self.app_started) #QObject.connect(self.trickplay, SIGNAL('finished(int, QProcess.ExitStatus)'), self.app_finished) QObject.connect(self.trickplay, SIGNAL('finished(int)'), self.app_finished) QObject.connect(self.trickplay, SIGNAL('readyRead()'), self.app_ready_read) self.icon = QIcon() self.icon.addPixmap(QPixmap(self.main.apath+"/Assets/icon-target.png"), QIcon.Normal, QIcon.Off) self.icon_null = QIcon() self.prev_index = 0 self.ui.comboBox.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.ui.comboBox.setIconSize(QSize(20,32)) self.debug_mode = False self.debug_run = False self.debug_port = None self.console_port = None self.http_port = None self.my_name = "" self.manager = QNetworkAccessManager() #self.reply = None #self.bs_command = False self.current_debug_file = None self.inbreak = True def service_selected(self, index): if index < 0: return self.ui.comboBox.setItemIcon(self.prev_index, self.icon_null) self.ui.comboBox.setItemIcon(index, self.icon) self.prev_index = index address = self.ui.comboBox.itemData(index, ADDRESS).toPyObject() port = self.ui.comboBox.itemData(index, PORT).toPyObject() self.debug_port = self.ui.comboBox.itemData(index, DEBUG_PORT).toPyObject() self.http_port = self.ui.comboBox.itemData(index, HTTP_PORT).toPyObject() self.console_port = self.ui.comboBox.itemData(index, CONSOLE_PORT).toPyObject() if not address or not port: return CON.port = port CON.address = address def event(self, event): QCoreApplication.setOrganizationName('Trickplay'); while 1: if event.type() == ADDEVENT: d = event.dict if d[0] != self.my_name: print "[VDBG] Service ' %s ' added"%d[0] # Add item to ComboBox self.ui.comboBox.addItem(d[0]) index = self.ui.comboBox.findText(d[0]) self.ui.comboBox.setItemData(index, d[1], ADDRESS) self.ui.comboBox.setItemData(index, d[2], PORT) self.ui.comboBox.setItemData(index, d[1], NAME) # Automatically select a service if only one exists if 1 == self.ui.comboBox.count(): self.service_selected(1) # index -> 1 data = getTrickplayControlData("%s:"%str(d[1])+"%s"%str(d[2])) if data is not None: if data.has_key("debugger"): self.ui.comboBox.setItemData(index, data["debugger"], DEBUG_PORT) print("[VDBG] debug Port : %s"%data["debugger"], d[0]) else : print("[VDBG] Didn't get %s's debug_port information "%d[0]) if data.has_key("http"): #self.http_port = data["http"] #print("[VDBG] http Port : %s"%self.http_port) self.ui.comboBox.setItemData(index, data["http"], HTTP_PORT) else : print("[VDBG] Didn't get %s's http_port information "%d[0]) if data.has_key("console"): self.console_port = data["console"] #print("[VDBG] console Port : %s"%self.console_port) self.ui.comboBox.setItemData(index, data["console"], CONSOLE_PORT) CON.port = self.http_port else : print("[VDBG] Didn't get %s's console_port information "%d[0]) else: print("[VDBG] Didn't get %s's Control information "%d[0]) elif event.type() == REMEVENT: d = event.dict print "[VDBG] Service ' %s ' removed"%d[0] # Remove item from ComboBox index = self.ui.comboBox.findText(d[0]) if index == self.ui.comboBox.currentIndex(): self.ui.comboBox.removeItem(index) self.ui.comboBox.setCurrentIndex(0) self.service_selected(0) else : self.ui.comboBox.removeItem(index) #self.inspector.clearTree() return True def stop(self): self.discovery.stop() def addLocalComboItem(self): """ Add combo box from running app locally. This always exists. """ name = 'Emulator' #'Trickplay Device ' port = '6789' address = 'localhost' icon = QIcon() icon.addPixmap(QPixmap(self.main.apath+"/Assets/icon-target.png"), QIcon.Normal, QIcon.Off) self.ui.comboBox.addItem(name) index = self.ui.comboBox.findText(name) self.ui.comboBox.setItemIcon(index, icon) self.ui.comboBox.setItemData(index, address, ADDRESS) self.ui.comboBox.setItemData(index, port, PORT) self.ui.comboBox.setItemData(index, address, NAME) CON.port = port CON.address = address def push(self): print('[VDBG] Pushing app to %s'%CON.get()) tp = TrickplayPushApp(self, str(self.path())) ret = tp.push(address = CON.get()) if ret is not False: self.app_started() return ret def setPath(self, p): self._path = p def path(self): return self._path def app_started(self): print "[VDBG] APP Started" self.newApp = True # Console Port self.socket = QTcpSocket() self.nextBlockSize = 0 self.bytesAvail = 0 self.connect(self.socket, SIGNAL("connected()"), self.sendRequest) self.connect(self.socket, SIGNAL("readyRead()"), self.readResponse) self.connect(self.socket, SIGNAL("disconnected()"), self.serverHasStopped) if self.socket.isOpen(): self.socket.close() if self.ui.comboBox.currentIndex() != 0: print("[VDBG] Connecting to console port ") cnt = 0 while cnt < 5: try : self.socket.connectToHost(CON.address, self.console_port, mode=QIODevice.ReadWrite) if self.socket.waitForConnected(100): self.newApp = True break except : cnt = cnt + 1 def sendRequest(self): print "Connected" def readDebugResponse(self): while self.debug_socket.waitForReadyRead(1100) : print self.debug_socket.read(self.debug_socket.bytesAvailable()) def readResponse(self): while self.socket.waitForReadyRead(1100) : EGN_MSG(self.socket.read(self.socket.bytesAvailable())[:-1].replace('\033[34;1m','').replace('\033[31;1m','').replace('\033[0m','').replace('\033[37m','').replace('\033[32m','')) def debugServerHasStopped(self): self.debug_socket.close() def serverHasStopped(self): print("Console port disconnected") self.socket.close() def serverHasError(self, error): print(QString("[VDBG] Error: %1").arg(self.socket.errorString())) self.socket.close() def app_ready_read(self): # Read all available output from the process while True: # Read one line if not self.trickplay.canReadLine(): break # Read one line s = self.trickplay.readLine() # If the line is null, it means there is nothing more # to read during this iteration if s.isNull(): break # Convert it to a string and strip the trailing white space s = str( s ).rstrip() # Look for the CONTROL line if s.startswith( "<<CONTROL>>:" ): try: # Parse the contents of the control line and get the # debugger port. control = json.loads( s[12:] ) # Store it. This could fail if the engine has no debugger # port. if control.has_key("debugger"): self.debug_port = control[ "debugger" ] if control.has_key("http"): self.http_port = control[ "http" ] if control.has_key("console"): self.console_port = control[ "console" ] self.ui.comboBox.setItemData(0, self.http_port, PORT) CON.port = self.http_port # Send our first debugger command, which will return # when the app breaks if self.debug_mode == True: self.inbreak = False self.send_debugger_command(DBG_CMD_INFO) if len(self.editorManager.bp_info[1]) > 0 : self.send_debugger_command(DBG_CMD_BB) else : self.inspector.ui.refresh.setEnabled(True) self.inspector.ui.search.setEnabled(True) except: print( "[VDBG] Failed to obtain debugger port" ) # Close the process self.trickplay.close() else: # Output the log line EGN_MSG(">> %s"%s.replace('\033[34;1m','').replace('\033[31;1m','').replace('\033[0m','').replace('\033[37m','').replace('\033[32m','')) def send_debugger_command(self, command): if self.debug_port is None: print "No debugger port" return url = QUrl() url.setScheme( "http" ) url.setHost( CON.address ) url.setPort( self.debug_port ) url.setPath( "/debugger" ) print ("[VDBG] ' %s ' Command Sent"%command) data = {} request = QNetworkRequest( url ) if command == "bb": data['clear'] = True data['add'] = [] bpCnt = len(self.editorManager.bp_info[1]) for r in range(0, bpCnt): bp_info = self.editorManager.bp_info[2][r] n = re.search(":", bp_info).end() fName = bp_info[:n-1] lNum = int(bp_info[n:]) -1 if self.editorManager.bp_info[1][r] == "on": bState = True else: bState = False #data['add'].append({'file':fName, 'line':lNum, 'on':bState}) bp_item = {} bp_item['file'] = fName bp_item['line'] = lNum + 1 bp_item['on'] = bState data['add'].append(bp_item) params = json.dumps(data) reply = self.manager.post( request ,command+' '+params) reply.command = command else: reply = self.manager.post( request , command ) reply.command = command def debugger_reply_finished(reply): def foo (): if reply.error()== QNetworkReply.NoError: print("[VDBG] ' %s ' Response"%reply.command) if reply.command == "bn": return if reply.command == "r": self.main.deviceManager.socket.write('/close\n\n') self.main.rSent = False if self.main.onExit == True: if self.editorManager.tab != None: while self.editorManager.tab.count() != 0: self.editorManager.close() self.stop() self.main.close() data = self.getFileLineInfo_Resp(str(reply.readAll()), command) if data is not None: if reply.command == DBG_CMD_INFO: self.inbreak = True self.inspector.ui.refresh.setEnabled(False) self.inspector.ui.search.setEnabled(False) # Open File, Show Current Lines if self.file_name.startswith("/"): self.file_name= self.file_name[1:] self.file_name= self.file_name+'/' if self.file_name.endswith("/"): self.file_name= self.file_name[:len(self.file_name) - 1] current_file = os.path.join(str(self.main.path), str(self.file_name)) if self.current_debug_file != current_file: self.editorManager.newEditor(current_file, None, self.line_no, self.current_debug_file, True) else : self.editorManager.newEditor(current_file, None, self.line_no, None, True) self.current_debug_file = current_file # Local Variable Table local_info = self.getLocalInfo_Resp(data) if local_info is not None: self.debugWindow.populateLocalTable(local_info) # Global Variable Table global_info = self.getGlobalInfo_Resp(data) if global_info is not None: self.debugWindow.populateGlobalTable(global_info, self.editorManager) # Stack Trace Table stack_info = self.getStackInfo_Resp(data) if stack_info is not None: self.backtraceWindow.populateTraceTable(stack_info, self.editorManager) #reply = None #reply.command = None # TODO: Here we should enable the debug UI self.main.debug_stepinto.setEnabled(True) self.main.debug_stepover.setEnabled(True) self.main.debug_stepout.setEnabled(False) self.main.debug_pause_bt.setEnabled(False) self.main.debug_continue_bt.setEnabled(True) self.main.ui.actionContinue.setEnabled(True) self.main.ui.actionPause.setEnabled(False) self.main.ui.actionStep_into.setEnabled(True) self.main.ui.actionStep_over.setEnabled(True) self.main.ui.actionStep_out.setEnabled(False) self.main.debug_run = False elif reply.command[:1] == DBG_CMD_BREAKPOINT or reply.command == DBG_CMD_BB or reply.command[:1] == DBG_CMD_DELETE: # Break Point break_info = self.getBreakPointInfo_Resp(data) if break_info is not None: self.debugWindow.populateBreakTable(break_info, self.editorManager) if reply.command == DBG_CMD_BB : return editor = self.editorManager.app.focusWidget() if editor is not None : nline = editor.margin_nline else: index = self.editorManager.tab.currentIndex() editor = self.editorManager.tab.editors[index] nline = editor.margin_nline if reply.command[:1] == DBG_CMD_DELETE and nline is not None: if editor.current_line != nline : editor.markerDelete(nline, -1) else : editor.markerDelete(nline, -1) editor.markerAdd(nline, editor.ARROW_MARKER_NUM) editor.line_click[nline] = 0 return elif nline is None: return # Break Point Setting t if not editor.line_click.has_key(nline) or editor.line_click[nline] == 0 : if editor.current_line != nline : editor.markerAdd(nline, editor.ACTIVE_BREAK_MARKER_NUM) else: editor.markerDelete(nline, editor.ARROW_MARKER_NUM) editor.markerAdd(nline, editor.ARROW_ACTIVE_BREAK_MARKER_NUM) editor.line_click[nline] = 1 # Break Point Deactivate elif editor.line_click[nline] == 1: if editor.current_line != nline : editor.markerDelete(nline, editor.ACTIVE_BREAK_MARKER_NUM) editor.markerAdd(nline, editor.DEACTIVE_BREAK_MARKER_NUM) else : editor.markerDelete(nline, editor.ARROW_ACTIVE_BREAK_MARKER_NUM) editor.markerAdd(nline, editor.ARROW_DEACTIVE_BREAK_MARKER_NUM) editor.line_click[nline] = 2 # Break Point Active elif editor.line_click[nline] == 2: if editor.current_line != nline : editor.markerDelete(nline, editor.DEACTIVE_BREAK_MARKER_NUM) editor.markerAdd(nline, editor.ACTIVE_BREAK_MARKER_NUM) else : editor.markerDelete(nline, editor.ARROW_DEACTIVE_BREAK_MARKER_NUM) editor.markerAdd(nline, editor.ARROW_ACTIVE_BREAK_MARKER_NUM) editor.line_click[nline] = 1 #reply = None #self.command = None if reply.command in DBG_ADVANCE_COMMANDS: if reply.command == DBG_CMD_CONTINUE : # delete current line marker for m in self.editorManager.editors: if self.current_debug_file == m: # check what kind of arrow marker was on the current line and delete just arrow mark not break points self.editorManager.tab.editors[self.editorManager.editors[m][1]].markerDelete( self.editorManager.tab.editors[self.editorManager.editors[m][1]].current_line, Editor.ARROW_MARKER_NUM) self.editorManager.tab.editors[self.editorManager.editors[m][1]].current_line = -1 # clean backtrace and debug windows self.backtraceWindow.clearTraceTable(0) self.debugWindow.clearLocalTable(0) self.debugWindow.clearGlobalTable(0) # Leave the debug UI disabled, and wait for the info command to return self.send_debugger_command( DBG_CMD_INFO ) return foo f=debugger_reply_finished(reply) QObject.connect( reply , SIGNAL( 'finished()' ) , f) reply.command = command[:] if command in DBG_ADVANCE_COMMANDS: # TODO: Here we should disable the debug UI self.main.debug_stepinto.setEnabled(False) self.main.debug_stepover.setEnabled(False) self.main.debug_stepout.setEnabled(False) self.main.debug_pause_bt.setEnabled(True) self.main.debug_continue_bt.setEnabled(False) self.main.ui.actionContinue.setEnabled(False) self.main.ui.actionPause.setEnabled(True) self.main.ui.actionStep_into.setEnabled(False) self.main.ui.actionStep_over.setEnabled(False) self.main.ui.actionStep_out.setEnabled(False) self.main.debug_run = True return True def app_finished(self, errorCode): if errorCode == 0 : print ("[VDBG] Error Code : ["+str(errorCode)+", FailedToStart] the process failed to start. Either the invoked program is missing, or you may have insufficient permissions to invoke the program" ) elif errorCode == 1 : print ("[VDBG] Error Code : ["+str(errorCode)+", Crashed] The process crashed some time after starting successfully.") elif errorCode == 2 : print ("[VDBG] Error Code : ["+str(errorCode)+", Timedout] The process crashed some time after starting successfully.") elif errorCode == 3 : print ("[VDBG] Error Code : ["+str(errorCode)+", ReadError] An error occurred when attempting to read from the process. For example, the process may not be running.") elif errorCode == 4 : print ("[VDBG] Error Code : ["+str(errorCode)+", WriteError] An error occurred when attempting to write to the process. For example, the process may not be running, or it may have closed its input channel.") elif errorCode == 5 : print ("[VDBG] Error Code : ["+str(errorCode)+", UnknownError] An unknown error occurred.") if self.trickplay.state() == QProcess.NotRunning : print "[VDBG] Trickplay APP is finished" if self.trickplay.exitStatus() == QProcess.NormalExit : print ("[VDBG] ExitStatus : The process exited normally.") elif self.trickplay.exitStatus() == QProcess.CrashExit : print ("[VDBG] ExitStatus : The process crashed.") if self.main.closedByIDE == False : msg = QMessageBox() msg.setText("The process crashed.") msg.setInformativeText('ErrorCode : [ '+str(errorCode)+' ]') msg.setWindowTitle("Warning") msg.setGeometry(500,500,0,0) msg.exec_() self.inspector.clearTree() self.inspector.ui.refresh.setEnabled(False) self.inspector.ui.search.setEnabled(False) self.main.stop() def run(self, dMode=False): # Run on local trickplay if 0 == self.ui.comboBox.currentIndex(): print("[VDBG] Starting trickplay locally") if self.trickplay.state() == QProcess.Running: self.trickplay.close() env = self.trickplay.processEnvironment().systemEnvironment() if self.main.config is None : print("[VDBG] .trickplay config file is ignored.") for item in env.toStringList(): if item[:3] == "TP_": n = re.search("=", item).end() env.remove(item[:n-1]) env.insert("TP_config_file","") else: print("[VDBG] .trickplay config file is read.") env.insert("TP_LOG", "bare") if dMode == True : self.debug_mode = True self.main.debug_mode = True env.insert("TP_start_debugger","true") else : self.debug_mode = False self.main.debug_mode = False # To merge stdout and stderr self.trickplay.setProcessChannelMode( QProcess.MergedChannels ) self.trickplay.setProcessEnvironment(env) ret = self.trickplay.start('trickplay', [self.path()]) # Push to remote device else: if dMode == True: if self.debug_port is None: print("[VDBG] Debug port is missing") return False # POST http://<host>:<debugger port>/debugger "r" #url = QUrl() #url.setScheme( "http" ) #url.setHost( CON.address ) #url.setPort( self.debug_port ) #url.setPath( "/debugger" ) #print ("[VDBG] ' %s ' Command Sent"%'r') #request = QNetworkRequest( url ) #self.manager.post( request , 'r' ) self.debug_mode = True self.main.debug_mode = True ret = self.push() if ret == False: return ret elif dMode == True: self.inbreak = False self.send_debugger_command(DBG_CMD_INFO) if len(self.editorManager.bp_info[1]) > 0 : self.send_debugger_command(DBG_CMD_BB) else : self.inspector.ui.refresh.setEnabled(True) self.inspector.ui.search.setEnabled(True) def getFileLineInfo_Resp(self, data, command): pdata = json.loads(data) if command == "i": if "error" in pdata: print "[VDBG] "+pdata["error"] return None else: file_name = pdata["file"] tp_id = pdata["id"] line_num = pdata["line"] self.line_no = str(line_num) self.file_name = str(file_name) return pdata else: return pdata def getGlobalInfo_Resp(self, data): if "globals" in data: name_var_list = [] type_var_list = [] value_var_list = [] defined_var_list = [] global_vars_str = "" global_vars = {} for c in data["globals"]: if c["name"] != "(*temporary)": c_v = None if global_vars_str != "": global_vars_str = global_vars_str+"\n\t" global_vars_str = global_vars_str+str(c["name"])+"("+str(c["type"])+")" name_var_list.append(str(c["name"])) type_var_list.append(str(c["type"])) defined_var_list.append(str(c["defined"])) try: c_v = c["value"] except KeyError: pass if c_v: global_vars_str = global_vars_str+" = "+str(c["value"]) value_var_list.append(str(c["value"])) else: value_var_list.append("") global_vars[1] = name_var_list global_vars[2] = type_var_list global_vars[3] = value_var_list global_vars[4] = defined_var_list return global_vars else: return None def getLocalInfo_Resp(self, data): if "locals" in data: name_var_list = [] type_var_list = [] value_var_list = [] local_vars_str = "" local_vars = {} for c in data["locals"]: if c["name"] != "(*temporary)": c_v = None if local_vars_str != "": local_vars_str = local_vars_str+"\n\t" local_vars_str = local_vars_str+str(c["name"])+"("+str(c["type"])+")" name_var_list.append(str(c["name"])) type_var_list.append(str(c["type"])) try: c_v = c["value"] except KeyError: pass if c_v: local_vars_str = local_vars_str+" = "+str(c["value"]) value_var_list.append(str(c["value"])) else: value_var_list.append("") local_vars[1] = name_var_list local_vars[2] = type_var_list local_vars[3] = value_var_list return local_vars else: return None def getStackInfo_Resp(self, data): if "stack" in data: stack_info_str = "" stack_list = [] info_list = [] stack_info = {} index = 0 for s in data["stack"]: if "file" in s and "line" in s: stack_info_str = stack_info_str+"["+str(index)+"] "+s["file"]+":"+str(s["line"])+"\n\t" stack_list.append("["+str(index)+"] "+s["file"]+":"+str(s["line"])) #info_list.append(str(self.path())+'/'+s["file"]+":"+str(s["line"])) eChar = "" if s["file"][:1] != "/" : eChar = "/" info_list.append(str(self.path())+eChar+s["file"]+":"+str(s["line"])) index = index + 1 stack_info[1] = stack_list stack_info[2] = info_list #print "\t"+stack_info_str return stack_info else: return None def getBreakPointInfo_Resp(self, data): if "breakpoints" in data: state_var_list = [] file_var_list = [] breakpoints_info = {} breakpoints_info_str = "" index = 0 if len(data["breakpoints"]) == 0: print "[VDBG] No breakpoints set" return breakpoints_info else: for b in data["breakpoints"]: if "file" in b and "line" in b: breakpoints_info_str = breakpoints_info_str+"["+str(index)+"] "+b["file"]+":"+str(b["line"]) file_var_list.append(b["file"]+":"+str(b["line"])) if "on" in b: if b["on"] == True: breakpoints_info_str = breakpoints_info_str+""+"\n " state_var_list.append("on") else: breakpoints_info_str = breakpoints_info_str+" (disabled)"+"\n " state_var_list.append("off") index = index + 1 breakpoints_info[1] = state_var_list breakpoints_info[2] = file_var_list print "[VDBG] "+breakpoints_info_str return breakpoints_info else: return None def printResp(self, data, command): if "source" in data: source_info = "" for l in data["source"]: if "line" in l and "text" in l: if l["line"] == line_num: source_info = source_info+str(l["line"])+" >>"+str(l["text"])+"\n\t" else: source_info = source_info+str(l["line"])+" "+str(l["text"])+"\n\t" print "\t"+source_info elif "lines" in data: fetched_lines = "" for l in data["lines"]: fetched_lines = fetched_lines+l+"\n\t" print "\t"+fetched_lines elif "app" in data: app_info = "" for key in data["app"].keys(): if key != "contents": app_info = app_info+str(key)+" : "+str(data["app"][key])+"\n\t" else: app_info = app_info+key+" : " for c in data["app"]["contents"]: app_info = app_info + str(c) + "," app_info = app_info+"\n\t" print "\t"+app_info if command in ['n','s','bn', 'cn']: #print "\t"+"Break at "+file_name+":"+str(line_num) pass return True
class ActivityProtocol(QObject): # SIGNAL that is emitted after activities have been read readSignal = pyqtSignal(bool, int, QString) # SIGNAL that is emitted after a new activity has been added created = pyqtSignal(bool, int, QString) # SIGNAL that is emitted after an existing activity has been updated updated = pyqtSignal(bool, int, QString) # SIGNAL that is emitted after an activity has been deleted deleted = pyqtSignal(bool, int, QString) # SIGNAL that is emitted after activities have been counted counted = pyqtSignal(bool, int, QString) def __init__(self, host, user, password): QObject.__init__(self) # Set the host self.host = host # Create a base64 encoded credential string from user name and password. # This is required for the HTTP basic access authentication, compare # also http://en.wikipedia.org/wiki/Basic_access_authentication self.userlogin = "******" % (user, password) self.login = "******" + QByteArray(self.userlogin).toBase64() # Create a new QNetworkAccessManager and connect it to the # authenticationRequired signal self.manager = QNetworkAccessManager(self) # self.connect(self.manager, SIGNAL("authenticationRequired( QNetworkReply*, QAuthenticator* )"), self.slotAuthenticationRequired) def update(self, rawBody): """ Update an activity using a POST request """ self.connect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.updateRequestFinished) url = "%s/activities" % self.host qurl = QUrl(url) self.request = QNetworkRequest(qurl) self.request.setRawHeader("Authorization", self.login) self.request.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") self.manager.post(self.request, QString(json.dumps(rawBody)).toUtf8()) return url def updateRequestFinished(self, reply): self.disconnect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.updateRequestFinished) # Get the HTTP status code from the reply self.httpStatusCode = int(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute).toString()) # httpReasonPhrase = reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute).toString() data = reply.readAll() self.updated.emit(self.httpStatusCode in (200, 201), self.httpStatusCode, QString(data)) def read(self, extent): self.connect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.readRequestFinished) # Limit the longitude and latitutde maximum boundaries xmin = extent.xMinimum() if extent.xMinimum() >= -180 else -180 ymin = extent.yMinimum() if extent.yMinimum() >= -90 else -90 xmax = extent.xMaximum() if extent.xMaximum() <= 180 else 180 ymax = extent.yMaximum() if extent.yMaximum() <= 90 else 90 url = "%s/activities/json?bbox=%d,%d,%d,%d" % (self.host, xmin, ymin, xmax, ymax) qUrl = QUrl(url) self.request = QNetworkRequest(qUrl) self.request.setRawHeader("Authorization", self.login) self.request.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") self.manager.get(self.request) return url def readRequestFinished(self, reply): # Get the HTTP status code from the reply self.httpStatusCode = int(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute).toString()) # data = str("a") data = reply.readAll() # Check the status code see also http://en.wikipedia.org/wiki/HTTP_status_code # In case of a successful upload we get a 201 status code back and the # "stylePosted" signal is emitted with the first parameter set to True. # If the query didn't succeed, the status code is 4xx indicating that # the host was not found, the authentication failed or a forbidden # action in case a style with the same name already exists. It's up to # the receiver to handle these status codes. self.readSignal.emit(self.httpStatusCode in (200, 201), self.httpStatusCode, QString(data)) def readById(self, id): """ """ self.connect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.readByIdRequestFinished) # Get the latest version of the activity with this id url = "%s/activities/json/%s?geometry=full" % (self.host, id) qUrl = QUrl(url) self.request = QNetworkRequest(qUrl) # self.request.setRawHeader("Authorization", self.login) self.manager.get(self.request) return url def readByIdRequestFinished(self, reply): # Get the HTTP status code from the reply self.httpStatusCode = int(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute).toString()) data = reply.readAll() self.readSignal.emit(self.httpStatusCode in (200, 201), self.httpStatusCode, QString(data))
class StravaUpload(QObject): LOGIN = '******' ATHLETES_SHOW = 'http://www.strava.com/api/v2/athletes/{id}' UPLOAD = 'http://www.strava.com/api/v2/upload' UPLOAD_STATUS = 'http://www.strava.com/api/v2/upload/status/{id}' statusMessage = pyqtSignal(str) totalProgress = pyqtSignal(int) itemProgress = pyqtSignal(int) authNeeded = pyqtSignal() finished = pyqtSignal() def __init__(self, tracks, auth_token=None, device_info=None, parent=None): super(StravaUpload, self).__init__(parent) self.device_info = device_info self.auth_token = auth_token self.tracks = tracks[:] self.current_track = None self.results = [] self.progress = 0 self.upload_id = None self._aborted = False self.reply = None self.network_manager = QNetworkAccessManager(self) def start(self): self._doAuthenticate() def cancel(self): self._aborted = True if self.reply is not None: self.reply.abort() def authenticate(self, username, password): log.debug('Sending auth request') req = QNetworkRequest(QUrl(self.LOGIN)) req.setHeader(QNetworkRequest.ContentTypeHeader, "application/x-www-form-urlencoded") self.reply = self.network_manager.post(req, urllib.urlencode({ 'email' : username, 'password' : password, })) self.reply.finished.connect(self._onAuthenticated) def _emitProgress(self, msg, value): self.progressString.emit(msg) self.progress.emit(value) def _doAuthenticate(self): self.statusMessage.emit('Authenticating') if self.auth_token is None: log.debug('Auth needed') self.authNeeded.emit() else: self._uploadNext() def _onAuthenticated(self): data = self._loadJson(str(self.reply.readAll())) self.reply = None if data is None: log.debug('Auth request failed (response: %s)', data) self._doAuthenticate() return if 'error' in data: log.debug('Auth request failed (response: %s)', data['error']) self._doAuthenticate() elif 'token' in data: log.debug('Auth OK') self.auth_token = data['token'] self._uploadNext() def _uploadNext(self): if self._aborted: return log.debug('Uploading next') self.itemProgress.emit(0) self.totalProgress.emit(self.progress) if not self.tracks: log.debug('Finished') self.finished.emit() return self.current_track = self.tracks.pop(0) self.progress += 1 self._doUpload() def _doUpload(self): if self.current_track is None or self._aborted: return track, track_type = self.current_track self.statusMessage.emit('Uploading {0}'.format(track['name'])) log.debug('Sending upload request (%s)', track['name']) # data_fields, data = bryton_gpx_to_strava_json(track['gpx']) data = tcx.bryton_gpx_to_tcx(track['gpx'], activity_type=track_type, pretty=False, device=self.device_info) req = QNetworkRequest(QUrl(self.UPLOAD)) req.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") # d2 = json.loads(open('tmp/test.json').read()) # d2['token'] = self.auth_token # self.reply = self.network_manager.post(req, json.dumps(d2)) self.reply = self.network_manager.post(req, json.dumps({ 'token' : self.auth_token, 'type' : 'tcx', 'data' : data, 'activity_type' : track_type, })) self.reply.finished.connect(self._onUploaded) def _onUploaded(self): data = self._loadJson(str(self.reply.readAll())) self.reply = None if data is None: log.debug('Upload failed (response: %s)', data) self._uploadFailed('Unknown error') elif 'error' in data: log.debug('Upload failed (%s)', data['error']) self._uploadFailed(data['error']) else: log.debug('Upload OK (%s)', data['upload_id']) self.upload_id = data['upload_id'] self.statusMessage.emit('Checking upload status') QTimer.singleShot(2000, self._checkUpload) # self._progress += 1 # self.tracks.pop(0) # self.progress.emit(self._progress) # self.upload() def _uploadFailed(self, msg): self.results.append({ 'status' : 'ERROR', 'msg' : 'Failed: ' + msg, }) self._uploadNext() def _uploadOk(self): self.results.append({ 'status' : 'OK', 'msg': 'Successfully uploaded' }) self._uploadNext() def _checkUpload(self): if self._aborted: return log.debug('Sending upload status request (%s)', self.upload_id) url = QUrl(self.UPLOAD_STATUS.format(id=self.upload_id)) url.addQueryItem('token', self.auth_token) req = QNetworkRequest(url) self.reply = self.network_manager.get(req) self.reply.finished.connect(self._onUploadStatus) def _onUploadStatus(self): data = self._loadJson(str(self.reply.readAll())) self.reply = None if data is None: log.debug('Upload status failed (response: %s)', data) self._uploadFailed('Unknown error') elif 'upload_error' in data: log.debug('Upload status failed (%s)', data['upload_error']) self.statusMessage.emit('Upload failed') self._uploadFailed(data['upload_error']) else: self.statusMessage.emit(data['upload_status']) log.debug('Upload status %d (%s)', self.upload_id, data['upload_progress']) progress = int(data['upload_progress']) if progress == 0: progress = 10 # Just add a little to the progress so it doesn't look stuck self.itemProgress.emit(progress) if progress == 100: self._uploadOk() else: QTimer.singleShot(2500, self._checkUpload) def _loadJson(self, data): try: return json.loads(data) except ValueError, e: return None
class CartoDBApi(QObject): fetchContent = pyqtSignal(object) progress = pyqtSignal(int, int) error = pyqtSignal(object) def __init__(self, cartodbUser, apiKey, multiuser=False, hostname='cartonico.datapy.info'): QObject.__init__(self) self.multiuser = multiuser self.apiKey = apiKey self.cartodbUser = cartodbUser self.hostname = hostname self.apiUrl = "https://{}.{}/api/v1/".format(cartodbUser, hostname) self.returnDict = True self.manager = QNetworkAccessManager() self.manager.finished.connect(self.returnFetchContent) def _getRequest(self, url): request = QNetworkRequest(url) request.setRawHeader("Content-Type", "application/json") request.setRawHeader('User-Agent', 'QGISCartoDB 0.2.x') return request def _createMultipart(self, data={}, files={}): multiPart = QHttpMultiPart(QHttpMultiPart.FormDataType) for key, value in data.items(): textPart = QHttpPart() textPart.setHeader(QNetworkRequest.ContentDispositionHeader, "form-data; name=\"%s\"" % key) textPart.setBody(value) multiPart.append(textPart) for key, file in files.items(): filePart = QHttpPart() # filePart.setHeader(QNetworkRequest::ContentTypeHeader, ...); fileName = QFileInfo(file.fileName()).fileName() filePart.setHeader( QNetworkRequest.ContentDispositionHeader, "form-data; name=\"%s\"; filename=\"%s\"" % (key, fileName)) filePart.setBodyDevice(file) multiPart.append(filePart) return multiPart def getUserDetails(self, returnDict=True): self.returnDict = returnDict url = QUrl( self.apiUrl + "users/{}/?api_key={}".format(self.cartodbUser, self.apiKey)) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def getUserTables(self, page=1, per_page=20, shared='yes', returnDict=True): self.returnDict = returnDict payload = { 'tag_name': '', 'q': '', 'page': page, 'type': '', 'exclude_shared': 'false', 'per_page': per_page, 'tags': '', 'shared': shared, 'locked': 'false', 'only_liked': 'false', 'order': 'name', 'types': 'table' } url = QUrl( self.apiUrl + "viz?api_key={}&{}".format(self.apiKey, urllib.urlencode(payload))) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def getDataFromTable(self, sql, returnDict=True): self.returnDict = returnDict apiUrl = 'http://{}.{}/api/v2/sql?api_key={}&format=GeoJSON&q={}'.format( self.cartodbUser, self.hostname, self.apiKey, sql) url = QUrl(apiUrl) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def download(self, sql): apiUrl = 'http://{}.{}/api/v2/sql?api_key={}&format=spatialite&q={}'.format( self.cartodbUser, self.hostname, self.apiKey, sql) url = QUrl(apiUrl) request = self._getRequest(url) def finished(reply): tempdir = tempfile.tempdir if tempdir is None: tempdir = tempfile.mkdtemp() tf = tempfile.NamedTemporaryFile(delete=False) sqlite = QFile(tf.name) tf.close() if (sqlite.open(QIODevice.WriteOnly)): sqlite.write(reply.readAll()) sqlite.close() self.fetchContent.emit(tf.name) else: self.error.emit('Error saving downloaded file') manager = QNetworkAccessManager() manager.finished.connect(finished) reply = manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def upload(self, filePath, returnDict=True): self.returnDict = returnDict file = QFile(filePath) file.open(QFile.ReadOnly) url = QUrl(self.apiUrl + "imports/?api_key={}".format(self.apiKey)) files = {'file': file} multipart = self._createMultipart(files=files) request = QNetworkRequest(url) request.setHeader( QNetworkRequest.ContentTypeHeader, 'multipart/form-data; boundary=%s' % multipart.boundary()) request.setRawHeader('User-Agent', 'QGISCartoDB 0.2.x') reply = self.manager.post(request, multipart) loop = QEventLoop() reply.uploadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def checkUploadStatus(self, id, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "imports/{}/?api_key={}".format(id, self.apiKey)) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def createVizFromTable(self, table, name, description='', returnDict=True): self.returnDict = returnDict payload = { 'type': 'derived', 'name': name, 'title': name, 'description': description, 'tags': ['QGISCartoDB'], "tables": [table] } url = QUrl(self.apiUrl + "viz/?api_key={}".format(self.apiKey)) request = self._getRequest(url) reply = self.manager.post(request, json.dumps(payload)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def updateViz(self, viz, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "viz/{}?api_key={}".format(viz['id'], self.apiKey)) request = self._getRequest(url) reply = self.manager.put(request, json.dumps(viz)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def addLayerToMap(self, mapId, layer, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "maps/{}/layers?api_key={}".format(mapId, self.apiKey)) request = self._getRequest(url) reply = self.manager.post(request, json.dumps(layer)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def updateLayerInMap(self, mapId, layer, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "maps/{}/layers/{}?api_key={}".format( mapId, layer['id'], self.apiKey)) request = self._getRequest(url) reply = self.manager.put(request, json.dumps(layer)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def getLayersMap(self, mapId, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "maps/{}/layers?api_key={}".format(mapId, self.apiKey)) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def progressCB(self, breceived, btotal): self.progress.emit(breceived, btotal) def returnFetchContent(self, reply): response = str(reply.readAll()) # qDebug('Response:' + response) # qDebug('Error: ' + str(reply.error())) # qDebug('Status: ' + str(reply.rawHeader('Location'))) if reply.rawHeader('Location') == 'http://cartodb.com/noneuser.html' or \ reply.rawHeader('Location') == 'http://carto.com/noneuser.html': response = '{"error": "User not found"}' elif reply.error() == QNetworkReply.AuthenticationRequiredError: response = '{"error": "Confirm user credentials"}' if self.returnDict: try: self.fetchContent.emit(json.loads(response)) except ValueError as e: qDebug('Error loading json. {}'.format(response)) response = '{"error": "Error loading JSON data"}' self.fetchContent.emit(json.loads(response)) else: self.fetchContent.emit(response) def _error(self, error): qDebug('Error: ' + str(error)) self.error.emit(error)
class StakeholderProtocol(QObject): # SIGNAL that is emitted after stakeholders have been read readSignal = pyqtSignal(bool, int, QString) # SIGNAL that is emitted after a new stakeholder has been added created = pyqtSignal(bool, int, QString) # SIGNAL that is emitted after an existing activity has been updated updated = pyqtSignal(bool, int, QString) # SIGNAL that is emitted after an activity has been deleted deleted = pyqtSignal(bool, int, QString) # SIGNAL that is emitted after activities have been counted counted = pyqtSignal(bool, int, QString) def __init__(self, host, user, password): QObject.__init__(self) # Set the host self.host = host # Create a base64 encoded credential string from user name and password. # This is required for the HTTP basic access authentication, compare # also http://en.wikipedia.org/wiki/Basic_access_authentication self.userlogin = "******" % (user, password) self.login = "******" + QByteArray(self.userlogin).toBase64() # Create a new QNetworkAccessManager and connect it to the # authenticationRequired signal self.manager = QNetworkAccessManager(self) # self.connect(self.manager, SIGNAL("authenticationRequired( QNetworkReply*, QAuthenticator* )"), self.slotAuthenticationRequired) def read(self, **kwargs): self.connect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.readRequestFinished) params = [] try: queryable = kwargs["queryable"] params.append({"queryable": queryable}) queryableList = QString(queryable).split(",") except KeyError: pass for key, value in kwargs.items(): if QString(key).split("__")[0] in queryableList: params.append({key: value}) url = "%s/stakeholders?" % self.host for p in params: url = "%s%s=%s&" % (url, p.keys()[0], p.values()[0]) qUrl = QUrl(url) # Create a new request request = QNetworkRequest(qUrl) request.setRawHeader("Authorization", self.login) request.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") self.manager.get(request) return url def readRequestFinished(self, reply): # Get the HTTP status code from the reply self.disconnect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.readRequestFinished) httpStatusCode = int(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute).toString()) # Check the status code see also http://en.wikipedia.org/wiki/HTTP_status_code # In case of a successful upload we get a 201 status code back and the # "stylePosted" signal is emitted with the first parameter set to True. # If the query didn't succeed, the status code is 4xx indicating that # the host was not found, the authentication failed or a forbidden # action in case a style with the same name already exists. It's up to # the receiver to handle these status codes. self.readSignal.emit(httpStatusCode in (200, 201), httpStatusCode, QString(reply.readAll())) def add(self, stakeholder): self.connect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.readRequestFinished) url = "%s/stakeholders" % self.host qurl = QUrl(url) # Create a new request request = QNetworkRequest(qurl) request.setRawHeader("Authorization", self.login) request.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") wrapperObj = {} if len(stakeholder) > 0: wrapperObj["stakeholders"] = [s.createDiff(None) for s in stakeholder] else: wrapperObj["stakeholders"] = [stakeholder.createDiff(None)] rawBody = json.dumps(wrapperObj, sort_keys=True, indent=4 * " ") self.manager.post(request, rawBody) return url, rawBody def addRequestFinished(self, reply): self.disconnect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.readRequestFinished) httpStatusCode = int(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute).toString()) self.created.emit(httpStatusCode in (200, 201), httpStatusCode, QString(reply.readAll()))
class CartoDBApi(QObject): fetchContent = pyqtSignal(object) progress = pyqtSignal(int, int) error = pyqtSignal(object) def __init__(self, cartodbUser, apiKey, multiuser=False, hostname="cartodb.com"): QObject.__init__(self) self.multiuser = multiuser self.apiKey = apiKey self.cartodbUser = cartodbUser self.hostname = hostname self.apiUrl = "https://{}.{}/api/v1/".format(cartodbUser, hostname) self.returnDict = True self.manager = QNetworkAccessManager() self.manager.finished.connect(self.returnFetchContent) def _getRequest(self, url): request = QNetworkRequest(url) request.setRawHeader("Content-Type", "application/json") request.setRawHeader("User-Agent", "QGISCartoDB 0.2.x") return request def _createMultipart(self, data={}, files={}): multiPart = QHttpMultiPart(QHttpMultiPart.FormDataType) for key, value in data.items(): textPart = QHttpPart() textPart.setHeader(QNetworkRequest.ContentDispositionHeader, 'form-data; name="%s"' % key) textPart.setBody(value) multiPart.append(textPart) for key, file in files.items(): filePart = QHttpPart() # filePart.setHeader(QNetworkRequest::ContentTypeHeader, ...); fileName = QFileInfo(file.fileName()).fileName() filePart.setHeader( QNetworkRequest.ContentDispositionHeader, 'form-data; name="%s"; filename="%s"' % (key, fileName) ) filePart.setBodyDevice(file) multiPart.append(filePart) return multiPart def getUserDetails(self, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "users/{}/?api_key={}".format(self.cartodbUser, self.apiKey)) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def getUserTables(self, page=1, per_page=20, shared="yes", returnDict=True): self.returnDict = returnDict payload = { "tag_name": "", "q": "", "page": page, "type": "", "exclude_shared": "false", "per_page": per_page, "tags": "", "shared": shared, "locked": "false", "only_liked": "false", "order": "name", "types": "table", } url = QUrl(self.apiUrl + "viz?api_key={}&{}".format(self.apiKey, urllib.urlencode(payload))) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def getDataFromTable(self, sql, returnDict=True): self.returnDict = returnDict apiUrl = "http://{}.cartodb.com/api/v2/sql?api_key={}&format=GeoJSON&q={}".format( self.cartodbUser, self.apiKey, sql ) url = QUrl(apiUrl) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def download(self, sql): apiUrl = "http://{}.cartodb.com/api/v2/sql?api_key={}&format=spatialite&q={}".format( self.cartodbUser, self.apiKey, sql ) url = QUrl(apiUrl) request = self._getRequest(url) def finished(reply): tempdir = tempfile.tempdir if tempdir is None: tempdir = tempfile.mkdtemp() tf = tempfile.NamedTemporaryFile(delete=False) sqlite = QFile(tf.name) tf.close() if sqlite.open(QIODevice.WriteOnly): sqlite.write(reply.readAll()) sqlite.close() self.fetchContent.emit(tf.name) else: self.error.emit("Error saving downloaded file") manager = QNetworkAccessManager() manager.finished.connect(finished) reply = manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def upload(self, filePath, returnDict=True): self.returnDict = returnDict file = QFile(filePath) file.open(QFile.ReadOnly) url = QUrl(self.apiUrl + "imports/?api_key={}".format(self.apiKey)) files = {"file": file} multipart = self._createMultipart(files=files) request = QNetworkRequest(url) request.setHeader(QNetworkRequest.ContentTypeHeader, "multipart/form-data; boundary=%s" % multipart.boundary()) request.setRawHeader("User-Agent", "QGISCartoDB 0.2.x") reply = self.manager.post(request, multipart) loop = QEventLoop() reply.uploadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def checkUploadStatus(self, id, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "imports/{}/?api_key={}".format(id, self.apiKey)) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def createVizFromTable(self, table, name, description="", returnDict=True): self.returnDict = returnDict payload = { "type": "derived", "name": name, "title": name, "description": description, "tags": ["QGISCartoDB"], "tables": [table], } url = QUrl(self.apiUrl + "viz/?api_key={}".format(self.apiKey)) request = self._getRequest(url) reply = self.manager.post(request, json.dumps(payload)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def updateViz(self, viz, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "viz/{}?api_key={}".format(viz["id"], self.apiKey)) request = self._getRequest(url) reply = self.manager.put(request, json.dumps(viz)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def addLayerToMap(self, mapId, layer, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "maps/{}/layers?api_key={}".format(mapId, self.apiKey)) request = self._getRequest(url) reply = self.manager.post(request, json.dumps(layer)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def updateLayerInMap(self, mapId, layer, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "maps/{}/layers/{}?api_key={}".format(mapId, layer["id"], self.apiKey)) request = self._getRequest(url) reply = self.manager.put(request, json.dumps(layer)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def getLayersMap(self, mapId, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "maps/{}/layers?api_key={}".format(mapId, self.apiKey)) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_() def progressCB(self, breceived, btotal): self.progress.emit(breceived, btotal) def returnFetchContent(self, reply): response = str(reply.readAll()) # qDebug('Response:' + response) # qDebug('Error: ' + str(reply.error())) # qDebug('Status: ' + str(reply.rawHeader('Location'))) if reply.rawHeader("Location") == "http://cartodb.com/noneuser.html": response = '{"error": "User not found"}' elif reply.error() == QNetworkReply.AuthenticationRequiredError: response = '{"error": "Confirm user credentials"}' if self.returnDict: try: self.fetchContent.emit(json.loads(response)) except ValueError as e: qDebug("Error loading json. {}".format(response)) response = '{"error": "Error loading JSON data"}' self.fetchContent.emit(json.loads(response)) else: self.fetchContent.emit(response) def _error(self, error): qDebug("Error: " + str(error)) self.error.emit(error)
newFile.open(QIODevice.WriteOnly) newFile.write(reply.readAll()) newFile.close() print("done") reply.deleteLater() def read_data(reply): messageBuffer += reply.readAll() # ---- Main ---------------------------------------------------- # print(dir(manager)) #manager.finished.connect( handle_download ) # with a simple web page #url = "https://www.nextinpact.com/news/103770-google-annonce-android-o-liste-nouveautes-developer-preview-disponible.htm" #request_download(manager, url) # with an authenticated API request req_token = ask_for_token(api_id, api_secret) token_reply = manager.post(req_token[0], req_token[1]) #token_reply.finished.connect( # partial(self.handle_token, answer=token_reply)) token_reply.finished.connect(partial(self.handle_token, answer=token_reply)) bytarray = token_reply.readAll() print("bbibit", bytarray, dir(bytarray)) content = str(bytarray) parsed_content = json.loads(QByteArray.fromBase64(bytarray)) print(parsed_content)
class TrickplayDeviceManager(QWidget): def __init__(self, main=None, parent=None): QWidget.__init__(self, parent) self.main = main self.inspector = main._inspector self.editorManager = main._editorManager self.debugWindow = main._debug self.backtraceWindow = main.backtrace self.ui = Ui_DeviceManager() self.ui.setupUi(self) self.addLocalComboItem() self.discovery = TrickplayDiscovery(self) QObject.connect(self.ui.comboBox, SIGNAL('currentIndexChanged(int)'), self.service_selected) QObject.connect(self.ui.run, SIGNAL("clicked()"), self.run) self._path = '' self.trickplay = QProcess() QObject.connect(self.trickplay, SIGNAL('started()'), self.app_started) #QObject.connect(self.trickplay, SIGNAL('finished(int, QProcess.ExitStatus)'), self.app_finished) QObject.connect(self.trickplay, SIGNAL('finished(int)'), self.app_finished) QObject.connect(self.trickplay, SIGNAL('readyRead()'), self.app_ready_read) self.icon = QIcon() self.icon.addPixmap( QPixmap(self.main.apath + "/Assets/icon-target.png"), QIcon.Normal, QIcon.Off) self.icon_null = QIcon() self.prev_index = 0 self.ui.comboBox.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.ui.comboBox.setIconSize(QSize(20, 32)) self.debug_mode = False self.debug_run = False self.debug_port = None self.console_port = None self.http_port = None self.my_name = "" self.manager = QNetworkAccessManager() #self.reply = None #self.bs_command = False self.current_debug_file = None self.inbreak = True def service_selected(self, index): if index < 0: return self.ui.comboBox.setItemIcon(self.prev_index, self.icon_null) self.ui.comboBox.setItemIcon(index, self.icon) self.prev_index = index address = self.ui.comboBox.itemData(index, ADDRESS).toPyObject() port = self.ui.comboBox.itemData(index, PORT).toPyObject() self.debug_port = self.ui.comboBox.itemData(index, DEBUG_PORT).toPyObject() self.http_port = self.ui.comboBox.itemData(index, HTTP_PORT).toPyObject() self.console_port = self.ui.comboBox.itemData( index, CONSOLE_PORT).toPyObject() if not address or not port: return CON.port = port CON.address = address def event(self, event): QCoreApplication.setOrganizationName('Trickplay') while 1: if event.type() == ADDEVENT: d = event.dict if d[0] != self.my_name: print "[VDBG] Service ' %s ' added" % d[0] # Add item to ComboBox self.ui.comboBox.addItem(d[0]) index = self.ui.comboBox.findText(d[0]) self.ui.comboBox.setItemData(index, d[1], ADDRESS) self.ui.comboBox.setItemData(index, d[2], PORT) self.ui.comboBox.setItemData(index, d[1], NAME) # Automatically select a service if only one exists if 1 == self.ui.comboBox.count(): self.service_selected(1) # index -> 1 data = getTrickplayControlData("%s:" % str(d[1]) + "%s" % str(d[2])) if data is not None: if data.has_key("debugger"): self.ui.comboBox.setItemData( index, data["debugger"], DEBUG_PORT) print("[VDBG] debug Port : %s" % data["debugger"], d[0]) else: print( "[VDBG] Didn't get %s's debug_port information " % d[0]) if data.has_key("http"): #self.http_port = data["http"] #print("[VDBG] http Port : %s"%self.http_port) self.ui.comboBox.setItemData( index, data["http"], HTTP_PORT) else: print( "[VDBG] Didn't get %s's http_port information " % d[0]) if data.has_key("console"): self.console_port = data["console"] #print("[VDBG] console Port : %s"%self.console_port) self.ui.comboBox.setItemData( index, data["console"], CONSOLE_PORT) CON.port = self.http_port else: print( "[VDBG] Didn't get %s's console_port information " % d[0]) else: print("[VDBG] Didn't get %s's Control information " % d[0]) elif event.type() == REMEVENT: d = event.dict print "[VDBG] Service ' %s ' removed" % d[0] # Remove item from ComboBox index = self.ui.comboBox.findText(d[0]) if index == self.ui.comboBox.currentIndex(): self.ui.comboBox.removeItem(index) self.ui.comboBox.setCurrentIndex(0) self.service_selected(0) else: self.ui.comboBox.removeItem(index) #self.inspector.clearTree() return True def stop(self): self.discovery.stop() def addLocalComboItem(self): """ Add combo box from running app locally. This always exists. """ name = 'Emulator' #'Trickplay Device ' port = '6789' address = 'localhost' icon = QIcon() icon.addPixmap(QPixmap(self.main.apath + "/Assets/icon-target.png"), QIcon.Normal, QIcon.Off) self.ui.comboBox.addItem(name) index = self.ui.comboBox.findText(name) self.ui.comboBox.setItemIcon(index, icon) self.ui.comboBox.setItemData(index, address, ADDRESS) self.ui.comboBox.setItemData(index, port, PORT) self.ui.comboBox.setItemData(index, address, NAME) CON.port = port CON.address = address def push(self): print('[VDBG] Pushing app to %s' % CON.get()) tp = TrickplayPushApp(self, str(self.path())) ret = tp.push(address=CON.get()) if ret is not False: self.app_started() return ret def setPath(self, p): self._path = p def path(self): return self._path def app_started(self): print "[VDBG] APP Started" self.newApp = True # Console Port self.socket = QTcpSocket() self.nextBlockSize = 0 self.bytesAvail = 0 self.connect(self.socket, SIGNAL("connected()"), self.sendRequest) self.connect(self.socket, SIGNAL("readyRead()"), self.readResponse) self.connect(self.socket, SIGNAL("disconnected()"), self.serverHasStopped) if self.socket.isOpen(): self.socket.close() if self.ui.comboBox.currentIndex() != 0: print("[VDBG] Connecting to console port ") cnt = 0 while cnt < 5: try: self.socket.connectToHost(CON.address, self.console_port, mode=QIODevice.ReadWrite) if self.socket.waitForConnected(100): self.newApp = True break except: cnt = cnt + 1 def sendRequest(self): print "Connected" def readDebugResponse(self): while self.debug_socket.waitForReadyRead(1100): print self.debug_socket.read(self.debug_socket.bytesAvailable()) def readResponse(self): while self.socket.waitForReadyRead(1100): EGN_MSG( self.socket.read(self.socket.bytesAvailable())[:-1].replace( '\033[34;1m', '').replace('\033[31;1m', '').replace( '\033[0m', '').replace('\033[37m', '').replace('\033[32m', '')) def debugServerHasStopped(self): self.debug_socket.close() def serverHasStopped(self): print("Console port disconnected") self.socket.close() def serverHasError(self, error): print(QString("[VDBG] Error: %1").arg(self.socket.errorString())) self.socket.close() def app_ready_read(self): # Read all available output from the process while True: # Read one line if not self.trickplay.canReadLine(): break # Read one line s = self.trickplay.readLine() # If the line is null, it means there is nothing more # to read during this iteration if s.isNull(): break # Convert it to a string and strip the trailing white space s = str(s).rstrip() # Look for the CONTROL line if s.startswith("<<CONTROL>>:"): try: # Parse the contents of the control line and get the # debugger port. control = json.loads(s[12:]) # Store it. This could fail if the engine has no debugger # port. if control.has_key("debugger"): self.debug_port = control["debugger"] if control.has_key("http"): self.http_port = control["http"] if control.has_key("console"): self.console_port = control["console"] self.ui.comboBox.setItemData(0, self.http_port, PORT) CON.port = self.http_port # Send our first debugger command, which will return # when the app breaks if self.debug_mode == True: self.inbreak = False self.send_debugger_command(DBG_CMD_INFO) if len(self.editorManager.bp_info[1]) > 0: self.send_debugger_command(DBG_CMD_BB) else: self.inspector.ui.refresh.setEnabled(True) self.inspector.ui.search.setEnabled(True) except: print("[VDBG] Failed to obtain debugger port") # Close the process self.trickplay.close() else: # Output the log line EGN_MSG(">> %s" % s.replace('\033[34;1m', '').replace( '\033[31;1m', '').replace('\033[0m', '').replace( '\033[37m', '').replace('\033[32m', '')) def send_debugger_command(self, command): if self.debug_port is None: print "No debugger port" return url = QUrl() url.setScheme("http") url.setHost(CON.address) url.setPort(self.debug_port) url.setPath("/debugger") print("[VDBG] ' %s ' Command Sent" % command) data = {} request = QNetworkRequest(url) if command == "bb": data['clear'] = True data['add'] = [] bpCnt = len(self.editorManager.bp_info[1]) for r in range(0, bpCnt): bp_info = self.editorManager.bp_info[2][r] n = re.search(":", bp_info).end() fName = bp_info[:n - 1] lNum = int(bp_info[n:]) - 1 if self.editorManager.bp_info[1][r] == "on": bState = True else: bState = False #data['add'].append({'file':fName, 'line':lNum, 'on':bState}) bp_item = {} bp_item['file'] = fName bp_item['line'] = lNum + 1 bp_item['on'] = bState data['add'].append(bp_item) params = json.dumps(data) reply = self.manager.post(request, command + ' ' + params) reply.command = command else: reply = self.manager.post(request, command) reply.command = command def debugger_reply_finished(reply): def foo(): if reply.error() == QNetworkReply.NoError: print("[VDBG] ' %s ' Response" % reply.command) if reply.command == "bn": return if reply.command == "r": self.main.deviceManager.socket.write('/close\n\n') self.main.rSent = False if self.main.onExit == True: if self.editorManager.tab != None: while self.editorManager.tab.count() != 0: self.editorManager.close() self.stop() self.main.close() data = self.getFileLineInfo_Resp(str(reply.readAll()), command) if data is not None: if reply.command == DBG_CMD_INFO: self.inbreak = True self.inspector.ui.refresh.setEnabled(False) self.inspector.ui.search.setEnabled(False) # Open File, Show Current Lines if self.file_name.startswith("/"): self.file_name = self.file_name[1:] self.file_name = self.file_name + '/' if self.file_name.endswith("/"): self.file_name = self.file_name[:len( self.file_name) - 1] current_file = os.path.join( str(self.main.path), str(self.file_name)) if self.current_debug_file != current_file: self.editorManager.newEditor( current_file, None, self.line_no, self.current_debug_file, True) else: self.editorManager.newEditor( current_file, None, self.line_no, None, True) self.current_debug_file = current_file # Local Variable Table local_info = self.getLocalInfo_Resp(data) if local_info is not None: self.debugWindow.populateLocalTable(local_info) # Global Variable Table global_info = self.getGlobalInfo_Resp(data) if global_info is not None: self.debugWindow.populateGlobalTable( global_info, self.editorManager) # Stack Trace Table stack_info = self.getStackInfo_Resp(data) if stack_info is not None: self.backtraceWindow.populateTraceTable( stack_info, self.editorManager) #reply = None #reply.command = None # TODO: Here we should enable the debug UI self.main.debug_stepinto.setEnabled(True) self.main.debug_stepover.setEnabled(True) self.main.debug_stepout.setEnabled(False) self.main.debug_pause_bt.setEnabled(False) self.main.debug_continue_bt.setEnabled(True) self.main.ui.actionContinue.setEnabled(True) self.main.ui.actionPause.setEnabled(False) self.main.ui.actionStep_into.setEnabled(True) self.main.ui.actionStep_over.setEnabled(True) self.main.ui.actionStep_out.setEnabled(False) self.main.debug_run = False elif reply.command[: 1] == DBG_CMD_BREAKPOINT or reply.command == DBG_CMD_BB or reply.command[: 1] == DBG_CMD_DELETE: # Break Point break_info = self.getBreakPointInfo_Resp(data) if break_info is not None: self.debugWindow.populateBreakTable( break_info, self.editorManager) if reply.command == DBG_CMD_BB: return editor = self.editorManager.app.focusWidget() if editor is not None: nline = editor.margin_nline else: index = self.editorManager.tab.currentIndex() editor = self.editorManager.tab.editors[index] nline = editor.margin_nline if reply.command[: 1] == DBG_CMD_DELETE and nline is not None: if editor.current_line != nline: editor.markerDelete(nline, -1) else: editor.markerDelete(nline, -1) editor.markerAdd(nline, editor.ARROW_MARKER_NUM) editor.line_click[nline] = 0 return elif nline is None: return # Break Point Setting t if not editor.line_click.has_key( nline) or editor.line_click[nline] == 0: if editor.current_line != nline: editor.markerAdd( nline, editor.ACTIVE_BREAK_MARKER_NUM) else: editor.markerDelete( nline, editor.ARROW_MARKER_NUM) editor.markerAdd( nline, editor.ARROW_ACTIVE_BREAK_MARKER_NUM) editor.line_click[nline] = 1 # Break Point Deactivate elif editor.line_click[nline] == 1: if editor.current_line != nline: editor.markerDelete( nline, editor.ACTIVE_BREAK_MARKER_NUM) editor.markerAdd( nline, editor.DEACTIVE_BREAK_MARKER_NUM) else: editor.markerDelete( nline, editor.ARROW_ACTIVE_BREAK_MARKER_NUM) editor.markerAdd( nline, editor.ARROW_DEACTIVE_BREAK_MARKER_NUM) editor.line_click[nline] = 2 # Break Point Active elif editor.line_click[nline] == 2: if editor.current_line != nline: editor.markerDelete( nline, editor.DEACTIVE_BREAK_MARKER_NUM) editor.markerAdd( nline, editor.ACTIVE_BREAK_MARKER_NUM) else: editor.markerDelete( nline, editor.ARROW_DEACTIVE_BREAK_MARKER_NUM) editor.markerAdd( nline, editor.ARROW_ACTIVE_BREAK_MARKER_NUM) editor.line_click[nline] = 1 #reply = None #self.command = None if reply.command in DBG_ADVANCE_COMMANDS: if reply.command == DBG_CMD_CONTINUE: # delete current line marker for m in self.editorManager.editors: if self.current_debug_file == m: # check what kind of arrow marker was on the current line and delete just arrow mark not break points self.editorManager.tab.editors[ self.editorManager.editors[m] [1]].markerDelete( self.editorManager.tab.editors[ self.editorManager.editors[m] [1]].current_line, Editor.ARROW_MARKER_NUM) self.editorManager.tab.editors[ self.editorManager.editors[m] [1]].current_line = -1 # clean backtrace and debug windows self.backtraceWindow.clearTraceTable(0) self.debugWindow.clearLocalTable(0) self.debugWindow.clearGlobalTable(0) # Leave the debug UI disabled, and wait for the info command to return self.send_debugger_command(DBG_CMD_INFO) return foo f = debugger_reply_finished(reply) QObject.connect(reply, SIGNAL('finished()'), f) reply.command = command[:] if command in DBG_ADVANCE_COMMANDS: # TODO: Here we should disable the debug UI self.main.debug_stepinto.setEnabled(False) self.main.debug_stepover.setEnabled(False) self.main.debug_stepout.setEnabled(False) self.main.debug_pause_bt.setEnabled(True) self.main.debug_continue_bt.setEnabled(False) self.main.ui.actionContinue.setEnabled(False) self.main.ui.actionPause.setEnabled(True) self.main.ui.actionStep_into.setEnabled(False) self.main.ui.actionStep_over.setEnabled(False) self.main.ui.actionStep_out.setEnabled(False) self.main.debug_run = True return True def app_finished(self, errorCode): if errorCode == 0: print( "[VDBG] Error Code : [" + str(errorCode) + ", FailedToStart] the process failed to start. Either the invoked program is missing, or you may have insufficient permissions to invoke the program" ) elif errorCode == 1: print( "[VDBG] Error Code : [" + str(errorCode) + ", Crashed] The process crashed some time after starting successfully." ) elif errorCode == 2: print( "[VDBG] Error Code : [" + str(errorCode) + ", Timedout] The process crashed some time after starting successfully." ) elif errorCode == 3: print( "[VDBG] Error Code : [" + str(errorCode) + ", ReadError] An error occurred when attempting to read from the process. For example, the process may not be running." ) elif errorCode == 4: print( "[VDBG] Error Code : [" + str(errorCode) + ", WriteError] An error occurred when attempting to write to the process. For example, the process may not be running, or it may have closed its input channel." ) elif errorCode == 5: print("[VDBG] Error Code : [" + str(errorCode) + ", UnknownError] An unknown error occurred.") if self.trickplay.state() == QProcess.NotRunning: print "[VDBG] Trickplay APP is finished" if self.trickplay.exitStatus() == QProcess.NormalExit: print("[VDBG] ExitStatus : The process exited normally.") elif self.trickplay.exitStatus() == QProcess.CrashExit: print("[VDBG] ExitStatus : The process crashed.") if self.main.closedByIDE == False: msg = QMessageBox() msg.setText("The process crashed.") msg.setInformativeText('ErrorCode : [ ' + str(errorCode) + ' ]') msg.setWindowTitle("Warning") msg.setGeometry(500, 500, 0, 0) msg.exec_() self.inspector.clearTree() self.inspector.ui.refresh.setEnabled(False) self.inspector.ui.search.setEnabled(False) self.main.stop() def run(self, dMode=False): # Run on local trickplay if 0 == self.ui.comboBox.currentIndex(): print("[VDBG] Starting trickplay locally") if self.trickplay.state() == QProcess.Running: self.trickplay.close() env = self.trickplay.processEnvironment().systemEnvironment() if self.main.config is None: print("[VDBG] .trickplay config file is ignored.") for item in env.toStringList(): if item[:3] == "TP_": n = re.search("=", item).end() env.remove(item[:n - 1]) env.insert("TP_config_file", "") else: print("[VDBG] .trickplay config file is read.") env.insert("TP_LOG", "bare") if dMode == True: self.debug_mode = True self.main.debug_mode = True env.insert("TP_start_debugger", "true") else: self.debug_mode = False self.main.debug_mode = False # To merge stdout and stderr self.trickplay.setProcessChannelMode(QProcess.MergedChannels) self.trickplay.setProcessEnvironment(env) ret = self.trickplay.start('trickplay', [self.path()]) # Push to remote device else: if dMode == True: if self.debug_port is None: print("[VDBG] Debug port is missing") return False # POST http://<host>:<debugger port>/debugger "r" #url = QUrl() #url.setScheme( "http" ) #url.setHost( CON.address ) #url.setPort( self.debug_port ) #url.setPath( "/debugger" ) #print ("[VDBG] ' %s ' Command Sent"%'r') #request = QNetworkRequest( url ) #self.manager.post( request , 'r' ) self.debug_mode = True self.main.debug_mode = True ret = self.push() if ret == False: return ret elif dMode == True: self.inbreak = False self.send_debugger_command(DBG_CMD_INFO) if len(self.editorManager.bp_info[1]) > 0: self.send_debugger_command(DBG_CMD_BB) else: self.inspector.ui.refresh.setEnabled(True) self.inspector.ui.search.setEnabled(True) def getFileLineInfo_Resp(self, data, command): pdata = json.loads(data) if command == "i": if "error" in pdata: print "[VDBG] " + pdata["error"] return None else: file_name = pdata["file"] tp_id = pdata["id"] line_num = pdata["line"] self.line_no = str(line_num) self.file_name = str(file_name) return pdata else: return pdata def getGlobalInfo_Resp(self, data): if "globals" in data: name_var_list = [] type_var_list = [] value_var_list = [] defined_var_list = [] global_vars_str = "" global_vars = {} for c in data["globals"]: if c["name"] != "(*temporary)": c_v = None if global_vars_str != "": global_vars_str = global_vars_str + "\n\t" global_vars_str = global_vars_str + str( c["name"]) + "(" + str(c["type"]) + ")" name_var_list.append(str(c["name"])) type_var_list.append(str(c["type"])) defined_var_list.append(str(c["defined"])) try: c_v = c["value"] except KeyError: pass if c_v: global_vars_str = global_vars_str + " = " + str( c["value"]) value_var_list.append(str(c["value"])) else: value_var_list.append("") global_vars[1] = name_var_list global_vars[2] = type_var_list global_vars[3] = value_var_list global_vars[4] = defined_var_list return global_vars else: return None def getLocalInfo_Resp(self, data): if "locals" in data: name_var_list = [] type_var_list = [] value_var_list = [] local_vars_str = "" local_vars = {} for c in data["locals"]: if c["name"] != "(*temporary)": c_v = None if local_vars_str != "": local_vars_str = local_vars_str + "\n\t" local_vars_str = local_vars_str + str( c["name"]) + "(" + str(c["type"]) + ")" name_var_list.append(str(c["name"])) type_var_list.append(str(c["type"])) try: c_v = c["value"] except KeyError: pass if c_v: local_vars_str = local_vars_str + " = " + str( c["value"]) value_var_list.append(str(c["value"])) else: value_var_list.append("") local_vars[1] = name_var_list local_vars[2] = type_var_list local_vars[3] = value_var_list return local_vars else: return None def getStackInfo_Resp(self, data): if "stack" in data: stack_info_str = "" stack_list = [] info_list = [] stack_info = {} index = 0 for s in data["stack"]: if "file" in s and "line" in s: stack_info_str = stack_info_str + "[" + str( index) + "] " + s["file"] + ":" + str( s["line"]) + "\n\t" stack_list.append("[" + str(index) + "] " + s["file"] + ":" + str(s["line"])) #info_list.append(str(self.path())+'/'+s["file"]+":"+str(s["line"])) eChar = "" if s["file"][:1] != "/": eChar = "/" info_list.append( str(self.path()) + eChar + s["file"] + ":" + str(s["line"])) index = index + 1 stack_info[1] = stack_list stack_info[2] = info_list #print "\t"+stack_info_str return stack_info else: return None def getBreakPointInfo_Resp(self, data): if "breakpoints" in data: state_var_list = [] file_var_list = [] breakpoints_info = {} breakpoints_info_str = "" index = 0 if len(data["breakpoints"]) == 0: print "[VDBG] No breakpoints set" return breakpoints_info else: for b in data["breakpoints"]: if "file" in b and "line" in b: breakpoints_info_str = breakpoints_info_str + "[" + str( index) + "] " + b["file"] + ":" + str(b["line"]) file_var_list.append(b["file"] + ":" + str(b["line"])) if "on" in b: if b["on"] == True: breakpoints_info_str = breakpoints_info_str + "" + "\n " state_var_list.append("on") else: breakpoints_info_str = breakpoints_info_str + " (disabled)" + "\n " state_var_list.append("off") index = index + 1 breakpoints_info[1] = state_var_list breakpoints_info[2] = file_var_list print "[VDBG] " + breakpoints_info_str return breakpoints_info else: return None def printResp(self, data, command): if "source" in data: source_info = "" for l in data["source"]: if "line" in l and "text" in l: if l["line"] == line_num: source_info = source_info + str( l["line"]) + " >>" + str(l["text"]) + "\n\t" else: source_info = source_info + str( l["line"]) + " " + str(l["text"]) + "\n\t" print "\t" + source_info elif "lines" in data: fetched_lines = "" for l in data["lines"]: fetched_lines = fetched_lines + l + "\n\t" print "\t" + fetched_lines elif "app" in data: app_info = "" for key in data["app"].keys(): if key != "contents": app_info = app_info + str(key) + " : " + str( data["app"][key]) + "\n\t" else: app_info = app_info + key + " : " for c in data["app"]["contents"]: app_info = app_info + str(c) + "," app_info = app_info + "\n\t" print "\t" + app_info if command in ['n', 's', 'bn', 'cn']: #print "\t"+"Break at "+file_name+":"+str(line_num) pass return True
class BrytonSportUpload(QObject): LOGIN = '******' authNeeded = pyqtSignal() statusMessage = pyqtSignal(str) totalProgress = pyqtSignal(int) finished = pyqtSignal() failed = pyqtSignal() def __init__(self, bbclient, parent, session_id=None): super(BrytonSportUpload, self).__init__(parent) self.bbclient = bbclient self.session_id = session_id self.network_manager = QNetworkAccessManager(self) self.bbclient.uploadFailed.connect(self.failed) self.bbclient.uploadFinished.connect(self.finished) self.bbclient.uploadProgress.connect(self.totalProgress) def start(self): self._doAuthenticate() def authenticate(self, username, password): req = QNetworkRequest(QUrl(self.LOGIN)) req.setHeader(QNetworkRequest.ContentTypeHeader, "application/x-www-form-urlencoded") log.debug('Sending auth request') self.reply = self.network_manager.post(req, urllib.urlencode({ 'data[account]' : username, 'data[passwd]' : password, })) self.reply.finished.connect(self._onAuthenticated) def _doAuthenticate(self): if self.session_id is not None: self._doUpload(self.session_id) return log.debug('Auth needed') self.statusMessage.emit('Authenticating') self.authNeeded.emit() def _onAuthenticated(self): data = self._loadJson(str(self.reply.readAll())) if data is None or not 'User' in data: log.debug('Auth failed (response: %s)', data) self._doAuthenticate() return log.debug('Auth OK') ses = data['User']['SessionId'] self._doUpload(ses) def _doUpload(self, session_id): self.statusMessage.emit('Uploading history') self.bbclient.uploadToBrytonSport(session_id) def _loadJson(self, data): try: return json.loads(data) except ValueError, e: return None
newFile.open(QIODevice.WriteOnly) newFile.write(reply.readAll()) newFile.close() print("done") reply.deleteLater() def read_data(reply): messageBuffer += reply.readAll() # ---- Main ---------------------------------------------------- # print(dir(manager)) #manager.finished.connect( handle_download ) # with a simple web page #url = "https://www.nextinpact.com/news/103770-google-annonce-android-o-liste-nouveautes-developer-preview-disponible.htm" #request_download(manager, url) # with an authenticated API request req_token = ask_for_token(api_id, api_secret) token_reply = manager.post(req_token[0], req_token[1]) #token_reply.finished.connect( # partial(self.handle_token, answer=token_reply)) token_reply.finished.connect( partial(self.handle_token, answer=token_reply)) bytarray = token_reply.readAll() print("bbibit", bytarray, dir(bytarray)) content = str(bytarray) parsed_content = json.loads(QByteArray.fromBase64(bytarray)) print(parsed_content)
class CartoDBApi(QObject): fetchContent = pyqtSignal(object) progress = pyqtSignal(int, int) def __init__(self, cartodbUser, apiKey, multiuser=False, hostname='cartodb.com'): QObject.__init__(self) self.multiuser = multiuser self.apiKey = apiKey self.cartodbUser = cartodbUser self.hostname = hostname self.apiUrl = "https://{}.{}/api/v1/".format(cartodbUser, hostname) self.manager = QNetworkAccessManager() self.manager.finished.connect(self.returnFetchContent) def _getRequest(self, url): request = QNetworkRequest(url) request.setRawHeader("Content-Type", "application/json") request.setRawHeader('User-Agent', 'QGIS 2.x') return request def _createMultipart(self, data={}, files={}): multiPart = QHttpMultiPart(QHttpMultiPart.FormDataType) for key, value in data.items(): textPart = QHttpPart() textPart.setHeader(QNetworkRequest.ContentDispositionHeader, "form-data; name=\"%s\"" % key) textPart.setBody(value) multiPart.append(textPart) for key, file in files.items(): filePart = QHttpPart() # filePart.setHeader(QNetworkRequest::ContentTypeHeader, ...); fileName = QFileInfo(file.fileName()).fileName() filePart.setHeader(QNetworkRequest.ContentDispositionHeader, "form-data; name=\"%s\"; filename=\"%s\"" % (key, fileName)) filePart.setBodyDevice(file) multiPart.append(filePart) return multiPart def getUserDetails(self, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "users/{}/?api_key={}".format(self.cartodbUser, self.apiKey)) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self.error) reply.finished.connect(loop.exit) loop.exec_() def getUserTables(self, page=1, per_page=20, shared='yes', returnDict=True): self.returnDict = returnDict payload = { 'tag_name': '', 'q': '', 'page': page, 'type': '', 'exclude_shared': 'false', 'per_page': per_page, 'tags': '', 'shared': shared, 'locked': 'false', 'only_liked': 'false', 'order': 'name', 'types': 'table' } url = QUrl(self.apiUrl + "viz?api_key={}&{}".format(self.apiKey, urllib.urlencode(payload))) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self.error) reply.finished.connect(loop.exit) loop.exec_() def getDataFromTable(self, sql, returnDict=True): self.returnDict = returnDict apiUrl = 'http://{}.cartodb.com/api/v2/sql?api_key={}&format=GeoJSON&q={}'.format(self.cartodbUser, self.apiKey, sql) url = QUrl(apiUrl) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self.error) reply.finished.connect(loop.exit) loop.exec_() def upload(self, filePath, returnDict=True): self.returnDict = returnDict file = QFile(filePath) file.open(QFile.ReadOnly) apiUrl = 'https://{}.cartodb.com/api/v1/imports/?api_key={}'.format(self.cartodbUser, self.apiKey) url = QUrl(apiUrl) files = {'file': file} multipart = self._createMultipart(files=files) request = QNetworkRequest(url) request.setHeader(QNetworkRequest.ContentTypeHeader, 'multipart/form-data; boundary=%s' % multipart.boundary()) request.setRawHeader('User-Agent', 'QGIS 2.x') reply = self.manager.post(request, multipart) loop = QEventLoop() reply.uploadProgress.connect(self.progressCB) reply.error.connect(self.error) reply.finished.connect(loop.exit) loop.exec_() def progressCB(self, breceived, btotal): self.progress.emit(breceived, btotal) def returnFetchContent(self, reply): response = str(reply.readAll()) # qDebug('Response:' + response) # qDebug('Error: ' + str(reply.error())) # qDebug('Status: ' + str(reply.rawHeader('Location'))) if reply.rawHeader('Location') == 'http://cartodb.com/noneuser.html': response = '{"error": "User not found"}' elif reply.error() == QNetworkReply.AuthenticationRequiredError: response = '{"error": "Confirm user credentials"}' if self.returnDict: self.fetchContent.emit(json.loads(response)) else: self.fetchContent.emit(response) def error(self, error): qDebug('Error' + str(error))
class ActivityProtocol(QObject): # SIGNAL that is emitted after activities have been read readSignal = pyqtSignal(bool, int, str) # SIGNAL that is emitted after a new activity has been added created = pyqtSignal(bool, int, str) # SIGNAL that is emitted after an existing activity has been updated updated = pyqtSignal(bool, int, str) # SIGNAL that is emitted after an activity has been deleted deleted = pyqtSignal(bool, int, str) # SIGNAL that is emitted after activities have been counted counted = pyqtSignal(bool, int, str) finished = pyqtSignal( QNetworkReply ) def __init__(self, host, user, password): QObject.__init__(self) # Set the host self.host = host # Create a base64 encoded credential string from user name and password. # This is required for the HTTP basic access authentication, compare # also http://en.wikipedia.org/wiki/Basic_access_authentication self.userlogin = "******" % (user, password) self.login = "******" + QByteArray(self.userlogin).toBase64() # Create a new QNetworkAccessManager and connect it to the # authenticationRequired signal self.manager = QNetworkAccessManager(self) #self.connect(self.manager, SIGNAL("authenticationRequired( QNetworkReply*, QAuthenticator* )"), self.slotAuthenticationRequired) def update(self, rawBody): """ Update an activity using a POST request """ self.connect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.updateRequestFinished) url = "%s/activities" % self.host qurl = QUrl(url) self.request = QNetworkRequest(qurl) self.request.setRawHeader("Authorization", self.login) self.request.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") self.manager.post(self.request, json.dumps(rawBody).decode("UTF-8")) return url def updateRequestFinished(self, reply): self.disconnect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.updateRequestFinished) # Get the HTTP status code from the reply self.httpStatusCode = int(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)) #httpReasonPhrase = reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute) data = reply.readAll() self.updated.emit(self.httpStatusCode in (200, 201), self.httpStatusCode, str(data)) def read(self, extent, iface): self.connect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.readRequestFinished) #self.finished.connect(self.readRequestFinished) # Get the map extent xmin = extent.xMinimum() ymin = extent.yMinimum() xmax = extent.xMaximum() ymax = extent.yMaximum() # Get the authority identifier for the projects destination reference # system. Normally in case of EPSG that authid looks like "EPSG:4326" authid = iface.mapCanvas().mapRenderer().destinationCrs().authid() authcode = int(authid.split(":")[-1]) # Add explicitly the EPSG code to the bounding box, to enable users to # work in a local reference system url = "%s/activities/json?bbox=%f,%f,%f,%f&epsg=%1.f" % (self.host, xmin, ymin, xmax, ymax, authcode) qUrl = QUrl(url) self.request = QNetworkRequest(qUrl) self.request.setRawHeader("Authorization", self.login) self.request.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") self.manager.get(self.request) return url def readRequestFinished(self, reply): # Get the HTTP status code from the reply self.httpStatusCode = int(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)) #data = str("a") data = reply.readAll() # Check the status code see also http://en.wikipedia.org/wiki/HTTP_status_code # In case of a successful upload we get a 201 status code back and the # "stylePosted" signal is emitted with the first parameter set to True. # If the query didn't succeed, the status code is 4xx indicating that # the host was not found, the authentication failed or a forbidden # action in case a style with the same name already exists. It's up to # the receiver to handle these status codes. self.readSignal.emit(self.httpStatusCode in (200, 201), self.httpStatusCode, str(data)) def readById(self, id): """ """ self.connect(self.manager, SIGNAL("finished( QNetworkReply* )"), self.readByIdRequestFinished) # Get the latest version of the activity with this id url = "%s/activities/json/%s?geometry=full" % (self.host, id) qUrl = QUrl(url) self.request = QNetworkRequest(qUrl) self.request.setRawHeader("Authorization", self.login) self.manager.get(self.request) return url def readByIdRequestFinished(self, reply): # Get the HTTP status code from the reply self.httpStatusCode = int(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)) data = reply.readAll() self.readSignal.emit(self.httpStatusCode in (200, 201), self.httpStatusCode, str(data))