Added simple classes to handle file extension related io

Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
This commit is contained in:
TheJackiMonster 2021-05-04 15:28:47 +02:00
parent 7e05b72d83
commit 5239598a91
No known key found for this signature in database
GPG key ID: D850A5F772E880F9
7 changed files with 249 additions and 0 deletions

15
manuskript/io/__init__.py Normal file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env python
# --!-- coding: utf8 --!--
from manuskript.io.textFile import TextFile
from manuskript.io.jsonFile import JsonFile
from manuskript.io.xmlFile import XmlFile
from manuskript.io.opmlFile import OpmlFile
from manuskript.io.plotsFile import PlotsFile
extensions = {
".txt": TextFile,
".json": JsonFile,
".xml": XmlFile,
".opml": OpmlFile
}

View file

@ -0,0 +1,14 @@
#!/usr/bin/env python
# --!-- coding: utf8 --!--
class AbstractFile:
def __init__(self, path):
self.path = path
def load(self):
raise IOError('Loading undefined!')
def save(self, content):
raise IOError('Saving undefined!')

14
manuskript/io/jsonFile.py Normal file
View file

@ -0,0 +1,14 @@
#!/usr/bin/env python
# --!-- coding: utf8 --!--
import json
from manuskript.io.textFile import TextFile
class JsonFile(TextFile):
def load(self):
return json.loads(TextFile.load(self))
def save(self, content):
TextFile.save(self, json.dumps(content))

61
manuskript/io/opmlFile.py Normal file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env python
# --!-- coding: utf8 --!--
from lxml import etree
from manuskript.io.xmlFile import XmlFile
class OpmlOutlineItem:
def __init__(self, tag="outline"):
self.tag = tag
self.attributes = dict()
self.children = []
def keys(self):
return self.attributes.keys()
def values(self):
return self.attributes.values()
class OpmlFile(XmlFile):
@classmethod
def loadOutline(cls, element):
outline = OpmlOutlineItem(element.tag)
for key in element.keys():
outline.attributes[key] = element.get(key)
for child in element.getchildren():
outline.children.append(cls.loadOutline(child))
return outline
def load(self):
tree = XmlFile.load(self)
root = tree.getroot()
return OpmlFile.loadOutline(root)
@classmethod
def saveOutline(cls, outline, parent=None):
if parent is None:
element = etree.Element(outline.tag)
else:
element = etree.SubElement(parent, outline.tag)
for key in outline.keys():
element.attrib[key] = outline.attributes[key]
for child in outline.children:
cls.saveOutline(child, element)
return element
def save(self, content):
root = OpmlFile.saveOutline(content)
tree = etree.ElementTree(root)
XmlFile.save(self, tree)

114
manuskript/io/plotsFile.py Normal file
View file

@ -0,0 +1,114 @@
#!/usr/bin/env python
# --!-- coding: utf8 --!--
from lxml import etree
from manuskript.io.xmlFile import XmlFile
class PlotStep:
def __init__(self, name, ID, meta="", summary=""):
self.name = name
self.ID = ID
self.meta = meta
self.summary = summary
class PlotItem:
def __init__(self, name, ID, importance, description, result):
self.name = name
self.ID = ID
self.importance = importance
self.characters = []
self.description = description
self.result = result
self.steps = []
class PlotsFile(XmlFile):
@classmethod
def loadPlot(cls, element):
plotID = element.get("ID")
if plotID is None:
return None
plot = PlotItem(
element.get("name"),
int(plotID),
int(element.get("importance", 0)),
element.get("description"),
element.get("result")
)
for characterID in element.get("characters", "").split(','):
try:
plot.characters.append(int(characterID))
except ValueError:
continue
for child in element.findall("step"):
stepID = child.get("ID")
if stepID is None:
continue
step = PlotStep(
child.get("name"),
int(stepID),
child.get("meta"),
child.get("summary")
)
plot.steps.append(step)
return plot
def load(self):
tree = XmlFile.load(self)
root = tree.getroot()
plots = []
for element in root.findall("plot"):
plots.append(PlotsFile.loadPlot(element))
return plots
@classmethod
def saveElementAttribute(cls, element, name, value):
if value is None:
return
element.set(name, str(value))
@classmethod
def savePlot(cls, parent, plot):
element = etree.SubElement(parent, "plot")
cls.saveElementAttribute(element, "name", plot.name)
cls.saveElementAttribute(element, "ID", plot.ID)
cls.saveElementAttribute(element, "importance", plot.importance)
cls.saveElementAttribute(element, "characters", ",".join([str(characterID) for characterID in plot.characters]))
cls.saveElementAttribute(element, "description", plot.description)
cls.saveElementAttribute(element, "result", plot.result)
for step in plot.steps:
child = etree.SubElement(element, "step")
cls.saveElementAttribute(child, "name", step.name)
cls.saveElementAttribute(child, "ID", step.ID)
cls.saveElementAttribute(child, "meta", step.meta)
cls.saveElementAttribute(child, "summary", step.summary)
def save(self, plots):
root = etree.Element("root")
for plot in plots:
PlotsFile.savePlot(root, plot)
tree = etree.ElementTree(root)
XmlFile.save(self, tree)

15
manuskript/io/textFile.py Normal file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env python
# --!-- coding: utf8 --!--
from manuskript.io.abstractFile import AbstractFile
class TextFile(AbstractFile):
def load(self):
with open(self.path, 'rb') as file:
return file.read()
def save(self, content):
with open(self.path, 'wb') as file:
file.write(content)

16
manuskript/io/xmlFile.py Normal file
View file

@ -0,0 +1,16 @@
#!/usr/bin/env python
# --!-- coding: utf8 --!--
from lxml import etree
from manuskript.io.abstractFile import AbstractFile
class XmlFile(AbstractFile):
def load(self):
with open(self.path, 'rb') as file:
return etree.parse(file)
def save(self, content):
with open(self.path, 'wb') as file:
content.write(file, encoding="utf-8", xml_declaration=True, pretty_print=True)