def loadConfig(self): self.initRiseRun = None self.initSetRun = None cfgFile = QFile(self.fileName) if cfgFile is not None: if cfgFile.exists(): debugMessage("Config file found") if cfgFile.open(QIODevice.ReadOnly | QIODevice.Text): inStream = QTextStream(cfgFile) if inStream is not None: # Assume correct for system timezone is OFF self.setCorrectForSysTZ(False) while not inStream.atEnd(): inStream.skipWhiteSpace() line = inStream.readLine() self.processConfigLine(line) else: debugMessage("Config file NOT found") self.setCorrectForSysTZ(False) # We are only successful if we have a latitude, longitude and timezone result = (self.latitude is not None)\ and (self.longitude is not None)\ and (self.homeTZ is not None) return result
def _check_data(self, data_set): for data, lines in data_set: stream = QTextStream(data) res = [] while not stream.atEnd(): res.append(stream.readLine()) self.assertEqual(res, lines)
class QTextStreamShiftTest(unittest.TestCase): def setUp(self): self.ba = QByteArray() self.read = QTextStream(self.ba, QIODevice.ReadOnly) self.write = QTextStream(self.ba, QIODevice.WriteOnly) def testNumber(self): '''QTextStream << number''' self.write << '4' self.write.flush() res = self.read.readLine() self.assertTrue(isinstance(res, py3k.unicode)) self.assertEqual(res, '4')
def PreprocessedQSS() -> str: lines = [] qss_file = QFile(Loader.STYLE_FILE_PATH) if qss_file.open(QIODevice.ReadOnly | QFile.Text): stream = QTextStream(qss_file) stream.setCodec("UTF-8") while not stream.atEnd(): lines.append(stream.readLine().rstrip("\n")) qss_file.close() qss_vars = {} qss_body = "" for i, l in enumerate(lines): l = l.strip() if len(l) == 0: continue if l.startswith("//"): continue if l[0] == "@": if not l.endswith(";"): print( "[WARNING] QSSPreprocessor : Parsing error at line %d for '%s' (missing semicolon)" % (i, l)) l = l.rstrip(";") s = l.split(":") if len(s) < 2: print( "[WARNING] QSSPreprocessor : Parsing error at line %d for '%s' (missing colon)" % (i, l)) continue qss_vars[s[0].strip()] = s[1].strip() else: qss_body += l qss_vars_names = list(qss_vars.keys()) qss_vars_names.sort() qss_vars_names.reverse() for k in qss_vars_names: qss_body = qss_body.replace(k, qss_vars[k]) Loader._qss_variables[k] = qss_vars[k] return qss_body
class QSingleApplication(QApplication): onMassageRecived = Signal(str) # Text Massege Event; def init(self, uid: str) -> bool: """ Try To Connect or Create : If Created Then Return True """ self._stream_str: Optional[QTextStream] = None self._server: Optional[QLocalServer] = None self._socket: Optional[QLocalSocket] = None self._uid: str = uid if not self.__connect(): self.__createServer() return True return False def sendTextMassege(self, msg: str): """ Send A Text Massege To Running Instance """ if self._socket and self._stream_str and not self._server: self._stream_str << msg << '\n' self._stream_str.flush() return self._socket.waitForBytesWritten() else: return False def __connect(self): """ Create A Local Socket And Try To Connect """ if self._socket: self._socket.close() socket = QLocalSocket() socket.connectToServer(self._uid) connected = socket.waitForConnected() if not connected: socket.close() return False self._socket = socket self._stream_str = QTextStream(self._socket) self._stream_str.setCodec('UTF-8') return True def __createServer(self): """ Create A Server & Listen as UID """ self._server = QLocalServer() self._server.listen(self._uid) self._server.newConnection.connect(self.__onNewConnection) def __onNewConnection(self): """ On New Socket Connection From Clint """ if self._socket: self._socket.readyRead.disconnect(self.__onReadyRead) self._socket = self._server.nextPendingConnection() if not self._socket: return self._stream_str = QTextStream(self._socket) self._stream_str.setCodec('UTF-8') self._socket.readyRead.connect(self.__onReadyRead) def __onReadyRead(self): """ Stream Ready To Read """ while True: msg = self._stream_str.readLine() if not msg: break self.onMassageRecived.emit(msg)
class SingleApplication(QApplication): messageReceived = Signal() def __init__(self, id, *argv): super(SingleApplication, 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.show() self._activationWindow.setWindowState(( self._activationWindow.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive) SetWindowPos( self._activationWindow.winId(), # = always on top. only reliable way to bring it to the front on windows win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) # Do not reset topmost; for monitor control this is set... # SetWindowPos(self._activationWindow.winId(), # # disable the always on top, but leave window at its top position # win32con.HWND_NOTOPMOST, # 0, 0, 0, 0, # win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) # 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)
class QtSingleApplication(QApplication): messageReceived = Signal() 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 loadCache(self): file = QFile("cache") if not file.open(QIODevice.ReadOnly | QIODevice.Text): return inStream = QTextStream(file) # ip self.ipLineEdit.setText(inStream.readLine()) # passwordLabel self.passwordLineEdit.setText(inStream.readLine()) # user self.userLineEdit.setText(inStream.readLine()) # ver1 self.ver1LineEdit.setText(inStream.readLine()) self.dir1LineEdit.setText(inStream.readLine()) # ver2 self.ver2LineEdit.setText(inStream.readLine()) self.dir2LineEdit.setText(inStream.readLine()) # ver3 self.ver3LineEdit.setText(inStream.readLine()) self.dir3LineEdit.setText(inStream.readLine()) self.intervalSpinBox.setValue(int(inStream.readLine())) self.loopSpinBox.setValue(int(inStream.readLine())) # web type button webType = inStream.readLine() if webType == "gxpAction": self.grp2602Action.setChecked(False) self.gxpAction.setChecked(True) self.webBtn.setText(self.gxpAction.text()) else: self.grp2602Action.setChecked(True) self.gxpAction.setChecked(False) self.webBtn.setText(self.grp2602Action.text()) testType = inStream.readLine() if testType == "reboot": self.radioReboot.setChecked(True) elif testType == "provision": self.radioProvision.setChecked(True) self.changePosition(False) self.provWidget.show() else: self.radioFactory.setChecked(True) serverType = inStream.readLine() if serverType == "Http": self.radioHttp.setChecked(True) elif serverType == "Https": self.radioHttps.setChecked(True) elif serverType == "Tftp": self.radioTftp.setChecked(True) elif serverType == "Ftp": self.radioFtp.setChecked(True) elif serverType == "Ftps": self.radioFtps.setChecked(True) else: self.radioWindow.setChecked(True) if inStream.readLine() == "True": self.findCoreStop = True else: self.findCoreStop = False if inStream.readLine() == "True": self.cleanCacheSet = True else: self.cleanCacheSet = False if inStream.readLine() == "True": self.useApiTest = True self.webBtn.setEnabled(False) else: self.useApiTest = False self.corePathBtn.hide() self.corePathLabel.hide() if inStream.readLine() == "True": self.showTestProgress = True else: self.showTestProgress = False self.infoLabel.hide() if inStream.readLine() == "True": self.showChrome = True else: self.showChrome = False self.chromePath = inStream.readLine() if inStream.readLine() == "True": self.saveTestLog = True else: self.saveTestLog = False self.saveLogPath = inStream.readLine() self.coreDumpPath = inStream.readLine() file.close()
def saveConfig(self): # We haven't yet saved each property self.savedShowLocationDMS = False self.savedLat = False self.savedLon = False self.savedTZ = False self.savedCorrectForSysTZ = False self.savedRiseRun = False self.savedSetRun = False self.savedRunLastEventAtLaunch = False # Get the config and temp filenames cfgFilename = self.getConfigFilename() tmpFilename = self.getConfigTempFilename() # Use any original config file to read and a temp file to write cfgFile = QFile(cfgFilename) tmpFile = QFile(tmpFilename) if (cfgFile is not None) and (tmpFile is not None): # Open the input inStream = None if cfgFile.exists(): debugMessage("Config file found") if cfgFile.open(QIODevice.ReadOnly | QIODevice.Text): inStream = QTextStream(cfgFile) # Open the output if tmpFile.open(QFile.WriteOnly | QFile.Truncate | QIODevice.Text): outStream = QTextStream(tmpFile) else: outStream = None if outStream is not None: # If we have an input file, read through it re-writing it to # the temp file and change any known settings to current values if inStream is not None: while not inStream.atEnd(): line = inStream.readLine() print("Saving Config line: {}".format(line)) self.processOutputConfigLine(outStream, line) # Remove the original config file cfgFile.remove() cfgFile = None else: warningMessage("Unable to open file to save " "configuration: {}".format(tmpFilename), self.configSrcFrom) # Fixup anything we didn't save in the temp file they will # only be written based on the third argument being True locationFmtCorrect = (self.savedShowLocationDMS is False) and\ (self.getShowLocationDMS() is True) self.processOutputConfigLine(outStream, "showLocationInDMS", locationFmtCorrect) self.processOutputConfigLine(outStream, "latitude=0", not self.savedLat) self.processOutputConfigLine(outStream, "longitude=0", not self.savedLon) self.processOutputConfigLine(outStream, "timezone=0", not self.savedTZ) tzCorrect = (self.savedCorrectForSysTZ is False) and\ (self.getCorrectForSysTZ() is True) self.processOutputConfigLine(outStream, "CorrectForSystemTimezone", tzCorrect) self.processOutputConfigLine(outStream, "sunriserun=abc", not self.savedRiseRun) self.processOutputConfigLine(outStream, "sunsetrun=abc", not self.savedSetRun) launchRun = (self.savedRunLastEventAtLaunch is False) and\ (self.runLastEventAtLaunch is True) self.processOutputConfigLine(outStream, "runlasteventatlaunch", launchRun) # Rename the temp file as the config file tmpFile.rename(cfgFilename) else: warningMessage("Unable to open previous file to save " "configuration: {}".format(cfgFilename), self.configSrcFrom)
class QtSingleApplication(QApplication): messageReceived = Signal(str) 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)