Rename NWDoc class to NWDocument
This commit is contained in:
@@ -34,7 +34,7 @@ from novelwriter.common import isHandle, sha256sum
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class NWDoc:
|
class NWDocument:
|
||||||
|
|
||||||
def __init__(self, theProject, theHandle):
|
def __init__(self, theProject, theHandle):
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ class NWDoc:
|
|||||||
return
|
return
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<NWDoc handle={self._docHandle}>"
|
return f"<NWDocument handle={self._docHandle}>"
|
||||||
|
|
||||||
def __bool__(self):
|
def __bool__(self):
|
||||||
return self._docHandle is not None and bool(self._theItem)
|
return self._docHandle is not None and bool(self._theItem)
|
||||||
@@ -277,4 +277,4 @@ class NWDoc:
|
|||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# END Class NWDoc
|
# END Class NWDocument
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ from time import time
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from novelwriter.constants import nwFiles
|
from novelwriter.constants import nwFiles
|
||||||
from novelwriter.core.document import NWDoc
|
from novelwriter.core.document import NWDocument
|
||||||
from novelwriter.core.projectxml import ProjectXMLReader, ProjectXMLWriter
|
from novelwriter.core.projectxml import ProjectXMLReader, ProjectXMLWriter
|
||||||
from novelwriter.error import logException
|
from novelwriter.error import logException
|
||||||
|
|
||||||
@@ -159,8 +159,8 @@ class NWStorage:
|
|||||||
"""Return a document wrapper object.
|
"""Return a document wrapper object.
|
||||||
"""
|
"""
|
||||||
if self._runtimePath is not None:
|
if self._runtimePath is not None:
|
||||||
return NWDoc(self.theProject, tHandle)
|
return NWDocument(self.theProject, tHandle)
|
||||||
return NWDoc(self.theProject, None)
|
return NWDocument(self.theProject, None)
|
||||||
|
|
||||||
def getMetaFile(self, fileName):
|
def getMetaFile(self, fileName):
|
||||||
"""Return the path to a file in the project meta folder.
|
"""Return the path to a file in the project meta folder.
|
||||||
|
|||||||
@@ -470,8 +470,8 @@ class GuiDocEditor(QTextEdit):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def saveText(self):
|
def saveText(self):
|
||||||
"""Save the text currently in the editor to the NWDoc object,
|
"""Save the text currently in the editor to the NWDocument
|
||||||
and update the NWItem meta data.
|
object, and update the NWItem meta data.
|
||||||
"""
|
"""
|
||||||
if self._nwItem is None or self._nwDocument is None:
|
if self._nwItem is None or self._nwDocument is None:
|
||||||
logger.error("Cannot save text as no document is open")
|
logger.error("Cannot save text as no document is open")
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
novelWriter – NWDoc Class Tester
|
novelWriter – NWDocument Class Tester
|
||||||
================================
|
=====================================
|
||||||
|
|
||||||
This file is a part of novelWriter
|
This file is a part of novelWriter
|
||||||
Copyright 2018–2022, Veronica Berglyd Olsen
|
Copyright 2018–2022, Veronica Berglyd Olsen
|
||||||
@@ -27,12 +27,12 @@ from tools import C, buildTestProject, readFile, writeFile
|
|||||||
|
|
||||||
from novelwriter.enum import nwItemClass, nwItemLayout
|
from novelwriter.enum import nwItemClass, nwItemLayout
|
||||||
from novelwriter.core.project import NWProject
|
from novelwriter.core.project import NWProject
|
||||||
from novelwriter.core.document import NWDoc
|
from novelwriter.core.document import NWDocument
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncDir, mockRnd):
|
def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncDir, mockRnd):
|
||||||
"""Test loading and saving a document with the NWDoc class.
|
"""Test loading and saving a document with the NWDocument class.
|
||||||
"""
|
"""
|
||||||
theProject = NWProject(mockGUI)
|
theProject = NWProject(mockGUI)
|
||||||
mockRnd.reset()
|
mockRnd.reset()
|
||||||
@@ -42,31 +42,31 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncDir, mockRnd):
|
|||||||
# =============
|
# =============
|
||||||
|
|
||||||
# Not a valid handle
|
# Not a valid handle
|
||||||
theDoc = NWDoc(theProject, "stuff")
|
theDoc = NWDocument(theProject, "stuff")
|
||||||
assert bool(theDoc) is False
|
assert bool(theDoc) is False
|
||||||
assert theDoc.readDocument() is None
|
assert theDoc.readDocument() is None
|
||||||
|
|
||||||
# Non-existent handle
|
# Non-existent handle
|
||||||
theDoc = NWDoc(theProject, C.hInvalid)
|
theDoc = NWDocument(theProject, C.hInvalid)
|
||||||
assert theDoc.readDocument() is None
|
assert theDoc.readDocument() is None
|
||||||
assert theDoc._currHash is None
|
assert theDoc._currHash is None
|
||||||
|
|
||||||
# Cause open() to fail while loading
|
# Cause open() to fail while loading
|
||||||
with monkeypatch.context() as mp:
|
with monkeypatch.context() as mp:
|
||||||
mp.setattr("builtins.open", causeOSError)
|
mp.setattr("builtins.open", causeOSError)
|
||||||
theDoc = NWDoc(theProject, C.hSceneDoc)
|
theDoc = NWDocument(theProject, C.hSceneDoc)
|
||||||
assert theDoc.readDocument() is None
|
assert theDoc.readDocument() is None
|
||||||
assert theDoc.getError() == "OSError: Mock OSError"
|
assert theDoc.getError() == "OSError: Mock OSError"
|
||||||
|
|
||||||
# Load the text
|
# Load the text
|
||||||
theDoc = NWDoc(theProject, C.hSceneDoc)
|
theDoc = NWDocument(theProject, C.hSceneDoc)
|
||||||
assert theDoc.readDocument() == "### New Scene\n\n"
|
assert theDoc.readDocument() == "### New Scene\n\n"
|
||||||
|
|
||||||
# Try to open a new (non-existent) file
|
# Try to open a new (non-existent) file
|
||||||
xHandle = theProject.newFile("New File", C.hNovelRoot)
|
xHandle = theProject.newFile("New File", C.hNovelRoot)
|
||||||
theDoc = NWDoc(theProject, xHandle)
|
theDoc = NWDocument(theProject, xHandle)
|
||||||
assert bool(theDoc) is True
|
assert bool(theDoc) is True
|
||||||
assert repr(theDoc) == f"<NWDoc handle={xHandle}>"
|
assert repr(theDoc) == f"<NWDocument handle={xHandle}>"
|
||||||
assert theDoc.readDocument() == ""
|
assert theDoc.readDocument() == ""
|
||||||
|
|
||||||
# Write Document
|
# Write Document
|
||||||
@@ -74,7 +74,7 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncDir, mockRnd):
|
|||||||
|
|
||||||
# Set handle and save again
|
# Set handle and save again
|
||||||
theText = "### Test File\n\nText ...\n\n"
|
theText = "### Test File\n\nText ...\n\n"
|
||||||
theDoc = NWDoc(theProject, xHandle)
|
theDoc = NWDocument(theProject, xHandle)
|
||||||
assert theDoc.readDocument(xHandle) == ""
|
assert theDoc.readDocument(xHandle) == ""
|
||||||
assert theDoc.writeDocument(theText) is True
|
assert theDoc.writeDocument(theText) is True
|
||||||
|
|
||||||
@@ -129,19 +129,19 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncDir, mockRnd):
|
|||||||
# ===============
|
# ===============
|
||||||
|
|
||||||
# Delete the last document
|
# Delete the last document
|
||||||
theDoc = NWDoc(theProject, "stuff")
|
theDoc = NWDocument(theProject, "stuff")
|
||||||
assert theDoc.deleteDocument() is False
|
assert theDoc.deleteDocument() is False
|
||||||
assert os.path.isfile(docPath)
|
assert os.path.isfile(docPath)
|
||||||
|
|
||||||
# Cause the delete to fail
|
# Cause the delete to fail
|
||||||
with monkeypatch.context() as mp:
|
with monkeypatch.context() as mp:
|
||||||
mp.setattr("pathlib.Path.unlink", causeOSError)
|
mp.setattr("pathlib.Path.unlink", causeOSError)
|
||||||
theDoc = NWDoc(theProject, xHandle)
|
theDoc = NWDocument(theProject, xHandle)
|
||||||
assert theDoc.deleteDocument() is False
|
assert theDoc.deleteDocument() is False
|
||||||
assert theDoc.getError() == "OSError: Mock OSError"
|
assert theDoc.getError() == "OSError: Mock OSError"
|
||||||
|
|
||||||
# Make the delete pass
|
# Make the delete pass
|
||||||
theDoc = NWDoc(theProject, xHandle)
|
theDoc = NWDocument(theProject, xHandle)
|
||||||
assert theDoc.deleteDocument() is True
|
assert theDoc.deleteDocument() is True
|
||||||
assert not os.path.isfile(docPath)
|
assert not os.path.isfile(docPath)
|
||||||
|
|
||||||
@@ -150,13 +150,13 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncDir, mockRnd):
|
|||||||
|
|
||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testCoreDocument_Methods(mockGUI, fncDir, mockRnd):
|
def testCoreDocument_Methods(mockGUI, fncDir, mockRnd):
|
||||||
"""Test other methods of the NWDoc class.
|
"""Test other methods of the NWDocument class.
|
||||||
"""
|
"""
|
||||||
theProject = NWProject(mockGUI)
|
theProject = NWProject(mockGUI)
|
||||||
mockRnd.reset()
|
mockRnd.reset()
|
||||||
buildTestProject(theProject, fncDir)
|
buildTestProject(theProject, fncDir)
|
||||||
|
|
||||||
theDoc = NWDoc(theProject, C.hSceneDoc)
|
theDoc = NWDocument(theProject, C.hSceneDoc)
|
||||||
docPath = os.path.join(fncDir, "content", C.hSceneDoc+".nwd")
|
docPath = os.path.join(fncDir, "content", C.hSceneDoc+".nwd")
|
||||||
|
|
||||||
assert theDoc.readDocument() == "### New Scene\n\n"
|
assert theDoc.readDocument() == "### New Scene\n\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user