def go_to_definition(self): self.dirty = True self.results = [] locations = self.get_locations() if self._isVariable: preResults = [ [file_manager.get_basename(x.path), x.path, x.lineno, ''] for x in locations if (x.type == FILTERS['attribs']) and (x.name == self._search)] else: preResults = [ [file_manager.get_basename(x.path), x.path, x.lineno, ''] for x in locations if ((x.type == FILTERS['functions']) or (x.type == FILTERS['classes'])) and (x.name.startswith(self._search))] for data in preResults: file_object = QFile(data[1]) if not file_object.open(QFile.ReadOnly): return stream = QTextStream(file_object) line_index = 0 line = stream.readLine() while not self._cancel and not stream.atEnd(): if line_index == data[2]: data[3] = line self.results.append(data) break #take the next line! line = stream.readLine() line_index += 1
def _grep_file(self, file_path, file_name): if not self.by_phrase: with open(file_path, 'r') as f: content = f.read() words = [word for word in self.search_pattern.pattern().split('|')] words.insert(0, True) def check_whole_words(result, word): return result and content.find(word) != -1 if not reduce(check_whole_words, words): return file_object = QFile(file_path) if not file_object.open(QFile.ReadOnly): return stream = QTextStream(file_object) lines = [] line_index = 0 line = stream.readLine() while not self._cancel and not (stream.atEnd() and not line): column = self.search_pattern.indexIn(line) if column != -1: lines.append((line_index, line)) #take the next line! line = stream.readLine() line_index += 1 #emit a signal! relative_file_name = file_manager.convert_to_relative( self.root_dir, file_path) self.emit(SIGNAL("found_pattern(PyQt_PyObject)"), (relative_file_name, lines))
def parseURLs(self, reply, startFrom): """ Get a dict of new IDs:URLs from the current playlist (newURLs) """ newURLs = {} # {0:URL0, 1:URL1, ...} count = 0 #Get the delta and start reading it allData = reply.readAll() allData = allData.right(startFrom) # Get rid of old data response = QTextStream(allData, QIODevice.ReadOnly) data = response.readLine() # Parse while (data): data = str(data.split("\n")[0]) if data: if "#" in data: # It's a playlist comment if self.__endTag in data: self.__playlistFinished = True elif self.__exceptionTag in data: if self.DEBUG: print "Exception found!" self.__exceptionFound = True self.__exceptionUrl = data.split(":",1)[1].strip() else: newURLs[count+self.__loadedChunks] = data count += 1 data = response.readLine() return newURLs
def _grep_file(self, file_path, file_name): file_object = QFile(file_path) if not file_object.open(QFile.ReadOnly): return stream = QTextStream(file_object) lines = [] line_index = 0 line = stream.readLine() while not self._cancel: if self.patFunction.match(line) or self.patClass.match(line): #fileName - path - lineNumber - lineContent lines.append((unicode(file_name), unicode(file_path), line_index, unicode(line))) #take the next line! line = stream.readLine() if line.isNull(): break line_index += 1 if lines: self.results += lines
def _grep_file_locate(self, file_path, file_name): file_object = QFile(file_path) if not file_object.open(QFile.ReadOnly): return #type - file_name - file_path global mapping_locations if file_manager.get_file_extension(unicode(file_name)) != 'py': mapping_locations[unicode(file_path)] = [('!', unicode(file_name), unicode(file_path), 0)] return mapping_locations[unicode(file_path)] = [('@', unicode(file_name), unicode(file_path), 0)] stream = QTextStream(file_object) lines = [] line_index = 0 line = stream.readLine() while not self._cancel: if self.patFunction.match(line): line = unicode(line) func_name = line[line.find('def') + 3:line.find('(')].strip() #type - function name - file_path - lineNumber lines.append(('>', func_name, unicode(file_path), line_index)) elif self.patClass.match(line): line = unicode(line) if line.find('(') > 0: class_name = line[ line.find('class') + 5:line.find('(')].strip() else: class_name = line[:line.find(':')].split( 'class')[1].strip() #type - class name - file_path - lineNumber lines.append(('<', class_name, unicode(file_path), line_index)) #take the next line! line = stream.readLine() if line.isNull(): break line_index += 1 if lines: mapping_locations[unicode(file_path)] += lines
def loadQTextStream(self): error = None fh = None try: fh = QFile(self.__fname) if not fh.open(QIODevice.ReadOnly): raise IOError(str(fh.errorString())) stream = QTextStream(fh) stream.setCodec(CODEC) self.clear(False) lino = 0 while not stream.atEnd(): title = year = minutes = acquired = notes = None line = stream.readLine() lino += 1 if not line.startsWith("{{MOVIE}}"): raise ValueError("no movie record found") else: title = line.mid(len("{{MOVIE}}")).trimmed() if stream.atEnd(): raise ValueError("premature end of file") line = stream.readLine() lino += 1 parts = line.split(" ") if parts.count() != 3: raise ValueError("invalid numeric data") year = intFromQStr(parts[0]) minutes = intFromQStr(parts[1]) ymd = parts[2].split("-") if ymd.count() != 3: raise ValueError("invalid acquired date") acquired = QDate(intFromQStr(ymd[0]), intFromQStr(ymd[1]), intFromQStr(ymd[2])) if stream.atEnd(): raise ValueError("premature end of file") line = stream.readLine() lino += 1 if line != "{NOTES}": raise ValueError("notes expected") notes = QString() while not stream.atEnd(): line = stream.readLine() lino += 1 if line == "{{ENDMOVIE}}": if (title is None or year is None or minutes is None or acquired is None or notes is None): raise ValueError("incomplete record") self.add(Movie(title, year, minutes, acquired, notes.trimmed())) break else: notes += line + "\n" else: raise ValueError("missing endmovie marker") except (IOError, OSError, ValueError) as e: error = "Failed to load: {0} on line {1}".format(e, lino) finally: if fh is not None: fh.close() if error is not None: return False, error self.__dirty = False return True, "Loaded {0} movie records from {1}".format( len(self.__movies), QFileInfo(self.__fname).fileName())
def load_gcode_file(self): file = QFile(self.filename) file.open(QIODevice.ReadOnly | QIODevice.Text) in_stream = QTextStream(file) file_size = file.size() counter = 0 line = 0 line_number = 0 while not in_stream.atEnd() and self.is_running is True: if self.update_progressbar: if counter==10000: #in_stream.pos() je hodne pomala funkce takze na ni pozor!!! progress = (in_stream.pos()*1./file_size*1.) * 100. self.set_update_progress.emit(int(progress)) counter=0 else: counter+=1 line = in_stream.readLine() bits = line.split(';', 1) bits_len = len(bits) if bits[0] == '': line_number+=1 if bits_len > 1: if bits[0] == '' and bits[1] == "END gcode for filament": break continue if 'G1 ' in bits[0]: self.parse_g1_line_new(bits, line_number) elif 'G4' in bits[0]: self.parse_g4_line(bits, line_number) elif 'T0' in bits[0] or 'T1' in bits[0] or 'T2' in bits[0] or 'T3' in bits[0]: self.parse_t_line(bits, line_number) elif 'G90' in bits[0]: self.absolute_coordinates = True elif 'G91' in bits[0]: self.absolute_coordinates = False elif 'G92' in bits[0]: self.parse_g92_line(bits, line_number) else: if DEBUG: print("Nezpracovano: " + str(bits)) line_number += 1 continue line_number += 1 if self.is_running is False and self.update_progressbar is True: self.set_update_progress.emit(0) self.printing_time = self.calculate_time_of_print() self.filament_length = 0.0 # self.calculate_length_of_filament() ### self.non_extruding_layers = [] for i in self.data: layer_flag = 'M' for l in self.data[i]: _start, _end, flag, _speed, _extrusion, _extruder, _line = l if flag in ['E', 'E-sk', 'E-su', 'E-i', 'E-p']: layer_flag = 'E' break if layer_flag == 'M': self.non_extruding_layers.append(i) for i in self.non_extruding_layers: self.data.pop(i, None) self.data_keys = set() self.data_keys = set(self.data) self.data_keys = sorted(self.data_keys, key=float) self.set_data_keys.emit(self.data_keys) self.set_data.emit(self.data) self.set_all_data.emit(self.all_data) self.set_printing_time.emit(self.printing_time) self.finished.emit()
class QtSingleApplication(QApplication): messageReceived = pyqtSignal(unicode) def __init__(self, id, *argv): super(QtSingleApplication, self).__init__(*argv) self._id = id self._activationWindow = None self._activateOnMessage = False # Is there another instance running? self._outSocket = QLocalSocket() self._outSocket.connectToServer(self._id) self._isRunning = self._outSocket.waitForConnected() if self._isRunning: # Yes, there is. self._outStream = QTextStream(self._outSocket) self._outStream.setCodec('UTF-8') else: # No, there isn't. self._outSocket = None self._outStream = None self._inSocket = None self._inStream = None self._server = QLocalServer() self._server.listen(self._id) self._server.newConnection.connect(self._onNewConnection) def isRunning(self): return self._isRunning def id(self): return self._id def activationWindow(self): return self._activationWindow def setActivationWindow(self, activationWindow, activateOnMessage = True): self._activationWindow = activationWindow self._activateOnMessage = activateOnMessage def activateWindow(self): if not self._activationWindow: return self._activationWindow.setWindowState( self._activationWindow.windowState() & ~Qt.WindowMinimized) self._activationWindow.raise_() self._activationWindow.activateWindow() def sendMessage(self, msg): if not self._outStream: return False self._outStream << msg << '\n' self._outStream.flush() return self._outSocket.waitForBytesWritten() def _onNewConnection(self): if self._inSocket: self._inSocket.readyRead.disconnect(self._onReadyRead) self._inSocket = self._server.nextPendingConnection() if not self._inSocket: return self._inStream = QTextStream(self._inSocket) self._inStream.setCodec('UTF-8') self._inSocket.readyRead.connect(self._onReadyRead) if self._activateOnMessage: self.activateWindow() def _onReadyRead(self): while True: msg = self._inStream.readLine() if not msg: break self.messageReceived.emit(msg)
def load_gcode_file(self): file = QFile(self.filename) file.open(QIODevice.ReadOnly | QIODevice.Text) in_stream = QTextStream(file) file_size = file.size() counter = 0 line = 0 line_number = 0 while not in_stream.atEnd() and self.is_running is True: if self.update_progressbar: if counter==10000: #in_stream.pos() je hodne pomala funkce takze na ni pozor!!! progress = (in_stream.pos()*1./file_size*1.) * 100. self.set_update_progress.emit(int(progress)) counter=0 else: counter+=1 line = in_stream.readLine() bits = line.split(';', 1) bits_len = len(bits) if bits[0] == '': line_number+=1 if bits_len > 1: if bits[0] == '' and bits[1] == "END gcode for filament": break continue if 'G1 ' in bits[0]: self.parse_g1_line_new(bits, line_number) elif 'G4' in bits[0]: self.parse_g4_line(bits, line_number) elif 'T0' in bits[0] or 'T1' in bits[0] or 'T2' in bits[0] or 'T3' in bits[0]: self.parse_t_line(bits, line_number) elif 'G90' in bits[0]: self.absolute_coordinates = True elif 'G91' in bits[0]: self.absolute_coordinates = False elif 'G92' in bits[0]: self.parse_g92_line(bits, line_number) else: if DEBUG: print("Nezpracovano: " + str(bits)) line_number += 1 continue line_number += 1 if self.is_running is False and self.update_progressbar is True: self.set_update_progress.emit(0) self.printing_time = self.calculate_time_of_print() self.filament_length = 0.0 # self.calculate_length_of_filament() ### self.non_extruding_layers = [] for i in self.data: layer_flag = 'M' for l in self.data[i]: _start, _end, flag, _speed, _extrusion, _extruder, _line = l if flag >= 1.0: layer_flag = 'E' break if layer_flag == 'M': self.non_extruding_layers.append(i) for i in self.non_extruding_layers: self.data.pop(i, None) #print("data after:") #pprint(len(self.data)) self.data_keys = set() self.data_keys = set(self.data) self.data_keys = sorted(self.data_keys, key=float) self.set_data_keys.emit(self.data_keys) self.set_data.emit(self.data) self.set_all_data.emit(self.all_data) self.set_printing_time.emit(self.printing_time) self.finished.emit()