Esempio n. 1
0
    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
Esempio n. 2
0
    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)
Esempio n. 3
0
    def testConstruction(self):
        '''QTextStream construction'''
        obj = QTextStream()

        self.assertEqual(obj.codec(), QTextCodec.codecForLocale())
        self.assertEqual(obj.device(), None)
        self.assertEqual(obj.string(), None)

        self.assertTrue(obj.atEnd())
        self.assertEqual(obj.readAll(), '')
Esempio n. 4
0
    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
Esempio n. 5
0
    def prepare_db(self) -> bool:
        """Create the Database from the DDL.

        The DDL is a script that provided as QResource

        Returns:
            True if creation succesful.
        """
        if not self.database.isValid():
            self.log.warning("The Database is not valid, reopen")
            self.database = QSqlDatabase.database(self.database_name)

        self.database.open()

        query = QSqlQuery(self.database)

        file = QFile(":/data/script.sql")
        if not file.exists():
            self.log.error("Kritischer Fehler beim erzeugen der Datenbank"
                           " {} nicht gefunden".format(file.fileName()))
            return False

        if not file.open(QIODevice.ReadOnly):
            self.log.error(
                "Kritischer Fehler bei der Initialisierung der Datenbank."
                "Die Datei {} konnte nicht geöffnet werden".format(
                    file.fileName()))

        ts = QTextStream(file)

        line: str = ""
        cleaned_line: str = ""
        string_list: list = []
        read_line: list = []

        self.log.info("File at end: {}".format(ts.atEnd()))

        while not ts.atEnd():
            has_text: bool = False
            line = ""
            string_list.clear()
            while not has_text:
                read_line = ts.read_line()
                self.log.info("read Line: {}".format(read_line))
                cleaned_line = read_line.strip()
                string_list = cleaned_line.split("--")
                cleaned_line = string_list[0]
                if not cleaned_line.startswith(
                        "--") and not cleaned_line.startswith("DROP"):
                    line += cleaned_line
                if cleaned_line.endswith(";"):
                    break
                if cleaned_line.startswith("COMMIT"):
                    has_text = True
            if not line == "":
                self.log.info("Line: {}".format(line))

                if not query.exec_(line):
                    self.log.error(
                        "Fehler beim Erzeugen der Tabelle {}".format(line))
                    self.log.error("Datenbank meldet Fehler {}".format(
                        query.lastError()))
                    return False
            else:
                self.log.error(
                    "Fehler beim Lesen der Datei zur Datenbank Erzeugung: ")
                return False
        file.close()
        self.log.info("Datenbank erfolgreich erzeugt")
        return True
Esempio n. 6
0
    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)