Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions xmind/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def save(workbook, path=None):
saver = WorkbookSaver(workbook)
saver.save(path)

def save_all(workbook, path=None):
""" Save workbook to given path with all reference in xmind zip file except Revisions.
If path not given, then will save to path that set to workbook.

"""
saver = WorkbookSaver(workbook)
saver.save_all(path)

def main():
pass
Expand Down
61 changes: 61 additions & 0 deletions xmind/core/saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
:license:

"""
import os

__author__ = "aiqi@xmind.net <Woody Ai>"

Expand Down Expand Up @@ -57,6 +58,66 @@ def save(self, path=None):
f=utils.compress(path)
f.write(content, const.CONTENT_XML)

def _get_reference(self, old_path):
reference_dir = utils.temp_dir()

old_file_name, old_ext = utils.split_ext(old_path)

if old_ext != const.XMIND_EXT:
raise Exception("XMind filenames require a '%s' extension" % const.XMIND_EXT)

myzip = utils.extract(old_path)
with myzip as input_stream:
for name in input_stream.namelist():
print(name)
if name == const.CONTENT_XML:
continue
if const.REVISIONS_DIR in name:
continue

target_file = utils.get_abs_path(utils.join_path(reference_dir,name))
if not os.path.exists(os.path.dirname(target_file)):
os.makedirs(os.path.dirname(target_file))
f_handle=open(target_file,"xb")
f_handle.write(myzip.read(name))
f_handle.close()

return reference_dir

def save_all(self, path=None):
"""
Save the workbook to the given path with all references except Revisions for saving space.
If the path is not given, then will save to the path set in workbook.
"""
old_path = self._workbook.get_path()
path = path or self._workbook.get_path()

if not path:
raise Exception("Please specify a filename for the XMind file")

path = utils.get_abs_path(path)
old_path = utils.get_abs_path(old_path)

file_name, ext = utils.split_ext(path)
old_file_name, old_ext = utils.split_ext(old_path)

if ext != const.XMIND_EXT:
raise Exception("XMind filenames require a '%s' extension" % const.XMIND_EXT)

if old_ext != const.XMIND_EXT:
raise Exception("XMind filenames require a '%s' extension" % const.XMIND_EXT)

content = self._get_content()
reference_dir = self._get_reference(old_path)

f = utils.compress(path)
f.write(content, const.CONTENT_XML)

len = reference_dir.__len__()
for dirpath, dirnames, filenames in os.walk(reference_dir):
for filename in filenames:
f.write(utils.join_path(dirpath, filename), utils.join_path(dirpath[len+1:]+os.sep, filename))
f.close()

def main():
pass
Expand Down