From 6e3262f78e56de33ca286b7779f09b075f56196a Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 20 Mar 2022 17:00:29 +0100 Subject: [PATCH] Minor fixes and updates (#1026) * Make the Tokenizer class an abstract class * Fix error message when opening non-file project items --- novelwriter/core/item.py | 6 +++--- novelwriter/core/tokenizer.py | 9 +++++++-- tests/test_core/test_core_tokenizer.py | 21 +++++++++++++-------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/novelwriter/core/item.py b/novelwriter/core/item.py index 07db4a41..67dd836c 100644 --- a/novelwriter/core/item.py +++ b/novelwriter/core/item.py @@ -183,9 +183,9 @@ class NWItem(): self.setParent(xItem.attrib.get("parent", None)) self.setOrder(xItem.attrib.get("order", 0)) - self.setType(xItem.attrib.get("type", None)) - self.setClass(xItem.attrib.get("class", None)) - self.setLayout(xItem.attrib.get("layout", None)) + self.setType(xItem.attrib.get("type", nwItemType.NO_TYPE)) + self.setClass(xItem.attrib.get("class", nwItemClass.NO_CLASS)) + self.setLayout(xItem.attrib.get("layout", nwItemLayout.NO_LAYOUT)) for xValue in xItem: if xValue.tag == "meta": diff --git a/novelwriter/core/tokenizer.py b/novelwriter/core/tokenizer.py index 6a3a9809..271ffd2c 100644 --- a/novelwriter/core/tokenizer.py +++ b/novelwriter/core/tokenizer.py @@ -1,7 +1,7 @@ """ novelWriter – Text Tokenizer ============================ -Splits a piece of novelWriter markdown text into its elements +Split novelWriter plain text into its elements File History: Created: 2019-05-05 [0.0.1] @@ -27,6 +27,7 @@ import re import logging import novelwriter +from abc import ABC, abstractmethod from operator import itemgetter from functools import partial @@ -40,7 +41,7 @@ from novelwriter.core.document import NWDoc logger = logging.getLogger(__name__) -class Tokenizer(): +class Tokenizer(ABC): # In-Text Format FMT_B_B = 1 # Begin bold @@ -267,6 +268,10 @@ class Tokenizer(): # Class Methods ## + @abstractmethod + def doConvert(self): + raise NotImplementedError + def addRootHeading(self, theHandle): """Add a heading at the start of a new root folder. """ diff --git a/tests/test_core/test_core_tokenizer.py b/tests/test_core/test_core_tokenizer.py index 0c57214b..d277a601 100644 --- a/tests/test_core/test_core_tokenizer.py +++ b/tests/test_core/test_core_tokenizer.py @@ -28,12 +28,17 @@ from novelwriter.core import NWProject, NWDoc from novelwriter.core.tokenizer import Tokenizer +class BareTokenizer(Tokenizer): + def doConvert(self): + pass + + @pytest.mark.core def testCoreToken_Setters(mockGUI): """Test all the setters for the Tokenizer class. """ theProject = NWProject(mockGUI) - theToken = Tokenizer(theProject) + theToken = BareTokenizer(theProject) # Verify defaults assert theToken._fmtTitle == "%title%" @@ -135,7 +140,7 @@ def testCoreToken_TextOps(monkeypatch, nwMinimal, mockGUI): theProject.projLang = "en" theProject._loadProjectLocalisation() - theToken = Tokenizer(theProject) + theToken = BareTokenizer(theProject) theToken.setKeepMarkdown(True) assert theProject.openProject(nwMinimal) @@ -222,7 +227,7 @@ def testCoreToken_HeaderFormat(mockGUI): """Test the tokenization of header formats in the Tokenizer class. """ theProject = NWProject(mockGUI) - theToken = Tokenizer(theProject) + theToken = BareTokenizer(theProject) theToken.setKeepMarkdown(True) # Title @@ -426,7 +431,7 @@ def testCoreToken_MetaFormat(mockGUI): """Test the tokenization of meta formats in the Tokenizer class. """ theProject = NWProject(mockGUI) - theToken = Tokenizer(theProject) + theToken = BareTokenizer(theProject) theToken.setKeepMarkdown(True) # Comment @@ -495,7 +500,7 @@ def testCoreToken_MarginFormat(mockGUI): """Test the tokenization of margin formats in the Tokenizer class. """ theProject = NWProject(mockGUI) - theToken = Tokenizer(theProject) + theToken = BareTokenizer(theProject) theToken.setKeepMarkdown(True) # Alignment and Indentation @@ -550,7 +555,7 @@ def testCoreToken_TextFormat(mockGUI): """Test the tokenization of text formats in the Tokenizer class. """ theProject = NWProject(mockGUI) - theToken = Tokenizer(theProject) + theToken = BareTokenizer(theProject) theToken.setKeepMarkdown(True) # Text @@ -672,7 +677,7 @@ def testCoreToken_SpecialFormat(mockGUI): """Test the tokenization of special formats in the Tokenizer class. """ theProject = NWProject(mockGUI) - theToken = Tokenizer(theProject) + theToken = BareTokenizer(theProject) theToken._isNovel = True @@ -877,7 +882,7 @@ def testCoreToken_ProcessHeaders(mockGUI): theProject = NWProject(mockGUI) theProject.projLang = "en" theProject._loadProjectLocalisation() - theToken = Tokenizer(theProject) + theToken = BareTokenizer(theProject) # Nothing theToken._theText = "Some text ...\n"