diff --git a/xmind/__init__.py b/xmind/__init__.py index 996de85..0f3392a 100644 --- a/xmind/__init__.py +++ b/xmind/__init__.py @@ -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 diff --git a/xmind/core/saver.py b/xmind/core/saver.py index 2546ab2..d3fb0a8 100644 --- a/xmind/core/saver.py +++ b/xmind/core/saver.py @@ -9,6 +9,7 @@ :license: """ +import os __author__ = "aiqi@xmind.net " @@ -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