Beispiel #1
0
    def loadConfig(self):
        """Load preferences from file and replace default settings.
        """
        logger.debug("Loading config file")
        if self.confPath is None:
            return False

        theConf = NWConfigParser()
        cnfPath = os.path.join(self.confPath, self.confFile)
        try:
            with open(cnfPath, mode="r", encoding="utf-8") as inFile:
                theConf.read_file(inFile)
        except Exception as exc:
            logger.error("Could not load config file")
            logException()
            self.hasError = True
            self.errData.append("Could not load config file")
            self.errData.append(formatException(exc))
            return False

        # Main
        cnfSec = "Main"
        self.guiTheme = theConf.rdStr(cnfSec, "theme", self.guiTheme)
        self.guiSyntax = theConf.rdStr(cnfSec, "syntax", self.guiSyntax)
        self.guiIcons = theConf.rdStr(cnfSec, "icons", self.guiIcons)
        self.guiFont = theConf.rdStr(cnfSec, "guifont", self.guiFont)
        self.guiFontSize = theConf.rdInt(cnfSec, "guifontsize",
                                         self.guiFontSize)
        self.lastNotes = theConf.rdStr(cnfSec, "lastnotes", self.lastNotes)
        self.guiLang = theConf.rdStr(cnfSec, "guilang", self.guiLang)
        self.hideVScroll = theConf.rdBool(cnfSec, "hidevscroll",
                                          self.hideVScroll)
        self.hideHScroll = theConf.rdBool(cnfSec, "hidehscroll",
                                          self.hideHScroll)

        # Sizes
        cnfSec = "Sizes"
        self.winGeometry = theConf.rdIntList(cnfSec, "geometry",
                                             self.winGeometry)
        self.prefGeometry = theConf.rdIntList(cnfSec, "preferences",
                                              self.prefGeometry)
        self.projColWidth = theConf.rdIntList(cnfSec, "projcols",
                                              self.projColWidth)
        self.mainPanePos = theConf.rdIntList(cnfSec, "mainpane",
                                             self.mainPanePos)
        self.docPanePos = theConf.rdIntList(cnfSec, "docpane", self.docPanePos)
        self.viewPanePos = theConf.rdIntList(cnfSec, "viewpane",
                                             self.viewPanePos)
        self.outlnPanePos = theConf.rdIntList(cnfSec, "outlinepane",
                                              self.outlnPanePos)
        self.isFullScreen = theConf.rdBool(cnfSec, "fullscreen",
                                           self.isFullScreen)

        # Project
        cnfSec = "Project"
        self.autoSaveProj = theConf.rdInt(cnfSec, "autosaveproject",
                                          self.autoSaveProj)
        self.autoSaveDoc = theConf.rdInt(cnfSec, "autosavedoc",
                                         self.autoSaveDoc)
        self.emphLabels = theConf.rdBool(cnfSec, "emphlabels", self.emphLabels)

        # Editor
        cnfSec = "Editor"
        self.textFont = theConf.rdStr(cnfSec, "textfont", self.textFont)
        self.textSize = theConf.rdInt(cnfSec, "textsize", self.textSize)
        self.textWidth = theConf.rdInt(cnfSec, "width", self.textWidth)
        self.textMargin = theConf.rdInt(cnfSec, "margin", self.textMargin)
        self.tabWidth = theConf.rdInt(cnfSec, "tabwidth", self.tabWidth)
        self.focusWidth = theConf.rdInt(cnfSec, "focuswidth", self.focusWidth)
        self.hideFocusFooter = theConf.rdBool(cnfSec, "hidefocusfooter",
                                              self.hideFocusFooter)
        self.doJustify = theConf.rdBool(cnfSec, "justify", self.doJustify)
        self.autoSelect = theConf.rdBool(cnfSec, "autoselect", self.autoSelect)
        self.doReplace = theConf.rdBool(cnfSec, "autoreplace", self.doReplace)
        self.doReplaceSQuote = theConf.rdBool(cnfSec, "repsquotes",
                                              self.doReplaceSQuote)
        self.doReplaceDQuote = theConf.rdBool(cnfSec, "repdquotes",
                                              self.doReplaceDQuote)
        self.doReplaceDash = theConf.rdBool(cnfSec, "repdash",
                                            self.doReplaceDash)
        self.doReplaceDots = theConf.rdBool(cnfSec, "repdots",
                                            self.doReplaceDots)
        self.scrollPastEnd = theConf.rdInt(cnfSec, "scrollpastend",
                                           self.scrollPastEnd)
        self.autoScroll = theConf.rdBool(cnfSec, "autoscroll", self.autoScroll)
        self.autoScrollPos = theConf.rdInt(cnfSec, "autoscrollpos",
                                           self.autoScrollPos)
        self.fmtSingleQuotes = theConf.rdStrList(cnfSec, "fmtsinglequote",
                                                 self.fmtSingleQuotes)
        self.fmtDoubleQuotes = theConf.rdStrList(cnfSec, "fmtdoublequote",
                                                 self.fmtDoubleQuotes)
        self.fmtPadBefore = theConf.rdStr(cnfSec, "fmtpadbefore",
                                          self.fmtPadBefore)
        self.fmtPadAfter = theConf.rdStr(cnfSec, "fmtpadafter",
                                         self.fmtPadAfter)
        self.fmtPadThin = theConf.rdBool(cnfSec, "fmtpadthin", self.fmtPadThin)
        self.spellLanguage = theConf.rdStr(cnfSec, "spellcheck",
                                           self.spellLanguage)
        self.showTabsNSpaces = theConf.rdBool(cnfSec, "showtabsnspaces",
                                              self.showTabsNSpaces)
        self.showLineEndings = theConf.rdBool(cnfSec, "showlineendings",
                                              self.showLineEndings)
        self.showMultiSpaces = theConf.rdBool(cnfSec, "showmultispaces",
                                              self.showMultiSpaces)
        self.wordCountTimer = theConf.rdFlt(cnfSec, "wordcounttimer",
                                            self.wordCountTimer)
        self.bigDocLimit = theConf.rdInt(cnfSec, "bigdoclimit",
                                         self.bigDocLimit)
        self.incNotesWCount = theConf.rdBool(cnfSec, "incnoteswcount",
                                             self.incNotesWCount)
        self.showFullPath = theConf.rdBool(cnfSec, "showfullpath",
                                           self.showFullPath)
        self.highlightQuotes = theConf.rdBool(cnfSec, "highlightquotes",
                                              self.highlightQuotes)
        self.allowOpenSQuote = theConf.rdBool(cnfSec, "allowopensquote",
                                              self.allowOpenSQuote)
        self.allowOpenDQuote = theConf.rdBool(cnfSec, "allowopendquote",
                                              self.allowOpenDQuote)
        self.highlightEmph = theConf.rdBool(cnfSec, "highlightemph",
                                            self.highlightEmph)
        self.stopWhenIdle = theConf.rdBool(cnfSec, "stopwhenidle",
                                           self.stopWhenIdle)
        self.userIdleTime = theConf.rdInt(cnfSec, "useridletime",
                                          self.userIdleTime)

        # Backup
        cnfSec = "Backup"
        self.backupPath = theConf.rdStr(cnfSec, "backuppath", self.backupPath)
        self.backupOnClose = theConf.rdBool(cnfSec, "backuponclose",
                                            self.backupOnClose)
        self.askBeforeBackup = theConf.rdBool(cnfSec, "askbeforebackup",
                                              self.askBeforeBackup)

        # State
        cnfSec = "State"
        self.showRefPanel = theConf.rdBool(cnfSec, "showrefpanel",
                                           self.showRefPanel)
        self.viewComments = theConf.rdBool(cnfSec, "viewcomments",
                                           self.viewComments)
        self.viewSynopsis = theConf.rdBool(cnfSec, "viewsynopsis",
                                           self.viewSynopsis)
        self.searchCase = theConf.rdBool(cnfSec, "searchcase", self.searchCase)
        self.searchWord = theConf.rdBool(cnfSec, "searchword", self.searchWord)
        self.searchRegEx = theConf.rdBool(cnfSec, "searchregex",
                                          self.searchRegEx)
        self.searchLoop = theConf.rdBool(cnfSec, "searchloop", self.searchLoop)
        self.searchNextFile = theConf.rdBool(cnfSec, "searchnextfile",
                                             self.searchNextFile)
        self.searchMatchCap = theConf.rdBool(cnfSec, "searchmatchcap",
                                             self.searchMatchCap)

        # Path
        cnfSec = "Path"
        self.lastPath = theConf.rdStr(cnfSec, "lastpath", self.lastPath)

        # Check Certain Values for None
        self.spellLanguage = self._checkNone(self.spellLanguage)

        # If we're using straight quotes, disable auto-replace
        if self.fmtSingleQuotes == ["'", "'"] and self.doReplaceSQuote:
            logger.info(
                "Using straight single quotes, so disabling auto-replace")
            self.doReplaceSQuote = False

        if self.fmtDoubleQuotes == ['"', '"'] and self.doReplaceDQuote:
            logger.info(
                "Using straight double quotes, so disabling auto-replace")
            self.doReplaceDQuote = False

        # Check deprecated settings
        if self.guiIcons in ("typicons_colour_dark", "typicons_grey_dark"):
            self.guiIcons = "typicons_dark"
        elif self.guiIcons in ("typicons_colour_light", "typicons_grey_light"):
            self.guiIcons = "typicons_light"

        return True
Beispiel #2
0
def testBaseCommon_NWConfigParser(fncDir):
    """Test the NWConfigParser subclass.
    """
    tstConf = os.path.join(fncDir, "test.cfg")
    writeFile(tstConf, ("[main]\n"
                        "stropt = value\n"
                        "intopt1 = 42\n"
                        "intopt2 = 42.43\n"
                        "boolopt1 = true\n"
                        "boolopt2 = TRUE\n"
                        "boolopt3 = 1\n"
                        "boolopt4 = 0\n"
                        "list1 = a, b, c\n"
                        "list2 = 17, 18, 19\n"
                        "float1 = 4.2\n"))

    cfgParser = NWConfigParser()
    cfgParser.read(tstConf)

    # Readers
    # =======

    # Read String
    assert cfgParser.rdStr("main", "stropt", "stuff") == "value"
    assert cfgParser.rdStr("main", "boolopt1", "stuff") == "true"
    assert cfgParser.rdStr("main", "intopt1", "stuff") == "42"

    assert cfgParser.rdStr("nope", "stropt", "stuff") == "stuff"
    assert cfgParser.rdStr("main", "blabla", "stuff") == "stuff"

    # Read Boolean
    assert cfgParser.rdBool("main", "boolopt1", None) is True
    assert cfgParser.rdBool("main", "boolopt2", None) is True
    assert cfgParser.rdBool("main", "boolopt3", None) is True
    assert cfgParser.rdBool("main", "boolopt4", None) is False
    assert cfgParser.rdBool("main", "intopt1", None) is None

    assert cfgParser.rdBool("nope", "boolopt1", None) is None
    assert cfgParser.rdBool("main", "blabla", None) is None

    # Read Integer
    assert cfgParser.rdInt("main", "intopt1", 13) == 42
    assert cfgParser.rdInt("main", "intopt2", 13) == 13
    assert cfgParser.rdInt("main", "stropt", 13) == 13

    assert cfgParser.rdInt("nope", "intopt1", 13) == 13
    assert cfgParser.rdInt("main", "blabla", 13) == 13

    # Read Float
    assert cfgParser.rdFlt("main", "intopt1", 13.0) == 42.0
    assert cfgParser.rdFlt("main", "float1", 13.0) == 4.2
    assert cfgParser.rdInt("main", "stropt", 13.0) == 13.0

    assert cfgParser.rdInt("nope", "intopt1", 13.0) == 13.0
    assert cfgParser.rdInt("main", "blabla", 13.0) == 13.0

    # Read String List
    assert cfgParser.rdStrList("main", "list1", []) == []
    assert cfgParser.rdStrList("main", "list1", ["x"]) == ["a"]
    assert cfgParser.rdStrList("main", "list1", ["x", "y"]) == ["a", "b"]
    assert cfgParser.rdStrList("main", "list1",
                               ["x", "y", "z"]) == ["a", "b", "c"]
    assert cfgParser.rdStrList("main", "list1",
                               ["x", "y", "z", "w"]) == ["a", "b", "c", "w"]

    assert cfgParser.rdStrList("main", "stropt", ["x"]) == ["value"]
    assert cfgParser.rdStrList("main", "intopt1", ["x"]) == ["42"]

    assert cfgParser.rdStrList("nope", "list1", ["x"]) == ["x"]
    assert cfgParser.rdStrList("main", "blabla", ["x"]) == ["x"]

    # Read Integer List
    assert cfgParser.rdIntList("main", "list2", []) == []
    assert cfgParser.rdIntList("main", "list2", [1]) == [17]
    assert cfgParser.rdIntList("main", "list2", [1, 2]) == [17, 18]
    assert cfgParser.rdIntList("main", "list2", [1, 2, 3]) == [17, 18, 19]
    assert cfgParser.rdIntList("main", "list2",
                               [1, 2, 3, 4]) == [17, 18, 19, 4]

    assert cfgParser.rdIntList("main", "stropt", [1]) == [1]
    assert cfgParser.rdIntList("main", "boolopt1", [1]) == [1]

    assert cfgParser.rdIntList("nope", "list2", [1]) == [1]
    assert cfgParser.rdIntList("main", "blabla", [1]) == [1]

    # Internal
    # ========

    assert cfgParser._parseLine("main", "stropt", None, 999) is None