71 lines
2.0 KiB
Python
71 lines
2.0 KiB
Python
# -*- coding: utf-8 -*
|
||
"""novelWriter Project Document
|
||
|
||
novelWriter – Project Document
|
||
================================
|
||
Class holding a document
|
||
|
||
File History:
|
||
Created: 2018-09-29 [0.1.0]
|
||
|
||
"""
|
||
|
||
import logging
|
||
import nw
|
||
|
||
from os import path, mkdir
|
||
from lxml import etree, html
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
class NWDoc():
|
||
|
||
def __init__(self, theProject):
|
||
|
||
self.mainConf = nw.CONFIG
|
||
self.theProject = theProject
|
||
self.docHandle = None
|
||
|
||
return
|
||
|
||
def openDocument(self, tHandle):
|
||
self.docHandle = tHandle
|
||
docDir, docFile = self._assemblePath("main.htm")
|
||
logger.debug("Opening document %s" % path.join(docDir,docFile))
|
||
dataDir = path.join(self.theProject.projPath, docDir)
|
||
docPath = path.join(dataDir, docFile)
|
||
if path.isfile(docPath):
|
||
with open(docPath,mode="r") as inFile:
|
||
return inFile.read()
|
||
else:
|
||
logger.debug("Document does not exist.")
|
||
return ""
|
||
return None
|
||
|
||
def saveDocument(self, docHtml):
|
||
if self.docHandle is None: return False
|
||
docDir, docFile = self._assemblePath("main.htm")
|
||
logger.debug("Saving document %s" % path.join(docDir,docFile))
|
||
dataDir = path.join(self.theProject.projPath, docDir)
|
||
docPath = path.join(dataDir, docFile)
|
||
if not path.isdir(dataDir):
|
||
mkdir(dataDir)
|
||
logger.debug("Created folder %s" % dataDir)
|
||
with open(docPath,mode="wb") as outFile:
|
||
docData = html.fromstring(docHtml)
|
||
outFile.write(html.tostring(
|
||
docData,
|
||
pretty_print = True,
|
||
encoding = "utf-8",
|
||
method = "html",
|
||
))
|
||
return True
|
||
|
||
def _assemblePath(self, docExt):
|
||
if self.docHandle is None: return None
|
||
docDir = "data_"+self.docHandle[0]
|
||
docFile = self.docHandle[1:13]+"_"+docExt
|
||
return docDir, docFile
|
||
|
||
# END Class NWDoc
|