Use constants for project and config file names

This commit is contained in:
Veronica Berglyd Olsen
2022-10-20 12:13:29 +02:00
parent 1da92e997c
commit 58f8f98c6f
5 changed files with 13 additions and 16 deletions
+4 -6
View File
@@ -52,12 +52,11 @@ class Config:
# Set Application Variables
self.appName = "novelWriter"
self.appHandle = self.appName.lower()
self.appHandle = "novelwriter"
# Set Paths
self.cmdOpen = None # Path from command line for project to be opened on launch
self.confPath = None # Folder where the config is saved
self.confFile = None # The config file name
self.dataPath = None # Folder where app data is stored
self.lastPath = None # The last user-selected folder (browse dialogs)
self.appPath = None # The full path to the novelwriter package folder
@@ -271,7 +270,6 @@ class Config:
logger.debug("Config path: %s", self.confPath)
logger.debug("Data path: %s", self.dataPath)
self.confFile = self.appHandle+".conf"
self.lastPath = os.path.expanduser("~")
self.appPath = getattr(sys, "_MEIPASS", os.path.abspath(os.path.dirname(__file__)))
self.appRoot = os.path.abspath(os.path.join(self.appPath, os.path.pardir))
@@ -311,7 +309,7 @@ class Config:
# Check if config file exists
if self.confPath is not None:
if os.path.isfile(os.path.join(self.confPath, self.confFile)):
if os.path.isfile(os.path.join(self.confPath, nwFiles.CONF_FILE)):
# If it exists, load it
self.loadConfig()
else:
@@ -396,7 +394,7 @@ class Config:
return False
theConf = NWConfigParser()
cnfPath = os.path.join(self.confPath, self.confFile)
cnfPath = os.path.join(self.confPath, nwFiles.CONF_FILE)
try:
with open(cnfPath, mode="r", encoding="utf-8") as inFile:
theConf.read_file(inFile)
@@ -619,7 +617,7 @@ class Config:
}
# Write config file
cnfPath = os.path.join(self.confPath, self.confFile)
cnfPath = os.path.join(self.confPath, nwFiles.CONF_FILE)
try:
with open(cnfPath, mode="w", encoding="utf-8") as outFile:
theConf.write(outFile)
+1
View File
@@ -68,6 +68,7 @@ class nwHeaders:
class nwFiles:
CONF_FILE = "novelwriter.conf"
PROJ_FILE = "nwProject.nwx"
PROJ_DICT = "wordlist.txt"
PROJ_LOCK = "nwProject.lock"
+3 -5
View File
@@ -85,7 +85,6 @@ class NWProject:
self.projDict = None # The spell check dictionary
self.projSpell = None # The spell check language, if different than default
self.projLang = None # The project language, used for builds
self.projFile = None # The file name of the project main XML file
self.projFiles = [] # A list of all files in the content folder on load
# Project Meta
@@ -260,7 +259,6 @@ class NWProject:
self.projDict = None
self.projSpell = None
self.projLang = None
self.projFile = nwFiles.PROJ_FILE
self.projFiles = []
self.projName = ""
self.bookTitle = ""
@@ -774,9 +772,9 @@ class NWProject:
self._projTree.packXML(nwXML)
# Write the xml tree to file
tempFile = os.path.join(self.projPath, self.projFile+"~")
saveFile = os.path.join(self.projPath, self.projFile)
backFile = os.path.join(self.projPath, self.projFile[:-3]+"bak")
tempFile = os.path.join(self.projPath, nwFiles.PROJ_FILE+"~")
saveFile = os.path.join(self.projPath, nwFiles.PROJ_FILE)
backFile = os.path.join(self.projPath, nwFiles.PROJ_FILE[:-3]+"bak")
try:
with open(tempFile, mode="wb") as outFile:
outFile.write(etree.tostring(
+2 -1
View File
@@ -55,6 +55,7 @@ from novelwriter.enum import (
nwDocMode, nwItemType, nwItemClass, nwAlert, nwWidget, nwState, nwView
)
from novelwriter.common import getGuiItem, hexToInt
from novelwriter.constants import nwFiles
logger = logging.getLogger(__name__)
@@ -353,7 +354,7 @@ class GuiMain(QMainWindow):
logger.error("No projData or projPath set")
return False
if os.path.isfile(os.path.join(projPath, self.theProject.projFile)):
if os.path.isfile(os.path.join(projPath, nwFiles.PROJ_FILE)):
self.makeAlert(self.tr(
"A project already exists in that location. "
"Please choose another folder."
+3 -4
View File
@@ -31,6 +31,7 @@ from PyQt5.QtWidgets import QMessageBox, QInputDialog
from novelwriter.enum import nwItemType, nwView, nwWidget
from novelwriter.tools import GuiProjectWizard
from novelwriter.dialogs import GuiEditLabel
from novelwriter.constants import nwFiles
from novelwriter.gui.outline import GuiOutlineView
from novelwriter.gui.projtree import GuiProjectTree
from novelwriter.gui.doceditor import GuiDocEditor
@@ -91,7 +92,7 @@ def testGuiMain_NewProject(monkeypatch, nwGUI, fncProj):
assert nwGUI.newProject(projData={}) is False
# Project file already exists
projFile = os.path.join(fncProj, nwGUI.theProject.projFile)
projFile = os.path.join(fncProj, nwFiles.PROJ_FILE)
writeFile(projFile, "Stuff")
assert nwGUI.newProject(projData={"projPath": fncProj}) is False
os.unlink(projFile)
@@ -102,7 +103,7 @@ def testGuiMain_NewProject(monkeypatch, nwGUI, fncProj):
# This one should work just fine
assert nwGUI.newProject(projData={"projPath": fncProj}) is True
assert os.path.isfile(os.path.join(fncProj, nwGUI.theProject.projFile))
assert os.path.isfile(os.path.join(fncProj, nwFiles.PROJ_FILE))
assert os.path.isdir(os.path.join(fncProj, "content"))
# END Test testGuiMain_NewProject
@@ -183,7 +184,6 @@ def testGuiMain_Editing(qtbot, monkeypatch, nwGUI, fncProj, refDir, outDir, mock
assert nwGUI.theProject.tree.trashRoot() is None
assert nwGUI.theProject.projPath is None
assert nwGUI.theProject.projMeta is None
assert nwGUI.theProject.projFile == "nwProject.nwx"
assert nwGUI.theProject.projName == ""
assert nwGUI.theProject.bookTitle == ""
assert len(nwGUI.theProject.bookAuthors) == 0
@@ -208,7 +208,6 @@ def testGuiMain_Editing(qtbot, monkeypatch, nwGUI, fncProj, refDir, outDir, mock
assert nwGUI.theProject.tree.trashRoot() is None
assert nwGUI.theProject.projPath == fncProj
assert nwGUI.theProject.projMeta == os.path.join(fncProj, "meta")
assert nwGUI.theProject.projFile == "nwProject.nwx"
assert nwGUI.theProject.projName == "New Project"
assert nwGUI.theProject.bookTitle == "New Novel"
assert len(nwGUI.theProject.bookAuthors) == 1