Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite config mocking #1447

Merged
merged 5 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions novelwriter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@
__date__ = "2023-04-16"
__status__ = "Stable"
__domain__ = "novelwriter.io"
__url__ = "https://novelwriter.io"
__docurl__ = "https://docs.novelwriter.io/"
__sourceurl__ = "https://github.com/vkbo/novelWriter"
__issuesurl__ = "https://github.com/vkbo/novelWriter/issues"
__helpurl__ = "https://github.com/vkbo/novelWriter/discussions"
__releaseurl__ = "https://github.com/vkbo/novelWriter/releases/latest"

logger = logging.getLogger(__name__)

Expand Down
12 changes: 6 additions & 6 deletions novelwriter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,10 @@ def initConfig(self, confPath=None, dataPath=None):
"""
logger.debug("Initialising Config ...")
if isinstance(confPath, (str, Path)):
logger.info("Setting config from alternative path: %s", confPath)
logger.info("Setting alternative config path: %s", confPath)
self._confPath = Path(confPath)
if isinstance(dataPath, (str, Path)):
logger.info("Setting data path from alternative path: %s", dataPath)
logger.info("Setting alternative data path: %s", dataPath)
self._dataPath = Path(dataPath)

logger.debug("Config Path: %s", self._confPath)
Expand Down Expand Up @@ -776,8 +776,8 @@ def _checkOptionalPackages(self):

class RecentProjects:

def __init__(self, mainConf):
self.mainConf = mainConf
def __init__(self, config):
self._conf = config
self._data = {}
return

Expand All @@ -786,7 +786,7 @@ def loadCache(self):
"""
self._data = {}

cacheFile = self.mainConf.dataPath(nwFiles.RECENT_FILE)
cacheFile = self._conf.dataPath(nwFiles.RECENT_FILE)
if not cacheFile.is_file():
return True

Expand All @@ -809,7 +809,7 @@ def loadCache(self):
def saveCache(self):
"""Save the cache dictionary of recent projects.
"""
cacheFile = self.mainConf.dataPath(nwFiles.RECENT_FILE)
cacheFile = self._conf.dataPath(nwFiles.RECENT_FILE)
cacheTemp = cacheFile.with_suffix(".tmp")
try:
with open(cacheTemp, mode="w+", encoding="utf-8") as outFile:
Expand Down
8 changes: 8 additions & 0 deletions novelwriter/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class nwConst:
MAX_DOCSIZE = 5000000 # Maxium size of a single document
MAX_BUILDSIZE = 10000000 # Maxium size of a project build

# URLs
URL_WEB = "https://novelwriter.io"
URL_DOCS = "https://docs.novelwriter.io"
URL_CODE = "https://github.com/vkbo/novelWriter"
URL_REPORT = "https://github.com/vkbo/novelWriter/issues"
URL_HELP = "https://github.com/vkbo/novelWriter/discussions"
URL_RELEASE = "https://github.com/vkbo/novelWriter/releases/latest"

# END Class nwConst


Expand Down
8 changes: 2 additions & 6 deletions novelwriter/core/coretools.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@

import shutil
import logging
import novelwriter

from time import time
from functools import partial

from PyQt5.QtCore import QCoreApplication

from novelwriter import CONFIG
from novelwriter.enum import nwAlert
from novelwriter.common import minmax, simplified
from novelwriter.constants import nwItemClass
Expand Down Expand Up @@ -268,12 +268,8 @@ class ProjectBuilder:
"""

def __init__(self, mainGui):

self.mainGui = mainGui
self.mainConf = novelwriter.CONFIG

self.tr = partial(QCoreApplication.translate, "NWProject")

return

##
Expand Down Expand Up @@ -431,7 +427,7 @@ def _extractSampleProject(self, data):
logger.error("No project path set for the example project")
return False

pkgSample = self.mainConf.assetPath("sample.zip")
pkgSample = CONFIG.assetPath("sample.zip")
if pkgSample.is_file():
try:
shutil.unpack_archive(pkgSample, projPath)
Expand Down
21 changes: 10 additions & 11 deletions novelwriter/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

import json
import logging
import novelwriter

from time import time
from pathlib import Path
from functools import partial

from PyQt5.QtCore import QCoreApplication, QObject, pyqtSignal

from novelwriter import CONFIG, __version__, __hexversion__
from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert
from novelwriter.error import logException
from novelwriter.constants import trConst, nwFiles, nwLabels
Expand All @@ -58,8 +58,7 @@ def __init__(self, mainGui):
super().__init__(parent=mainGui)

# Internal
self.mainConf = novelwriter.CONFIG
self.mainGui = mainGui
self.mainGui = mainGui

# Core Elements
self._options = OptionState(self) # Project-specific GUI options
Expand Down Expand Up @@ -328,7 +327,7 @@ def openProject(self, projPath, overrideLock=False):
# Check novelWriter Version
# =========================

if xmlReader.hexVersion > hexToInt(novelwriter.__hexversion__):
if xmlReader.hexVersion > hexToInt(__hexversion__):
msgYes = self.mainGui.askQuestion(
self.tr("Version Conflict"),
self.tr(
Expand All @@ -337,7 +336,7 @@ def openProject(self, projPath, overrideLock=False):
"continue to open the project, some attributes and "
"settings may not be preserved, but the overall project "
"should be fine. Continue opening the project?"
).format(appVersion, novelwriter.__version__)
).format(appVersion, __version__)
)
if not msgYes:
self.clearProject()
Expand All @@ -351,7 +350,7 @@ def openProject(self, projPath, overrideLock=False):
self._loadProjectLocalisation()

# Update recent projects
self.mainConf.recentProjects.update(
CONFIG.recentProjects.update(
self._storage.storagePath, self._data.name, sum(self._data.initCounts), time()
)

Expand Down Expand Up @@ -422,7 +421,7 @@ def saveProject(self, autoSave=False):
self._storage.runPostSaveTasks(autoSave=autoSave)

# Update recent projects
self.mainConf.recentProjects.update(
CONFIG.recentProjects.update(
self._storage.storagePath, self._data.name, sum(self._data.currCounts), saveTime
)

Expand Down Expand Up @@ -455,7 +454,7 @@ def backupProject(self, doNotify):
logger.info("Backing up project")
self.mainGui.setStatus(self.tr("Backing up project ..."))

backupPath = self.mainConf.backupPath()
backupPath = CONFIG.backupPath()
if not isinstance(backupPath, Path):
self.mainGui.makeAlert(self.tr(
"Cannot backup project because no valid backup path is set. "
Expand Down Expand Up @@ -677,13 +676,13 @@ def _setStatusImport(self, new, delete, target):
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 CONFIG._nwLangPath is None:
self._langData = {}
return False

langFile = Path(self.mainConf._nwLangPath) / f"project_{self._data.language}.json"
langFile = Path(CONFIG._nwLangPath) / f"project_{self._data.language}.json"
if not langFile.is_file():
langFile = Path(self.mainConf._nwLangPath) / "project_en_GB.json"
langFile = Path(CONFIG._nwLangPath) / "project_en_GB.json"

try:
with open(langFile, mode="r", encoding="utf-8") as inFile:
Expand Down
6 changes: 3 additions & 3 deletions novelwriter/core/projectxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
"""

import logging
import novelwriter

from enum import Enum
from lxml import etree
from time import time
from pathlib import Path

from novelwriter import __version__, __hexversion__
from novelwriter.common import (
checkBool, checkInt, checkString, checkStringNone, formatTimeStamp,
hexToInt, simplified, yesNo
Expand Down Expand Up @@ -501,8 +501,8 @@ def write(self, projData, projContent, saveTime, editTime):
logger.debug("Writing project XML")

xRoot = etree.Element("novelWriterXML", attrib={
"appVersion": str(novelwriter.__version__),
"hexVersion": str(novelwriter.__hexversion__),
"appVersion": str(__version__),
"hexVersion": str(__hexversion__),
"fileVersion": FILE_VERSION,
"timeStamp": formatTimeStamp(saveTime),
})
Expand Down
10 changes: 5 additions & 5 deletions novelwriter/core/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

import random
import logging
import novelwriter

from PyQt5.QtGui import QIcon, QPainter, QPainterPath, QPixmap, QColor
from PyQt5.QtCore import QRectF, Qt

from novelwriter import CONFIG
from novelwriter.common import minmax, simplified

logger = logging.getLogger(__name__)
Expand All @@ -47,11 +47,11 @@ def __init__(self, type):
self._store = {}
self._default = None

self._iPX = novelwriter.CONFIG.pxInt(24)
self._iPX = CONFIG.pxInt(24)

pA = novelwriter.CONFIG.pxInt(2)
pB = novelwriter.CONFIG.pxInt(20)
pR = float(novelwriter.CONFIG.pxInt(4))
pA = CONFIG.pxInt(2)
pB = CONFIG.pxInt(20)
pR = float(CONFIG.pxInt(4))
self._iconPath = QPainterPath()
self._iconPath.addRoundedRect(QRectF(pA, pA, pB, pB), pR, pR)

Expand Down
7 changes: 3 additions & 4 deletions novelwriter/core/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
"""

import logging
import novelwriter

from time import time
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZIP_STORED, ZipFile

from novelwriter import CONFIG
from novelwriter.error import logException
from novelwriter.common import minmax
from novelwriter.constants import nwFiles
Expand All @@ -47,7 +47,6 @@ class NWStorage:

def __init__(self, theProject):

self.mainConf = novelwriter.CONFIG
self.theProject = theProject

self._storagePath = None
Expand Down Expand Up @@ -220,8 +219,8 @@ def writeLockFile(self):
return False

data = [
self.mainConf.hostName, self.mainConf.osType,
self.mainConf.kernelVer, str(int(time()))
CONFIG.hostName, CONFIG.osType,
CONFIG.kernelVer, str(int(time()))
]
try:
self._lockFilePath.write_text(";".join(data), encoding="utf-8")
Expand Down
5 changes: 3 additions & 2 deletions novelwriter/core/tohtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import logging

from novelwriter import CONFIG
from novelwriter.constants import nwKeyWords, nwLabels, nwHtmlUnicode
from novelwriter.core.tokenizer import Tokenizer, stripEscape

Expand Down Expand Up @@ -204,9 +205,9 @@ def doConvert(self):
aStyle.append("margin-top: 0;")

if tStyle & self.A_IND_L:
aStyle.append(f"margin-left: {self.mainConf.tabWidth:d}px;")
aStyle.append(f"margin-left: {CONFIG.tabWidth:d}px;")
if tStyle & self.A_IND_R:
aStyle.append(f"margin-right: {self.mainConf.tabWidth:d}px;")
aStyle.append(f"margin-right: {CONFIG.tabWidth:d}px;")

if len(aStyle) > 0:
stVals = " ".join(aStyle)
Expand Down
2 changes: 0 additions & 2 deletions novelwriter/core/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import re
import logging
import novelwriter

from abc import ABC, abstractmethod
from operator import itemgetter
Expand Down Expand Up @@ -92,7 +91,6 @@ class Tokenizer(ABC):
def __init__(self, theProject):

self.theProject = theProject
self.mainConf = novelwriter.CONFIG

# Data Variables
self._theText = "" # The raw text to be tokenized
Expand Down
4 changes: 2 additions & 2 deletions novelwriter/core/toodt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
"""

import logging
import novelwriter

from lxml import etree
from hashlib import sha256
from zipfile import ZipFile
from datetime import datetime

from novelwriter import __version__
from novelwriter.constants import nwKeyWords, nwLabels
from novelwriter.core.tokenizer import Tokenizer, stripEscape

Expand Down Expand Up @@ -336,7 +336,7 @@ def initDocument(self):
xMeta.text = timeStamp

xMeta = etree.SubElement(self._xMeta, _mkTag("meta", "generator"))
xMeta.text = f"novelWriter/{novelwriter.__version__}"
xMeta.text = f"novelWriter/{__version__}"

xMeta = etree.SubElement(self._xMeta, _mkTag("meta", "initial-creator"))
xMeta.text = self.theProject.data.author
Expand Down
Loading