Change assetPath to a Path object

This commit is contained in:
Veronica Berglyd Olsen
2022-11-09 17:44:20 +01:00
parent 6792c11a71
commit 9d3291aba6
13 changed files with 142 additions and 151 deletions
-2
View File
@@ -27,7 +27,6 @@ import sys
import getopt
import logging
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QErrorMessage
from novelwriter.error import exceptionHandler, logException
@@ -249,7 +248,6 @@ def main(sysArgs=None):
nwApp = QApplication([CONFIG.appName, (f"-style={qtStyle}")])
nwApp.setApplicationName(CONFIG.appName)
nwApp.setApplicationVersion(__version__)
nwApp.setWindowIcon(QIcon(CONFIG.appIcon))
nwApp.setOrganizationDomain(__domain__)
# Connect the exception handler before making the main GUI
+63 -57
View File
@@ -51,6 +51,9 @@ class Config:
def __init__(self):
# Initialisation
# ==============
# Set Application Variables
self.appName = "novelWriter"
self.appHandle = "novelwriter"
@@ -62,20 +65,37 @@ class Config:
self._confPath = confRoot.absolute() / self.appHandle # The user config location
self._dataPath = dataRoot.absolute() / self.appHandle # The user data location
self.cmdOpen = None # Path from command line for project to be opened on launch
self.lastPath = None # The last user-selected folder (browse dialogs)
self.appPath = None # The full path to the novelwriter package folder
self.appRoot = None # The full path to the novelwriter root folder
self.appIcon = None # The full path to the novelwriter icon file
self.assetPath = None # The full path to the novelwriter/assets folder
self.pdfDocs = None # The location of the PDF manual, if it exists
if hasattr(sys, "_MEIPASS"):
self._appPath = Path(sys._MEIPASS).absolute()
else:
self._appPath = Path(__file__).parent.absolute()
self._appRoot = self._appPath.parent
if self._appRoot.is_file():
# novelWriter is packaged as a single file
self._appRoot = self._appRoot.parent
self._appPath = self._appRoot
self.cmdOpen = None # Path from command line for project to be opened on launch
self.lastPath = None # The last user-selected folder (browse dialogs)
self.pdfDocs = None # The location of the PDF manual, if it exists
# Runtime Settings and Variables
self.hasError = False # True if the config class encountered an error
self.errData = [] # List of error messages
self.confChanged = False # True whenever the config has chenged, false after save
# General
# Localisation Info
self._qLocal = QLocale.system()
self._qtTrans = {}
self._qtLangPath = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
self._nwLangPath = str(self._appPath / "assets" / "i18n")
# User Settings
# =============
# General GUI Settings
self.guiLang = self._qLocal.name()
self.guiTheme = "" # GUI theme
self.guiSyntax = "" # Syntax theme
self.guiFont = "" # Defaults to system default font
@@ -86,14 +106,7 @@ class Config:
self.setDefaultGuiTheme()
self.setDefaultSyntaxTheme()
# Localisation
self.qLocal = QLocale.system()
self.guiLang = self.qLocal.name()
self.qtLangPath = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
self.nwLangPath = None
self.qtTrans = {}
# Sizes
# Size Settings
self.winGeometry = [1200, 650]
self.prefGeometry = [700, 615]
self.projColWidth = [200, 60, 140]
@@ -103,16 +116,16 @@ class Config:
self.outlnPanePos = [500, 150]
self.isFullScreen = False
# Features
# Feature Settings
self.hideVScroll = False # Hide vertical scroll bars on main widgets
self.hideHScroll = False # Hide horizontal scroll bars on main widgets
self.emphLabels = True # Add emphasis to H1 and H2 item labels
# Project
# Project Settings
self.autoSaveProj = 60 # Interval for auto-saving project in seconds
self.autoSaveDoc = 30 # Interval for auto-saving document in seconds
# Text Editor
# Text Editor Settings
self.textFont = None # Editor font
self.textSize = 12 # Editor font size
self.textWidth = 700 # Editor text width
@@ -151,7 +164,7 @@ class Config:
self.stopWhenIdle = True # Stop the status bar clock when the user is idle
self.userIdleTime = 300 # Time of inactivity to consider user idle
# User-Selected Symbols
# User-Selected Symbol Settings
self.fmtApostrophe = nwUnicode.U_RSQUO
self.fmtSingleQuotes = [nwUnicode.U_LSQUO, nwUnicode.U_RSQUO]
self.fmtDoubleQuotes = [nwUnicode.U_LDQUO, nwUnicode.U_RDQUO]
@@ -159,7 +172,7 @@ class Config:
self.fmtPadAfter = ""
self.fmtPadThin = False
# Spell Checking
# Spell Checking Settings
self.spellLanguage = None
# Search Bar Switches
@@ -170,7 +183,7 @@ class Config:
self.searchNextFile = False
self.searchMatchCap = False
# Backup
# Backup Settings
self.backupPath = ""
self.backupOnClose = False
self.askBeforeBackup = True
@@ -180,6 +193,9 @@ class Config:
self.viewComments = True # Comments are shown in the viewer
self.viewSynopsis = True # Synopsis is shown in the viewer
# System and App Information
# ==========================
# Check Qt5 Versions
verQt = splitVersionNumber(QT_VERSION_STR)
self.verQtString = QT_VERSION_STR
@@ -254,6 +270,13 @@ class Config:
return self._dataPath / target
return self._dataPath
def getAssetPath(self, target=None):
"""Return a path in the assets folder.
"""
if isinstance(target, str):
return self._appPath / "assets" / target
return self._appPath / "assets"
##
# Config Actions
##
@@ -271,29 +294,12 @@ class Config:
logger.info("Setting data path from alternative path: %s", dataPath)
self._dataPath = Path(dataPath)
logger.debug("Config path: %s", self._confPath)
logger.debug("Data path: %s", self._dataPath)
logger.debug("Config Path: %s", self._confPath)
logger.debug("Data Path: %s", self._dataPath)
logger.debug("App Root: %s", self._appRoot)
logger.debug("App Path: %s", self._appPath)
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))
if os.path.isfile(self.appRoot):
# novelWriter is packaged as a single file, so the app and
# root paths are the same, and equal to the folder that
# contains the single executable.
self.appRoot = os.path.dirname(self.appRoot)
self.appPath = self.appRoot
# Assets
self.assetPath = os.path.join(self.appPath, "assets")
self.appIcon = os.path.join(self.assetPath, "icons", "novelwriter.svg")
# Internationalisation
self.nwLangPath = os.path.join(self.assetPath, "i18n")
logger.debug("Assets: %s", self.assetPath)
logger.debug("App path: %s", self.appPath)
logger.debug("Last path: %s", self.lastPath)
# If the config and data folders don't not exist, create them
@@ -324,9 +330,9 @@ class Config:
self.spellLanguage = "en"
# Look for a PDF version of the manual
pdfDocs = os.path.join(self.assetPath, "manual.pdf")
if os.path.isfile(pdfDocs):
logger.debug("Found manual: %s", pdfDocs)
pdfDocs = self._appPath / "assets" / "manual.pdf"
if pdfDocs.is_file():
logger.debug("Found PDF manual: %s", pdfDocs)
self.pdfDocs = pdfDocs
logger.debug("Config initialisation complete")
@@ -336,24 +342,24 @@ class Config:
def initLocalisation(self, nwApp):
"""Initialise the localisation of the GUI.
"""
self.qLocal = QLocale(self.guiLang)
QLocale.setDefault(self.qLocal)
self.qtTrans = {}
self._qLocal = QLocale(self.guiLang)
QLocale.setDefault(self._qLocal)
self._qtTrans = {}
langList = [
(self.qtLangPath, "qtbase"), # Qt 5.x
(self.nwLangPath, "qtbase"), # Alternative Qt 5.x
(self.nwLangPath, "nw"), # novelWriter
(self._qtLangPath, "qtbase"), # Qt 5.x
(self._nwLangPath, "qtbase"), # Alternative Qt 5.x
(self._nwLangPath, "nw"), # novelWriter
]
for lngPath, lngBase in langList:
for lngCode in self.qLocal.uiLanguages():
for lngCode in self._qLocal.uiLanguages():
qTrans = QTranslator()
lngFile = "%s_%s" % (lngBase, lngCode.replace("-", "_"))
if lngFile not in self.qtTrans:
if lngFile not in self._qtTrans:
if qTrans.load(lngFile, lngPath):
logger.debug("Loaded: %s", os.path.join(lngPath, lngFile))
nwApp.installTranslator(qTrans)
self.qtTrans[lngFile] = qTrans
self._qtTrans[lngFile] = qTrans
return
@@ -372,8 +378,8 @@ class Config:
else:
return []
for qmFile in os.listdir(self.nwLangPath):
if not os.path.isfile(os.path.join(self.nwLangPath, qmFile)):
for qmFile in os.listdir(self._nwLangPath):
if not os.path.isfile(os.path.join(self._nwLangPath, qmFile)):
continue
if not qmFile.startswith(fPre) or not qmFile.endswith(fExt):
continue
+2 -3
View File
@@ -24,7 +24,6 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import shutil
import logging
import novelwriter
@@ -431,8 +430,8 @@ class ProjectBuilder:
logger.error("No project path set for the example project")
return False
pkgSample = os.path.join(self.mainConf.assetPath, "sample.zip")
if os.path.isfile(pkgSample):
pkgSample = self.mainConf.getAssetPath("sample.zip")
if pkgSample.is_file():
try:
shutil.unpack_archive(pkgSample, projPath)
except Exception as exc:
+3 -3
View File
@@ -698,13 +698,13 @@ class NWProject(QObject):
def _loadProjectLocalisation(self):
"""Load the language data for the current project language.
"""
if self._data.language is None or self.mainConf.nwLangPath is None:
if self._data.language is None or self.mainConf._nwLangPath is None:
self._langData = {}
return False
langFile = Path(self.mainConf.nwLangPath) / f"project_{self._data.language}.json"
langFile = Path(self.mainConf._nwLangPath) / f"project_{self._data.language}.json"
if not langFile.is_file():
langFile = Path(self.mainConf.nwLangPath) / "project_en_GB.json"
langFile = Path(self.mainConf._nwLangPath) / "project_en_GB.json"
try:
with open(langFile, mode="r", encoding="utf-8") as inFile:
+2 -3
View File
@@ -23,7 +23,6 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import logging
import novelwriter
@@ -233,7 +232,7 @@ class GuiAbout(QDialog):
def _fillNotesPage(self):
"""Load the content for the Release Notes page.
"""
docPath = os.path.join(self.mainConf.assetPath, "text", "release_notes.htm")
docPath = self.mainConf.getAssetPath("text") / "release_notes.htm"
docText = readTextFile(docPath)
if docText:
self.pageNotes.setHtml(docText)
@@ -244,7 +243,7 @@ class GuiAbout(QDialog):
def _fillLicensePage(self):
"""Load the content for the Licence page.
"""
docPath = os.path.join(self.mainConf.assetPath, "text", "gplv3_en.htm")
docPath = self.mainConf.getAssetPath("text") / "gplv3_en.htm"
docText = readTextFile(docPath)
if docText:
self.pageLicense.setHtml(docText)
+7 -5
View File
@@ -26,6 +26,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
import novelwriter
from pathlib import Path
from urllib.parse import urljoin
from urllib.request import pathname2url
@@ -104,10 +105,11 @@ class GuiMainMenu(QMenuBar):
def _openUserManualFile(self):
"""Open the documentation in PDF format.
"""
if self.mainConf.pdfDocs is None:
return False
QDesktopServices.openUrl(QUrl(urljoin("file:", pathname2url(self.mainConf.pdfDocs))))
return True
if isinstance(self.mainConf.pdfDocs, Path):
QDesktopServices.openUrl(
QUrl(urljoin("file:", pathname2url(str(self.mainConf.pdfDocs))))
)
return
##
# Menu Builders
@@ -881,7 +883,7 @@ class GuiMainMenu(QMenuBar):
self.helpMenu.addAction(self.aHelpDocs)
# Help > User Manual (PDF)
if self.mainConf.pdfDocs is not None:
if isinstance(self.mainConf.pdfDocs, Path):
self.aPdfDocs = QAction(self.tr("User Manual (PDF)"), self)
self.aPdfDocs.setShortcut("Shift+F1")
self.aPdfDocs.triggered.connect(self._openUserManualFile)
+15 -22
View File
@@ -24,12 +24,10 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import logging
import novelwriter
from math import ceil
from pathlib import Path
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import qApp
@@ -120,9 +118,8 @@ class GuiTheme:
self._availThemes = {}
self._availSyntax = {}
self._listConf(self._availSyntax, os.path.join(self.mainConf.assetPath, "syntax"))
self._listConf(self._availThemes, os.path.join(self.mainConf.assetPath, "themes"))
self._listConf(self._availSyntax, self.mainConf.getAssetPath("syntax"))
self._listConf(self._availThemes, self.mainConf.getAssetPath("themes"))
self._listConf(self._availSyntax, self.mainConf.getDataPath("syntax"))
self._listConf(self._availThemes, self.mainConf.getDataPath("themes"))
@@ -380,7 +377,6 @@ class GuiTheme:
def _listConf(self, targetDict, checkDir):
"""Scan for theme config files and populate the dictionary.
"""
checkDir = Path(checkDir)
if not checkDir.is_dir():
return False
@@ -476,7 +472,7 @@ class GuiIcons:
self._confName = "icons.conf"
# Icon Theme Path
self._iconPath = os.path.join(self.mainConf.assetPath, "icons")
self._iconPath = self.mainConf.getAssetPath("icons")
# Icon Theme Meta
self.themeName = ""
@@ -499,12 +495,12 @@ class GuiIcons:
update functions for the classes where they're used.
"""
self._themeMap = {}
themePath = os.path.join(self.mainConf.assetPath, "icons", iconTheme)
if not os.path.isdir(themePath):
themePath = self._iconPath / iconTheme
if not themePath.is_dir():
logger.warning("No icons loaded for '%s'", iconTheme)
return False
themeConf = os.path.join(themePath, self._confName)
themeConf = themePath / self._confName
logger.info("Loading icon theme '%s'", iconTheme)
# Config File
@@ -535,8 +531,8 @@ class GuiIcons:
if iconName not in self.ICON_KEYS:
logger.error("Unknown icon name '%s' in config file", iconName)
else:
iconPath = os.path.join(themePath, iconFile)
if os.path.isfile(iconPath):
iconPath = themePath / iconFile
if iconPath.is_file():
self._themeMap[iconName] = iconPath
logger.debug("Icon slot '%s' using file '%s'", iconName, iconFile)
else:
@@ -572,18 +568,16 @@ class GuiIcons:
if decoKey in self._themeMap:
imgPath = self._themeMap[decoKey]
elif decoKey in self.IMAGE_MAP:
imgPath = os.path.join(
self.mainConf.assetPath, "images", self.IMAGE_MAP[decoKey]
)
imgPath = self.mainConf.getAssetPath("images") / self.IMAGE_MAP[decoKey]
else:
logger.error("Decoration with name '%s' does not exist", decoKey)
return QPixmap()
if not os.path.isfile(imgPath):
if not imgPath.is_file():
logger.error("Asset not found: %s", imgPath)
return QPixmap()
theDeco = QPixmap(imgPath)
theDeco = QPixmap(str(imgPath))
if pxW is not None and pxH is not None:
return theDeco.scaled(pxW, pxH, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
elif pxW is None and pxH is not None:
@@ -667,15 +661,14 @@ class GuiIcons:
# If we just want the app icons, return right away
if iconKey == "novelwriter":
return QIcon(os.path.join(self._iconPath, "novelwriter.svg"))
return QIcon(str(self._iconPath / "novelwriter.svg"))
elif iconKey == "proj_nwx":
return QIcon(os.path.join(self._iconPath, "x-novelwriter-project.svg"))
return QIcon(str(self._iconPath / "x-novelwriter-project.svg"))
# Otherwise, we load from the theme folder
if iconKey in self._themeMap:
relPath = os.path.relpath(self._themeMap[iconKey], self._iconPath)
logger.debug("Loading: %s", relPath)
return QIcon(self._themeMap[iconKey])
logger.debug("Loading: %s", self._themeMap[iconKey].name)
return QIcon(str(self._themeMap[iconKey]))
# If we didn't find one, give up and return an empty icon
logger.warning("Did not load an icon for '%s'", iconKey)
+7 -2
View File
@@ -29,6 +29,7 @@ import novelwriter
from enum import Enum
from time import time
from pathlib import Path
from datetime import datetime
from PyQt5.QtCore import Qt, QTimer, QThreadPool, pyqtSlot
@@ -95,7 +96,11 @@ class GuiMain(QMainWindow):
# Prepare Main Window
self.resize(*self.mainConf.getWinSize())
self._updateWindowTitle()
self.setWindowIcon(QIcon(self.mainConf.appIcon))
nwIcon = self.mainConf.getAssetPath("icons") / "novelwriter.svg"
self.nwIcon = QIcon(str(nwIcon)) if nwIcon.is_file() else QIcon()
self.setWindowIcon(self.nwIcon)
qApp.setWindowIcon(self.nwIcon)
# Build the GUI
# =============
@@ -1362,7 +1367,7 @@ class GuiMain(QMainWindow):
# Help
self.addAction(self.mainMenu.aHelpDocs)
if self.mainConf.pdfDocs is not None:
if isinstance(self.mainConf.pdfDocs, Path):
self.addAction(self.mainMenu.aPdfDocs)
return True
+1 -2
View File
@@ -23,7 +23,6 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import random
import logging
import novelwriter
@@ -120,7 +119,7 @@ class GuiLipsum(QDialog):
def _doInsert(self):
"""Load the text and insert it in the open document.
"""
lipsumFile = os.path.join(self.mainConf.assetPath, "text", "lipsum.txt")
lipsumFile = self.mainConf.getAssetPath("text") / "lipsum.txt"
lipsumText = readTextFile(lipsumFile).splitlines()
if self.randSwitch.isChecked():