manuskript/manuskript/loadSave.py

69 lines
1.6 KiB
Python
Raw Normal View History

2015-05-31 16:03:07 +12:00
#!/usr/bin/env python
2016-03-04 06:48:53 +13:00
# --!-- coding: utf8 --!--
2015-05-31 16:03:07 +12:00
# The loadSave file calls the proper functions to load and save file
# trying to detect the proper file format if it comes from an older version
2016-03-10 04:02:22 +13:00
import os
2016-03-08 21:21:44 +13:00
import zipfile
2016-02-07 00:34:22 +13:00
import manuskript.load_save.version_0 as v0
import manuskript.load_save.version_1 as v1
2016-02-07 00:34:22 +13:00
import logging
LOGGER = logging.getLogger(__name__)
2016-03-08 21:21:44 +13:00
def saveProject(version=None):
2016-02-07 00:34:22 +13:00
2016-03-10 23:45:40 +13:00
# While debugging, we don't save the project
2016-03-11 02:11:28 +13:00
# return
2016-03-10 23:45:40 +13:00
if version == 0:
return v0.saveProject()
2015-06-17 23:25:46 +12:00
else:
return v1.saveProject()
2016-03-10 23:45:40 +13:00
2016-03-04 06:48:53 +13:00
def clearSaveCache():
v1.cache = {}
def loadProject(project):
2016-03-04 06:48:53 +13:00
# Detect version
2016-03-08 21:21:44 +13:00
isZip = False
version = 0
2016-03-04 06:48:53 +13:00
2016-03-10 04:02:22 +13:00
# Is it a zip?
2016-03-08 21:21:44 +13:00
try:
zf = zipfile.ZipFile(project)
isZip = True
except zipfile.BadZipFile:
isZip = False
# Does it have a VERSION in zip root?
2016-03-31 21:45:58 +13:00
# Was used in transition between 0.2.0 and 0.3.0
# So VERSION part can be deleted for manuskript 0.4.0
2016-03-08 21:21:44 +13:00
if isZip and "VERSION" in zf.namelist():
version = int(zf.read("VERSION"))
2016-03-31 21:45:58 +13:00
# Does it have a MANUSKRIPT in zip root?
elif isZip and "MANUSKRIPT" in zf.namelist():
version = int(zf.read("MANUSKRIPT"))
# Zip but no VERSION/MANUSKRIPT: oldest file format
2016-03-08 21:21:44 +13:00
elif isZip:
version = 0
# Not a zip
else:
with open(project, 'rt', encoding="utf-8") as f:
2016-03-10 05:20:43 +13:00
version = int(f.read())
2016-03-08 21:21:44 +13:00
LOGGER.info("Loading: %s", project)
LOGGER.info("Detected file format version: {}. Zip: {}.".format(version, isZip))
2016-03-08 21:21:44 +13:00
2016-03-10 23:45:40 +13:00
if version == 0:
return v0.loadProject(project)
2015-06-17 23:25:46 +12:00
else:
return v1.loadProject(project, zip=isZip)