mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-06-28 11:10:31 +12:00
Added io handling for zip archives and project files
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
This commit is contained in:
parent
08220eda42
commit
2faac4efa8
|
@ -13,27 +13,19 @@ import manuskript.data as data
|
||||||
|
|
||||||
path = os.path.join(sys.path[1], "sample-projects/book-of-acts")
|
path = os.path.join(sys.path[1], "sample-projects/book-of-acts")
|
||||||
|
|
||||||
settings = data.Settings(path)
|
project = data.Project(path + ".msk")
|
||||||
|
project.load()
|
||||||
|
|
||||||
settings.load()
|
settings = project.settings
|
||||||
settings.save()
|
|
||||||
|
|
||||||
print(settings.properties)
|
print(settings.properties)
|
||||||
|
|
||||||
plots = data.Plots(path)
|
plots = project.plots
|
||||||
|
|
||||||
plots.load()
|
revs = project.revisions
|
||||||
plots.save()
|
|
||||||
|
|
||||||
revs = data.Revisions(path)
|
for status in project.statuses:
|
||||||
|
|
||||||
revs.load()
|
|
||||||
|
|
||||||
statusHost = data.StatusHost(path)
|
|
||||||
|
|
||||||
statusHost.load()
|
|
||||||
|
|
||||||
for status in statusHost:
|
|
||||||
print("--" + str(status))
|
print("--" + str(status))
|
||||||
|
|
||||||
statusHost.save()
|
settings.set("saveToZip", True)
|
||||||
|
project.save()
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# --!-- coding: utf8 --!--
|
# --!-- coding: utf8 --!--
|
||||||
|
|
||||||
|
|
||||||
from manuskript.data.plots import Plots, PlotLine, PlotStep
|
from manuskript.data.plots import Plots, PlotLine, PlotStep
|
||||||
|
from manuskript.data.project import Project
|
||||||
from manuskript.data.revisions import Revisions
|
from manuskript.data.revisions import Revisions
|
||||||
from manuskript.data.settings import Settings
|
from manuskript.data.settings import Settings
|
||||||
from manuskript.data.status import StatusHost, Status
|
from manuskript.data.status import StatusHost, Status
|
||||||
|
|
|
@ -135,8 +135,12 @@ class Plots:
|
||||||
cls.loadPlotLine(plots, element)
|
cls.loadPlotLine(plots, element)
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
tree = self.file.load()
|
try:
|
||||||
Plots.loadPlots(self, tree.getroot())
|
tree = self.file.load()
|
||||||
|
Plots.loadPlots(self, tree.getroot())
|
||||||
|
except FileNotFoundError:
|
||||||
|
self.host.reset()
|
||||||
|
self.lines.clear()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def saveElementAttribute(cls, element: etree.Element, name: str, value):
|
def saveElementAttribute(cls, element: etree.Element, name: str, value):
|
||||||
|
|
48
manuskript/data/project.py
Normal file
48
manuskript/data/project.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# --!-- coding: utf8 --!--
|
||||||
|
|
||||||
|
from zipfile import BadZipFile
|
||||||
|
from manuskript.data.plots import Plots
|
||||||
|
from manuskript.data.revisions import Revisions
|
||||||
|
from manuskript.data.settings import Settings
|
||||||
|
from manuskript.data.status import StatusHost
|
||||||
|
from manuskript.io.mskFile import MskFile
|
||||||
|
|
||||||
|
|
||||||
|
class Project:
|
||||||
|
|
||||||
|
def __init__(self, path):
|
||||||
|
self.file = MskFile(path)
|
||||||
|
|
||||||
|
self.statuses = StatusHost(self.file.dir_path)
|
||||||
|
self.settings = Settings(self.file.dir_path)
|
||||||
|
self.plots = Plots(self.file.dir_path)
|
||||||
|
self.revisions = Revisions(self.file.dir_path)
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
del self.file
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
try:
|
||||||
|
self.file.load()
|
||||||
|
except BadZipFile:
|
||||||
|
return
|
||||||
|
except FileNotFoundError:
|
||||||
|
return
|
||||||
|
|
||||||
|
self.statuses.load()
|
||||||
|
self.settings.load()
|
||||||
|
self.plots.load()
|
||||||
|
self.revisions.load()
|
||||||
|
|
||||||
|
self.file.setZipFile(self.settings.isEnabled("saveToZip"))
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
print("Save project: " + str(self.file.path) + " " + str(self.file.dir_path))
|
||||||
|
|
||||||
|
self.statuses.save()
|
||||||
|
self.settings.save()
|
||||||
|
self.plots.save()
|
||||||
|
#self.revisions.save()
|
||||||
|
|
||||||
|
self.file.save(self.settings.isEnabled("saveToZip"))
|
|
@ -78,5 +78,10 @@ class Revisions:
|
||||||
cls.loadRevisionOutline(revisions, child, element)
|
cls.loadRevisionOutline(revisions, child, element)
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
tree = self.file.load()
|
try:
|
||||||
Revisions.loadRevisionOutline(self, tree.getroot(), None)
|
tree = self.file.load()
|
||||||
|
self.outline.clear()
|
||||||
|
|
||||||
|
Revisions.loadRevisionOutline(self, tree.getroot(), None)
|
||||||
|
except FileNotFoundError:
|
||||||
|
self.outline.clear()
|
||||||
|
|
|
@ -15,6 +15,15 @@ class Settings:
|
||||||
if initDefault:
|
if initDefault:
|
||||||
Settings.loadDefaultSettings(self)
|
Settings.loadDefaultSettings(self)
|
||||||
|
|
||||||
|
def get(self, key: str):
|
||||||
|
return self.properties.get(key)
|
||||||
|
|
||||||
|
def isEnabled(self, key: str) -> bool:
|
||||||
|
return self.properties.get(key, False) is True
|
||||||
|
|
||||||
|
def set(self, key: str, value):
|
||||||
|
self.properties[key] = value
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self.properties.__iter__()
|
return self.properties.__iter__()
|
||||||
|
|
||||||
|
@ -113,7 +122,10 @@ class Settings:
|
||||||
}
|
}
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
self.properties = self.file.load()
|
try:
|
||||||
|
self.properties = self.file.load()
|
||||||
|
except FileNotFoundError:
|
||||||
|
Settings.loadDefaultSettings(self)
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
self.file.save(self.properties)
|
self.file.save(self.properties)
|
||||||
|
|
|
@ -48,8 +48,12 @@ class StatusHost:
|
||||||
return self.statuses.values().__iter__()
|
return self.statuses.values().__iter__()
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
text = self.file.load()
|
try:
|
||||||
self.statuses.clear()
|
text = self.file.load()
|
||||||
|
self.statuses.clear()
|
||||||
|
except FileNotFoundError:
|
||||||
|
self.statuses.clear()
|
||||||
|
return
|
||||||
|
|
||||||
if len(text) <= 1:
|
if len(text) <= 1:
|
||||||
return
|
return
|
||||||
|
|
|
@ -5,10 +5,5 @@ from manuskript.io.textFile import TextFile
|
||||||
from manuskript.io.jsonFile import JsonFile
|
from manuskript.io.jsonFile import JsonFile
|
||||||
from manuskript.io.xmlFile import XmlFile
|
from manuskript.io.xmlFile import XmlFile
|
||||||
from manuskript.io.opmlFile import OpmlFile
|
from manuskript.io.opmlFile import OpmlFile
|
||||||
|
from manuskript.io.zipFile import ZipFile
|
||||||
extensions = {
|
from manuskript.io.mskFile import MskFile
|
||||||
".txt": TextFile,
|
|
||||||
".json": JsonFile,
|
|
||||||
".xml": XmlFile,
|
|
||||||
".opml": OpmlFile
|
|
||||||
}
|
|
||||||
|
|
60
manuskript/io/mskFile.py
Normal file
60
manuskript/io/mskFile.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# --!-- coding: utf8 --!--
|
||||||
|
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
from manuskript.io.textFile import TextFile
|
||||||
|
from manuskript.io.zipFile import ZipFile
|
||||||
|
|
||||||
|
|
||||||
|
class MskFile(TextFile, ZipFile):
|
||||||
|
|
||||||
|
def __init__(self, path):
|
||||||
|
dir_path = os.path.splitext(path)[0]
|
||||||
|
|
||||||
|
if (not os.path.isdir(dir_path)) or (os.path.getsize(path) > 1):
|
||||||
|
dir_path = None
|
||||||
|
|
||||||
|
self.zipFile = dir_path is None
|
||||||
|
ZipFile.__init__(self, path, dir_path)
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
ZipFile.__del__(self)
|
||||||
|
|
||||||
|
if self.isZipFile() and (self.tmp is None) and not (self.dir_path is None):
|
||||||
|
shutil.rmtree(self.dir_path)
|
||||||
|
|
||||||
|
def isZipFile(self) -> bool:
|
||||||
|
return self.zipFile
|
||||||
|
|
||||||
|
def setZipFile(self, zipFile: bool):
|
||||||
|
if zipFile is self.zipFile:
|
||||||
|
return
|
||||||
|
|
||||||
|
if not zipFile:
|
||||||
|
self.dir_path = os.path.splitext(self.path)[0]
|
||||||
|
os.mkdir(self.dir_path)
|
||||||
|
ZipFile.load(self)
|
||||||
|
|
||||||
|
self.zipFile = zipFile
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
if self.zipFile:
|
||||||
|
ZipFile.load(self)
|
||||||
|
else:
|
||||||
|
value = TextFile.load(self)
|
||||||
|
|
||||||
|
if value == "1":
|
||||||
|
self.setZipFile(False)
|
||||||
|
|
||||||
|
return self.zipFile
|
||||||
|
|
||||||
|
def save(self, content=None):
|
||||||
|
if not (content is None):
|
||||||
|
self.setZipFile(content)
|
||||||
|
|
||||||
|
if self.zipFile:
|
||||||
|
ZipFile.save(self)
|
||||||
|
else:
|
||||||
|
TextFile.save(self, "1")
|
51
manuskript/io/zipFile.py
Normal file
51
manuskript/io/zipFile.py
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# --!-- coding: utf8 --!--
|
||||||
|
|
||||||
|
import tempfile
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
from zipfile import ZipFile as _ZipFile
|
||||||
|
from manuskript.io.abstractFile import AbstractFile
|
||||||
|
|
||||||
|
|
||||||
|
class ZipFile(AbstractFile):
|
||||||
|
|
||||||
|
def __init__(self, path, dir_path=None):
|
||||||
|
AbstractFile.__init__(self, path)
|
||||||
|
|
||||||
|
if dir_path is None:
|
||||||
|
self.tmp = tempfile.TemporaryDirectory()
|
||||||
|
dir_path = self.tmp.name
|
||||||
|
else:
|
||||||
|
self.tmp = None
|
||||||
|
|
||||||
|
self.dir_path = dir_path
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if not (self.tmp is None):
|
||||||
|
self.tmp.cleanup()
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
if self.dir_path is None:
|
||||||
|
self.tmp = tempfile.TemporaryDirectory()
|
||||||
|
self.dir_path = self.tmp.name
|
||||||
|
|
||||||
|
archive = _ZipFile(self.path)
|
||||||
|
archive.extractall(self.dir_path)
|
||||||
|
return self.dir_path
|
||||||
|
|
||||||
|
def save(self, content=None):
|
||||||
|
if not (content is None):
|
||||||
|
if not (self.tmp is None):
|
||||||
|
self.tmp.cleanup()
|
||||||
|
|
||||||
|
self.tmp = None
|
||||||
|
self.dir_path = content
|
||||||
|
elif self.dir_path is None:
|
||||||
|
if self.tmp is None:
|
||||||
|
self.tmp = tempfile.TemporaryDirectory()
|
||||||
|
|
||||||
|
self.dir_path = self.tmp.name
|
||||||
|
|
||||||
|
shutil.make_archive(self.path, 'zip', self.dir_path)
|
||||||
|
shutil.move(self.path + ".zip", self.path)
|
Loading…
Reference in a new issue