Allow more than one meta data line in documents, and split the current line into three
This commit is contained in:
+51
-48
@@ -30,7 +30,7 @@ import os
|
|||||||
|
|
||||||
from nw.constants import nwAlert
|
from nw.constants import nwAlert
|
||||||
from nw.common import isHandle
|
from nw.common import isHandle
|
||||||
from nw.constants import nwItemLayout, nwItemClass, nwConst
|
from nw.constants import nwItemLayout, nwItemClass
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ class NWDoc():
|
|||||||
self._theItem = None # The currently open item
|
self._theItem = None # The currently open item
|
||||||
self._docHandle = None # The handle of the currently open item
|
self._docHandle = None # The handle of the currently open item
|
||||||
self._fileLoc = None # The file location of the currently open item
|
self._fileLoc = None # The file location of the currently open item
|
||||||
self._docMeta = "" # The meta string of the currently open item
|
self._docMeta = {} # The meta data of the currently open item
|
||||||
|
|
||||||
# Internal Mapping
|
# Internal Mapping
|
||||||
self.makeAlert = self.theParent.makeAlert
|
self.makeAlert = self.theParent.makeAlert
|
||||||
@@ -62,7 +62,7 @@ class NWDoc():
|
|||||||
self._theItem = None
|
self._theItem = None
|
||||||
self._docHandle = None
|
self._docHandle = None
|
||||||
self._fileLoc = None
|
self._fileLoc = None
|
||||||
self._docMeta = ""
|
self._docMeta = {}
|
||||||
return
|
return
|
||||||
|
|
||||||
def openDocument(self, tHandle, showStatus=True, isOrphan=False):
|
def openDocument(self, tHandle, showStatus=True, isOrphan=False):
|
||||||
@@ -94,16 +94,21 @@ class NWDoc():
|
|||||||
self._fileLoc = docPath
|
self._fileLoc = docPath
|
||||||
|
|
||||||
theText = ""
|
theText = ""
|
||||||
self._docMeta = ""
|
self._docMeta = {}
|
||||||
if os.path.isfile(docPath):
|
if os.path.isfile(docPath):
|
||||||
try:
|
try:
|
||||||
with open(docPath, mode="r", encoding="utf8") as inFile:
|
with open(docPath, mode="r", encoding="utf8") as inFile:
|
||||||
fstLine = inFile.readline()
|
|
||||||
if fstLine.startswith("%%~ "):
|
# Check the first <= 10 lines for metadata
|
||||||
# This is the meta line
|
for i in range(10):
|
||||||
self._docMeta = fstLine[4:].strip()
|
inLine = inFile.readline()
|
||||||
else:
|
if inLine.startswith(r"%%~"):
|
||||||
theText = fstLine
|
self._parseMeta(inLine)
|
||||||
|
else:
|
||||||
|
theText = inLine
|
||||||
|
break
|
||||||
|
|
||||||
|
# Load the rest of the file
|
||||||
theText += inFile.read()
|
theText += inFile.read()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -119,8 +124,6 @@ class NWDoc():
|
|||||||
logger.debug("The requested document does not exist.")
|
logger.debug("The requested document does not exist.")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
logger.verbose("DocMeta: '%s'" % self._docMeta)
|
|
||||||
|
|
||||||
if showStatus and not isOrphan:
|
if showStatus and not isOrphan:
|
||||||
self.theParent.setStatus("Opened Document: %s" % self._theItem.itemName)
|
self.theParent.setStatus("Opened Document: %s" % self._theItem.itemName)
|
||||||
|
|
||||||
@@ -145,14 +148,10 @@ class NWDoc():
|
|||||||
if self._theItem is None:
|
if self._theItem is None:
|
||||||
docMeta = ""
|
docMeta = ""
|
||||||
else:
|
else:
|
||||||
itemPath = self.theProject.projTree.getItemPath(self._docHandle)
|
|
||||||
docMeta = (
|
docMeta = (
|
||||||
"%%~ {handlepath:s}:{itemclass:s}:{itemlayout:s}:{itemname:s}\n"
|
f"%%~name: {self._theItem.itemName:s}\n"
|
||||||
).format(
|
f"%%~path: {self._theItem.itemParent:s}/{self._theItem.itemHandle:s}\n"
|
||||||
handlepath = ":".join(itemPath),
|
f"%%~kind: {self._theItem.itemClass.name:s}/{self._theItem.itemLayout.name:s}\n"
|
||||||
itemclass = self._theItem.itemClass.name,
|
|
||||||
itemlayout = self._theItem.itemLayout.name,
|
|
||||||
itemname = self._theItem.itemName,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -215,39 +214,43 @@ class NWDoc():
|
|||||||
"""Parses the document meta tag and returns the path and name as
|
"""Parses the document meta tag and returns the path and name as
|
||||||
a list and a string.
|
a list and a string.
|
||||||
"""
|
"""
|
||||||
if len(self._docMeta) < 14:
|
theName = self._docMeta.get("name", "")
|
||||||
# Not enough information
|
theParent = self._docMeta.get("parent", None)
|
||||||
return "", [], None, None
|
theClass = self._docMeta.get("class", None)
|
||||||
|
theLayout = self._docMeta.get("layout", None)
|
||||||
|
|
||||||
theMeta = self._docMeta
|
return theName, theParent, theClass, theLayout
|
||||||
|
|
||||||
# Scan for handles
|
##
|
||||||
thePath = []
|
# Internal Functions
|
||||||
for n in range(nwConst.maxDepth + 5):
|
##
|
||||||
if len(theMeta) < 14:
|
|
||||||
break
|
|
||||||
if theMeta[13] == ":":
|
|
||||||
theHandle = theMeta[:13]
|
|
||||||
if isHandle(theHandle):
|
|
||||||
thePath.append(theHandle)
|
|
||||||
theMeta = theMeta[14:]
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
theClass = nwItemClass.NO_CLASS
|
def _parseMeta(self, metaLine):
|
||||||
for aClass in nwItemClass:
|
"""Parse a line from the document statting with the characters
|
||||||
if theMeta.startswith(aClass.name):
|
%%~ that may contain meta data.
|
||||||
theClass = aClass
|
"""
|
||||||
theMeta = theMeta[len(aClass.name)+1:]
|
if metaLine.startswith("%%~name:"):
|
||||||
|
self._docMeta["name"] = metaLine[9:].strip()
|
||||||
|
|
||||||
theLayout = nwItemLayout.NO_LAYOUT
|
elif metaLine.startswith("%%~path:"):
|
||||||
for aLayout in nwItemLayout:
|
metaVal = metaLine[9:].strip()
|
||||||
if theMeta.startswith(aLayout.name):
|
metaBits = metaVal.split("/")
|
||||||
theLayout = aLayout
|
if len(metaBits) == 2:
|
||||||
theMeta = theMeta[len(aLayout.name)+1:]
|
if isHandle(metaBits[0]):
|
||||||
|
self._docMeta["parent"] = metaBits[0]
|
||||||
|
if isHandle(metaBits[1]):
|
||||||
|
self._docMeta["handle"] = metaBits[1]
|
||||||
|
|
||||||
return theMeta, thePath, theClass, theLayout
|
elif metaLine.startswith("%%~kind:"):
|
||||||
|
metaVal = metaLine[9:].strip()
|
||||||
|
metaBits = metaVal.split("/")
|
||||||
|
if len(metaBits) == 2:
|
||||||
|
if metaBits[0] in nwItemClass.__members__:
|
||||||
|
self._docMeta["class"] = nwItemClass[metaBits[0]]
|
||||||
|
if metaBits[1] in nwItemLayout.__members__:
|
||||||
|
self._docMeta["layout"] = nwItemLayout[metaBits[1]]
|
||||||
|
|
||||||
|
# print(self._docMeta)
|
||||||
|
return
|
||||||
|
|
||||||
# END Class NWDoc
|
# END Class NWDoc
|
||||||
|
|||||||
Reference in New Issue
Block a user